Call a controller action from a different view

Hi, I am new in RoR and i am lost in a simple action. In my home page i am trying to call a create action from a different controller. Here is how i do it:

<%= link_to "Join!", :controller => "participants",:action => "create",:user_id => 2,:defi_id => item.id%>

What am i doing wrong? It should be a post action, how do i specify that?

Thank you for your help Greg

Do you want to create a Participant?

I think you should use POST when you want to create a record in database. Normal a link always a GET request, so maybe you could

create a form or use <%= link_to “Join!”, participants_path, :method => :post %>, you could check the api for details

yong gu wrote: <%= link_to "Join!", participants_path, :method => :post %>

I think i should try this as i want to insert a new participant

<%= link_to "Participer!", new_participant_path, :method => :post %>

But this only redirects me to the page, how do i add the parameters to actually insert data?

When you click “Join!”, you want to create a participant and then redirect to somewhere, is that right? If so, you should post your data to the controller, by using <%= link_to “Participer!”, participants_path, :method => :post, :group_id => @group.id %>. when you click the link, the ‘create’ method in the according controller will be executed.

or you could create a form yourself, just put your parameters in the hidden input, as:

yong gu wrote:

When you click "Join!", you want to create a participant and then redirect to somewhere, is that right?

If so, you should post your data to the controller, by using <%= link_to "Participer!", participants_path, :method => :post,

*:group_id => @group.id* %>. when you click the link, the 'create' method in the according controller will be executed.

or you could create a form yourself, just put your parameters in the hidden input, as:

  <form action="/participants">

   <input type="hidden" name="paramter" value="value" />

   <input type="submit" name="submit" value="Join"/>

  </form>

Yes i know i could use a form but i prefer to use a link_to. Is it better use a from instead?

I did this: <%= link_to "Participer!", new_participant_path, :method => :post,:defi_id => item.id,:user_id => 2 %>

but it still doesn't create the data into de db, it redirects me to the new template.

Greg

i think it should be <%= link_to “Participer!”, participants_path, :method =>:post, :defi_id => item.id, :user_id => 2 %>

not

<%= link_to “Participer!”, new_participant_path, :method =>:post, :defi_id => item.id, :user_id => 2 %>

Yong Gu wrote:

i think it should be

<%= link_to "Participer!", *participants_path*, :method =>:post, :defi_id => item.id, :user_id => 2 %>

not

<%= link_to "Participer!", *new_participant_path*, :method =>:post, :defi_id => item.id, :user_id => 2 %>

Thanks we are on the good track. But there is a last problem, the parameters arent posted. I've checked with firebug.

so how about using button_to?

I think this could be useful for you

http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#M001597

Yong Gu wrote:

so how about using button_to?

I think this could be useful for you

ActionView::Helpers::UrlHelper

I've replaced link_to by button_to and it does the same thing. Do i HAVE TO use a form_for?

What parameters aren't posted? If you want additional parameters in the link you will need to include them in the link_to. Have a look at the generated html for the link to check that what you want is there.

Where is the data coming from? If it is user input why not use a form with a submit button?

Colin

Colin Law wrote:

What does the generated html for the link look like?

What do you see in the log file when you click the link? It should show the params.

Colin