How do I link to the create function in my controller in Ruby on Rails?
I've got this l开发者_JAVA技巧ine in my routes.db file:
map.resources :things
I'm trying to create a link that will create a new thing. So far I've got
<%= link_to "add thing", things_path (:thingName => key)%>
But I'm getting this error message:
Unknown action
No action responded to index. Actions: create and new
How do I do the link_to line so that it links to the create method instead of the index method? Thanks for reading.
Do you want to link to the new or the create action? The new action is: <%= link_to "add thing", new_thing_path %> The create action would not make sense here, since you don't have any data to inject into the new object? Unless I'm missing something...
You probably dont want to create a resource through a link like that. Links are HTTP GET
requests, which can be cached, and search engines will follow that link, resulting in database records being created incorrectly. You should only use HTTP POST
requests to create a resource. To do that you need a form
. If you already know the data to pass, you can use hidden_field
to pass additional data
<% form_for Thing.new(:thing_name => key ) do |f| %>
<%= f.hidden_field :thing_name %>
<%= f.submit %>
<% end %>
精彩评论