1

I have a code in my view as

<% topics.each do |t| %>
<li><%= link_to "#{t.name}", {:controller => "pages", :action => "session_start", :topic_id => t.id} , :method => :post , :confirm => "Start a new QA session for '#{t.name}' ?"  %></li>
<% end %>

The parameter :topic_id is sent as get and appended to url, I want it to be send as a part of post body. I have tried various combinations and even read the link_to API documentation.

What am I missing on?

3
  • i think get is the default way. you can use. button_to and then do a :method => :post Apr 3, 2013 at 14:29
  • I want a link and not a button. Apr 3, 2013 at 14:31
  • you can do it with a link_to too sorry./ Apr 3, 2013 at 14:32

3 Answers 3

2

actually, you can do a rake routes to check the routes which you want to POST. your routes should have something like this:

  resources :pages do
    member do
      post   'session_start'
    end
  end

then in your link, you can just do

<%= link_to "Hello", session_start_pages_path() %>
0
2

Why not do something like:

<%= link_to "#{t.name}", pages_session_start_path(topic_id: t.id), method: :post, confirm: "Start a new QA session for '#{t.name}' ?" %>

P.S. off the top of my head I can't remember if it would be pages_session_start_path or session_start_pages_path but either way you might want to rename the URL helper in your routes.rb to something more memorable.

Make sure the route for this URL is set as POST

post "/pages/session_start", to: "pages#session_start", as: :session_start
4
  • Do you get any errors or it's just not POST? By the way I had an extra } there before by accident.
    – mind.blank
    Apr 3, 2013 at 14:42
  • If I name the route and try the code, it is the same get request again. Apr 3, 2013 at 14:42
  • Make sure your routes is set to POST
    – mind.blank
    Apr 3, 2013 at 14:46
  • does not help. Still a get request. Apr 3, 2013 at 14:47
-1

See the link_to doc, there is a url_options and html_options hash; the option method must belong to the url_options hash, but as you have written your code, it belongs to the html_options hash. Try to move all your options in a single hash (in your example, you don't need the html_options hash) :

<%= link_to "#{t.name}", {:controller => "pages", :action => "session_start", :topic_id => t.id, :method => :post , :confirm => "Start a new QA session for '#{t.name}' ?" } %>
1
  • When I do this, everything ( method, topic and confirm) becomes a parameter for a get request. Apr 3, 2013 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.