views:

21

answers:

2

I've got this line 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.

+1  A: 

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...

jasonpgignac
I'm not sure if what I'm doing is correct. I am injecting some data, by doing (:thingName => key). Is this right? Thanks for your help.
ben
Ah! Well, in that case, you will be better off using a form consisting of hidden fields that contain the data you want submitted, or something similar - otherwise, you'll have to figure out how to attach the authenticity token and everything else. None of which is IMPOSSIBLE, but it might not be worth the trouble.
jasonpgignac
The mechanics of a form of hidden are in josh's post below. 100 forms on a single page is certainly going to be obnoxious to deal with - but then, so would 100 very complex link_to's. You CAN, btw, use the link_to to submit a form, you'd just need to set the method: link_to "add thing", things_path(*insert params here*), :method => :postBut, again, at that point, make sure you deal with the authenticity token, or it will just throw an error.
jasonpgignac
+1  A: 

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 %>
This sounds like a good idea, but there might be a problem because I'm actually dynamically creating these save links, based on the result of another algorithm. There could potentially be hundreds of forms, would that be a problem?
ben
If you are using a Javascript framework, you can do the equivalent http post in javascript.