0

Trying to use link_to to send info to action in my controller.

Link looks like this:

<%= link_to "Buy", {:controller => :policies, :action => :policy_option_price_calc, :option => "policy_option_a_cost" }, {:method => :post}  %>

I have a policies controller and inside my action looks like this:

def policy_option_price_calc(params)
   #logic stuff

   render :partial => "/policies/swipe_protect/price_total"
end

and in my routes I have:

resources :policies
    member do
        post 'policy_option_price_calc'
    end
end

I don't know why but I am getting this url when clicking the link_to link:

http://localhost:3000/?action=policy_option_price_calc&controller=policies&option=policy_option_a_cost

Which of course doesn't exist, but its not even giving me an error its just being processed by my HomeControll#index

I have looked at:

Form tag helpers

rubyguild routing

Stack overflow question

Plus many more. Any help would be appreciated, thanks.

Community
  • 1
  • 1
Ryan
  • 5,644
  • 3
  • 38
  • 66

2 Answers2

0

Have you tried using the named route format?

<%= link_to 'Buy', policy_option_price_calc_policy_url(:option => 'policy_option_a_cost'), :method => :post %>

You may have to tweak the name of the route, but that's what I would do. The way you're trying to do it you would want to use strings instead of symbols:

<%= link_to 'Buy', { :controller => 'policies', :action => 'policy_option_price_calc', :option => 'policy_option_a_cost' }, { :method => :post } %>

See http://guides.rubyonrails.org/routing.html#the-query-string for more information.

Josh
  • 338
  • 3
  • 14
  • I have tried using strings, actually that was the first thing that I tired. It still doesn't work. – Ryan Sep 24 '12 at 21:39
0

If you remove the

{ :method => post }

hash I think the expected url will be created. At the moment your request is routed to the HomeController because that is what your URL is invoking.

http://localhost:3000/policies/?action=policy_option_price_calc&controller=policies&option=policy_option_a_cost

is the route you are thinking about, but the policies/ part is missing.

However this is not the solution to your problem. For a POST request you should use button_to or form helper instead of link_to.

wpp
  • 7,093
  • 4
  • 33
  • 65