Reputation: 355
Im writing an app that the user can update their plan data on a view.
My route its like this:
get '/account/plans', to: 'accounts#plans'
post '/account/change_plan', to: 'accounts#change_plan'
get '/account/change_plan', to: 'accounts#plans'
On my controller i have:
#accounts_controller.rb
def change_plan
@plan = Plan.find(params[:plan])
current_user.plan = @plan
current_user.save
render 'payment_options'
end
and in my view I have:
<div class="plan-details">
<header class= <%= plan.css %> >
<h3><%= plan.name %></h3>
<small><%= plan.visibility %></small>
<p><%= plan.card_description.html_safe %></p>
<span class="plan-price"><sup>$</sup><%= plan.amount.to_s %></span>
</header>
<article>
<%= plan.features_description.html_safe %>
<%= link_to button_text, account_change_plan_path, title: button_text, :class=>"btn btn-md btn-block btn-outline-green" %>
</article>
</div><!-- end plan details -->
But when I press this button it show me the message:
No route matches [GET] "/account/change_plan"
Why when I press the button, on the server log it says:
Processing by AccountsController#plans as HTML
and not Processing by AccountsController#change_plan as HTML
Thanks
Upvotes: 0
Views: 33
Reputation: 6321
You need to call as POST action and pass plan
params {plan: plan.id}
<%= link_to button_text,
account_change_plan_path({plan: plan.id}),
method: :post,
title: button_text,
class: "btn btn-md btn-block btn-outline-green" %>
Upvotes: 1
Reputation: 820
There are a few things going on here, it looks like.
You are using link_to for your submit button, instead of a form submit button. By default link_to makes GET requests. You need to tell it to submit a POST request instead, since that is what your route is defined as.
Change
<%= link_to button_text, account_change_plan_path, title: button_text, :class=>"btn btn-md btn-block btn-outline-green" %>
To
<%= link_to button_text, account_change_plan_path, method: :post, title: button_text, :class=>"btn btn-md btn-block btn-outline-green" %>
See https://api.rubyonrails.org/v5.2.1/classes/ActionView/Helpers/UrlHelper.html for how to use link_to.
Secondly, your route does not have a parameter for 'plan' in it, and you aren't sending one over with your link_to, so even after you fix your link_to, your action won't be able to find the plan. You should change your route to be:
post '/account/change_plan/:plan', to: 'accounts#change_plan'
And then change your link_to to be (assuming you are using plan.id as your plan identifier):
<%= link_to button_text, account_change_plan_path(plan.id), method: :post, title: button_text, :class=>"btn btn-md btn-block btn-outline-green" %>
Upvotes: 1
Reputation: 5313
You defined your route as POST
but your link is doing a GET
.
This should work:
<%= link_to button_text, account_change_plan_path(plan_id: plan.id), title: button_text, class: "btn btn-md btn-block btn-outline-green", method: :post %>
Upvotes: 1