2

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?

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

You are missing one closing tag at the end of your if - else statement:

    else
      false
    end
end

1 Comment

Ah, my apologies, the end closing tag is in my code, I just missed it when copying and pasting to this question. Thank you anyway!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.