I currently have a user system set up with 2 models –> The Plans Model has_many Users.
What I would like to implement is a way to allow the users to upgrade/downgrade their plans. To do this, I’ve created a POST action in the controller called “update_plan”, and when the user POSTs a new Plan_id to it, than the user’s plan_id would change, and hence would be subscribed to a different plan.
When I POST to the Update-plan controller though the plan_id for the user does not change. I verified this by going to the console, and doing
user = User.find(1)
user.plan.id
When checking the plan_id the second time, there was no change.
Here’s what my form looks like for changing the plan id
<%= form_tag("/users/update_plan", :method => "post" ) do %>
<%= hidden_field_tag :plan_id, plan.id %>
<%= submit_tag("Change To Plan", :class => "signup") %>
<% end %>
And here’s the Update Plan Action, in the Users Controller
def update_plan
@user = current_user
if @user.update_attributes(params[:plan_id])
flash[:success] = "Profile updated"
sign_in @user
redirect_to change_plan_path
else
render change_plan_path
flash[:errors] = "Oops, something went wrong with the Update. Please Talk To Support"
end
end
I am not quite sure where the error is though, because I’m not too sure in the core I’ve written above.
How would you update the parameters of the user’s plan_id? Any help greatly appreciated
@user.update_attributes(params[:plan_id])won’t update the user in the way you’re expecting.update_attributesexpects a hash which contains keys that match the model’s columns names.To update a single column, try this:
Another way, is to pass
update_attributesa hash with plan_id (assumingplan_idcan be mass assigned):