gregm
March 25, 2010, 3:26am
1
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
gregm
March 25, 2010, 3:41am
2
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
=>
:pos t %>, you could check the api for details
gregm
March 25, 2010, 3:55am
3
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?
gregm
March 25, 2010, 4:24am
4
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:
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
gregm
March 25, 2010, 4:15am
5
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:
gregm
March 25, 2010, 4:28am
6
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 %>
gregm
March 25, 2010, 4:36am
7
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.
gregm
March 25, 2010, 4:42am
8
so how about using button_to?
I think this could be useful for you
gregm
March 25, 2010, 4:49am
9
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?
gregm
March 26, 2010, 7:10am
10
Colin L. wrote:
On 25 March 2010 03:36, Greg Ma [email protected] wrote:
: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.
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
There is no user input this is why i prefer using a link. The parameter
not posted are :defi_id and user_id
gregm
March 26, 2010, 10:08am
11
On 26 March 2010 06:10, Greg Ma [email protected] wrote:
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
There is no user input this is why i prefer using a link. The parameter
not posted are :defi_id and user_id
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
gregm
March 25, 2010, 9:49am
12
On 25 March 2010 03:36, Greg Ma [email protected] wrote:
: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.
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