Reputation: 505
I have erb file in tencet contoller
<td><%= link_to "OK", post_path(post.id), method: :suit %></td>
And controller of Post
helper_method :suit
def suit
@post.suit = true
end
And routes
resources :posts
And I get this error "No route matches [POST] "/posts/77" "
How can I fix it?
How pass an argument?
<%= link_to "OK", suit_post_path(post), method: :put %>
I want pass object post and get in controller
def suit
@post = params[:post]
@post.suit = true
if @post.save
redirect_to tencet_show_path
end
end
Upvotes: 1
Views: 2900
Reputation: 26
Method in this case doesn't refer to the controller method but the HTTP method. This is also what is blowing up your route.
Create a custom action
def toggle_suit
@post.suit = true
render :show
end
and update your routes
routes :posts do
post '/suits' => 'posts#toggle_suits', as: :toggle_suit
end
finally update your erb
<%= link_to '...', toggle_suit_url(post: post), method: :post %>
The take away here is that method: :suit
does not do what you think it does. It causes link to to create an invisible form where the link is the submit button allowing it to POST to your app.
Upvotes: 0
Reputation: 663
Assuming you are updating post.suit...
You will have to define the suit route in you config/routes.rb:
resources :posts do
put :suit, on: :member
end
Also, you can't have the method be :suit. It must be one of the methods, like post, put, get, etc...
So, in your .erb, you would have:
<td><%= link_to "OK", suit_post_path(post.id), method: :put %></td>
In the contoller:
def suit
@post.suit = true
if @post.save
redirect_to your_desired_path
else
render :previous_controller_action
end
end
Upvotes: 2
Reputation: 44715
So couple of things. Firstly, define a new route:
resources :posts do
post :suit, on: :member
end
Your suit
method is already in place (However I have no idea what it is supposed to do - from the code I would expect it will raise an exception). Note that this is not a helper_method.
Then update your link:
<td><%= link_to "OK", suit_post_path(post), method: :post %></td>
I expect you will receive TemplateMissing exception at the current stage. If you just want to update your post, then change your suit
action to:
def suit
@post.update_attributes(suit: true)
redirect :back
end
If you still getting some other exception you are not sure what to do with, you might need to read some Rails books - it is not a framework you can easily learn just by reading an existing code.
Upvotes: 0