Reputation: 43
I am trying to implement a "Settings" page to my website. In this page, the user can change some basic settings, such as their email and subscription to emails. Right now, I am just trying to get them to be able to unsubscribe from emails. When first signing up, they have the option of checking a checkbox, and the value is stored in a mysql database. Now, I am having difficulty figuring out how to change it from a different page. Here is what the controller for signing up looks like:
class SignUpsController < ApplicationController
def index
end
def new
@user = SignUp.new
end
def cost
return 10
end
def create
@user = SignUp.new(sign_ups_params)
if @user.save!
UserMailer.welcome_email(@user).deliver
flash[:notice] = "Thank you " + @user.first_name + ", account created successfully."
redirect_to(:controller => 'home')
else
flash[:error] = "something failed"
redirect_to :back
end
rescue
flash[:error] = "Failed to create user, email is taken or passwords do not match"
redirect_to :back
end
private
def sign_ups_params
params.require(:sign_ups).permit(:first_name, :last_name, :email, :password, :password_confirmation, :subscription)
end
end
Here is a portion of the view for the sign up page to show how I went about filling out and submitting the form:
<h3>Create New User</h3>
<%= form_for(:sign_ups, :url => {:action => 'create'}) do |f| %>
<table summary="Subject form fields">
<tr>
<th>First Name</th>
<td><%= f.text_field(:first_name) %></td>
</tr>
<tr>
<th>Last Name</th>
<td><%= f.text_field(:last_name) %></td>
</tr>
<tr>
<th>Email</th>
<td><%= f.email_field(:email) %></td>
</tr>
<tr>
<th>Password</th>
<td><%= f.password_field(:password) %></td>
</tr>
<tr>
<th>Password Conformation</th>
<td><%= f.password_field(:password_confirmation) %></td>
</tr>
<tr>
<th>Subscribe to email</th>
<td><%= f.check_box(:subscription) %></td>
</tr>
</table>
<input class="btn btn-primary" name="commit" type="submit" value="Create User"/>
</div>
I have made a "Profile" view and controller for the settings page, but am not sure if that is the proper way of doing it, or if I should just use the sign ups controller. Thank you in advance!
Upvotes: 0
Views: 123
Reputation: 3714
You can and should use your signups_controller since you want to give the user the ability to edit his SignUp record (or registration).
You only need to add a few things to make this work. first, create an edit
and an update
action in your controller:
def edit
@user = SignUp.find(params[:id])
end
def update
@user = SignUp.find(params[:id])
respond_to do |format|
if @user.update(sign_up_params)
# do this
else
# do that
end
end
end
Also rename your sign_ups_params
to its singular form => sign_up_params
And your corresponding views
views/sign_ups/edit.html.erb
<%= render 'form' %>
views/sign_ups/_form.html.erb
<%= form_for(@user, :url => {:action => 'update'}) do |f| %>
....
since you probably have resources :sign_ups
in your config/routes.rb, you should be good to go.
Seperating the SignUp
from your Profile
probably makes life harder than it has to be, except if you use a gem like devise (which does that) in a very clean way.
Which object are you trying to update, or which object holds the attributes you want to update?
If you want to update sign_up_params
, do so in your SignUpsController
and also add your update
action as the error you get suggests.
However, if you need to update profile_params, do that in your ProfilesController
.
Probably you'll see the confusion now. Consider yourself looking into someone elses code, seeing a ProfilesController
as well as a SignUpsController
create
action.
Why not just having a ProfilesController where the sign_up is the create action for a Profile object?
In case you keep this in your SignUpsController
move edit
as well as update
there.
Upvotes: 1