Reputation: 129
Really new to using Ruby on Rails and programming in gentle so apologies in advance if this seems very basic.
I'm working on a very simple wiki based website, where users can upgrade and downgrade their account.
In my downgrade.html.erb
I have the following code:
<p>Are you sure you want to downgrade back to standard?<p>
<%= link_to "Yes", :controller => :charges, :action => :downgrade1, class: 'btn btn-danger' %>
<%= link_to "No", root_url, class: 'btn btn-success' %>
and in my charges_controller.rb
I have my downgrade1 method:
def downgrade1
if current_user.premium?
current_user.update_attribute(:role, 'standard')
flash[:success] = "You have been downgraded to standard."
redirect_to root_url
else
false
end
end
Ultimately, when the user clicks that 'Yes' button, I want that downgrade1
method to run and the user's account to be downgraded back to standard.
However, what happens is the website loads a 'show webpage' with my header and footer, but the user is still a premium user.
Any ideas how I can fix this?
Upvotes: 2
Views: 4038
Reputation: 129
Thanks for your attempts to solve my problem, both your answers eventually led to me finding the solution.
In the end, I used this code in my view:
<%= button_to "Yes", { :controller => "charges", :action => "downgrade1"}, class: 'btn btn-danger' %>
and added this to my routes.rb
post "charges/downgrade1" => "charges#downgrade1"
Downgrading now works as planned.
Thanks again for your help.
Upvotes: 2
Reputation: 1627
I think the problem here is that you didn't specify the HTTP method for your link_to
, like
<%= link_to "Yes", :controller => :charges, :action => :downgrade1, class: 'btn btn-danger', :method => :patch %>
By default, it will call a GET method, then nothing in your database will be changed.
And make sure you have defined that method in your routes.rb
Upvotes: 1
Reputation:
You are missing one closing tag at the end of your if - else statement:
else
false
end
end
Upvotes: 0