I have a rails 3 app and I’m trying to use a button to activate a method in a controller. Meaning that in my users_controller.rb I have
def follow
...
...
end
And in the view (users/show.html.erb) I want a button to call that method.
I currently have <%= button_to("Un-Follow", :controller=>"users", :action=> "follow") %>, however when i click on the button I get the following error: No route matches [POST] "/users/5/follow" any idea why?
I’m not sure how to fix this.
EDIT: I fixed the config/routes.rb and fixed that error. However I am still getting an error on the controller. nil id.
EDIT: I was able to get everything working error free. However now when I click the button, it says no view associated. However, I want to just call that method, I do not want a new view. Would it be best to, instead of in the controller, do it in a helper?
EDIT: Lastly, for some reason, when I click the “follow” button everything works. However when I click the “unfollow” button it doesn’t work.
You don’t necessarily need a view for a controller action. You may just render arbitrary text.
Or instead of rendering a view, you may redirect to another page.