i am trying to delete some rows on my database but when i call destroy action, update action works.
this is delete.html.haml
=form_for #post, :url => {:action => 'destroy', :id => #post.id} do |f|
this is route
resources :post
root :to => "post#index"
match '/delete/:id/', :to => "post#delete"
What's the problem? Anybody does understand?
The :action should be delete, to match the name of the action in your controller:
= form_for #post, :url => {:action => 'delete', :id => #post.id} do |f|
If you are using rails 3 then you need to make sure you have <%= csrf_meta_tag %> in your header. I put mine after my <%= javascript_include_tag... %> tags. Also make sure that you include the rails.js. (This will be on your layout files).
More details here: http://railsforum.com/viewtopic.php?id=38460 Look toward the bottom.
The root entry in your routes should be placed after the matchblock. Plus: seems like you mixed up the controller action and the http verb: use delete in the form action and destroy in the controller action call:
=form_for #post, :url => {:action => 'delete', :id => #post.id} do |f|
resources :post
match '/delete/:id/', :to => "post#destroy"
root :to => "post#index"
Related
I am trying to call an action from a controller 2 steps up the folder structure namespace
<%= link_to( "Reset Search" ,
{:controller => "../../application",
:action => 'reset_search',
:reset_search_redirect => reset_search_redirect},
class: 'tiny button') %>
The "../../application"works! however; i dont think it's rails-y. Is there a better way?
Here is the href link when I just put :controller => "application"
/human_resources/settings/application/reset_search?reset_search_redirect=%2Fhuman_resources%2Fsettings
Which does not work.
Here are my routes:
get 'human_resources/' => 'human_resources#index'
namespace :human_resources do
get 'settings/' => 'settings#index'
namespace :settings do
get 'constants/' => 'constants#index'
resources :constants
end
end
resources :test, only: :index
root to: 'home#index'
match ':controller(/:action(/:id))', :via => [:get, :post]
I have ajax comments in my rails app and it all works in the posts show but I'd like to add the form to render the comments in the index page.
I'm using sorl when rendering the index page and I tried to include comments in the post model like this:
searchable(:include => :comments) do
code....
end
but that doesn't render the comments below the posts in the index page, even though I have this in the view
<%= render :partial => 'comments/comment',:collection => #comments, :as => :comment %>
So to help you understand, here is all the code:
_post.html.erb:
<%= simple_form_for [post, Comment.new], :url => comments_path, :remote => true do |f| %>
<%= image_tag current_user.avatar.url(:small) %>
<%= f.input :body, placeholder: 'write a comment...', :input_html => { :rows => "1"}, :label => false %>
//the below two lines do not work, the commentable_id is 0 and commentable_type is blank
//post.comments.commentable_id doesn't work either
<%= f.input :commentable_id, :as => :hidden, :value => Comment.new.commentable_id %>
<%= f.input :commentable_type, :as => :hidden, :value => Comment.new.commentable_type %>
<%= button_tag(type: 'submit', :id => 'commentsend') do %>
<i class="fa fa-check"></i>
<% end %>
<% end %>
<%= render :partial => 'comments/comment',:collection => #comments, :as => :comment %>
Posts_controller:
def index
#user_count = User.where(:school => current_user.school).count
#blub_count = Post.where(:category => "status").where(:school => current_user.school).count
#items_count = Post.where(:school => current_user.school).where.not( :category => "status").count
#search = Post.search(:include => :comments) do #Post.search do
fulltext params[:search]
with(:school, current_user.school)
paginate :page => params[:page], :per_page => 20
order_by(:updated_at, :desc)
end
#posts = #search.results
respond_to do |format|
format.js
format.html
end
end
def show
#post = Post.find(params[:id])
#comments = #post.comment_threads.order('created_at desc')
#new_comment = Comment.build_from(#post, current_user, "")
end
comments_controller:
def create
#comment_hash = params[:comment]
#obj = #comment_hash[:commentable_type].constantize.find(#comment_hash[:commentable_id])
# Not implemented: check to see whether the user has permission to create a comment on this object
#comment = Comment.build_from(#obj, current_user.id, #comment_hash[:body])
if #comment.save
render :partial => "comments/comment", :locals => { :comment => #comment }, :layout => false, :status => :created
else
render :js => "alert('error saving comment');"
end
end
comments model:
acts_as_nested_set :scope => [:commentable_id, :commentable_type]
validates :body, :presence => true
#validates :user, :presence => true
# NOTE: install the acts_as_votable plugin if you
# want user to vote on the quality of comments.
#acts_as_votable
belongs_to :commentable, :polymorphic => true
# NOTE: Comments belong to a user
belongs_to :user
belongs_to :post
# Helper class method that allows you to build a comment
# by passing a commentable object, a user_id, and comment text
# example in readme
def self.build_from(obj, user_id, comment)
new \
:commentable => obj,
:body => comment,
:user_id => user_id
end
#helper method to check if a comment has children
def has_children?
self.children.any?
end
# Helper class method to lookup all comments assigned
# to all commentable types for a given user.
scope :find_comments_by_user, lambda { |user|
where(:user_id => user.id).order('created_at DESC')
}
# Helper class method to look up all comments for
# commentable class name and commentable id.
scope :find_comments_for_commentable, lambda { |commentable_str, commentable_id|
where(:commentable_type => commentable_str.to_s, :commentable_id => commentable_id).order('created_at DESC')
}
# Helper class method to look up a commentable object
# given the commentable class name and id
def self.find_commentable(commentable_str, commentable_id)
commentable_str.constantize.find(commentable_id)
end
The comments form shoudl be like this:
<%= simple_form_for Comment.build_from(post, current_user, ""), :url => comments_path, :remote => true do |f| %>
I have to assign the comment the current post ID(commentable_id) and current user id for commentable_type to build the comment
in the view, to render the comments I used
<%= render post.comment_threads.order('created_at desc') %>
the only issue is the the render is ignoring the div
I'm trying to route the following URL:
/shop/{category_id}/{product_id}/
to a controller action called 'product'
I've got this in my routes:
match "/shop/:category/:id" => "shop#product"
and i've got link_to's as follows:
link_to_unless_current "#{t('murals')}", url_for(:controller => 'shop', :category => 'walls', :id => 'murals')
When the urls are output, they are:
http://0.0.0.0:3000/shop?category=walls&id=murals
instead of the desired:
http://0.0.0.0:3000/shop/walls/murals
if i switch the route, so it goes to:
match "/shop/:category/:id" => "shop#index"
this works, but it's not the right action.
Any help would be appreciated.
try this
<%= link_to_unless_current "#{t('murals')}", custom_path(:category => 'walls', :id => 'murals') %>
match "/shop/:category/:id" => "shop#product", :as => :custom
I have implemented devise and set up the routes to prettyify the urls as so (in the routes file):
devise_scope :user do
get "/login" => "devise/sessions#new"
get "/logout" => "devise/sessions#destroy"
get "/register" => "devise/registrations#new"
end
I have global menu in my application.html.erb file, however, which now inserts devise/controller/action on every link that is on the login or register pages, such as
<%= link_to "Upload Video", {:controller => "videos", :action => "new"} %>
becomes devise/videos/new
Any ideas on how to fix this? I can hack around it but I'm pretty sure it is a simple fix.
Cheers,
s
Take out the devise_scope and try something like this:
devise_for :users,
:controllers => {:registrations => 'devise/registrations', :sessions => 'devise/sessions'},
:path => '/',
:path_names => {:sign_in => 'login', :sign_out => 'logout'}
I'm trying to implement some link_to's using post method.
However, it always puts all the information as a query string in the browser's bar (it behaves as a GET).
Why?
Here is my code:
<%= link_to hotel[:name], {:controller => "gds_hotels", :action => "hotel_details",
:dest => #destination,
:ci => #check_in,
:co => #check_out,
:hotel => hotel,
:rooms => hotel[:rooms]}, :method => "POST" %>
Thank you! =)
You should use button_to
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to