[英]Rails - Passing parameters from a view to a controller
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.
首次注册时,他们可以选择选中一个复选框,并且该值存储在mysql数据库中。 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!
先感谢您!
You can and should use your signups_controller since you want to give the user the ability to edit his SignUp record (or registration). 您可以并且应该使用signups_controller,因为您希望使用户能够编辑其SignUp记录(或注册)。
You only need to add a few things to make this work. 您只需添加一些内容即可完成这项工作。 first, create an
edit
and an update
action in your controller: 首先,在控制器中创建
edit
和update
操作:
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
还要将您的
sign_ups_params
重命名为其单数=> 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. 由于您可能在config / routes.rb中具有
resources :sign_ups
,因此您应该一切顺利。
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. 将
SignUp
与您的Profile
SignUp
可能会使生活变得更加艰难,除非您以非常干净的方式使用诸如devise之类的gem(这样做)。
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. 如果您想更新
sign_up_params
,在您这样做SignUpsController
也可以增加你的update
行动,你得到错误提示。 However, if you need to update profile_params, do that in your ProfilesController
. 但是,如果您需要更新profile_params,请在
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. 考虑一下自己在研究别人的代码,看到一个
ProfilesController
以及一个SignUpsController
create
动作。
Why not just having a ProfilesController where the sign_up is the create action for a Profile object? 为什么不仅仅拥有一个ProfilesController,其中sign_up是Profile对象的create动作?
In case you keep this in your SignUpsController
move edit
as well as update
there. 如果您将其保留在
SignUpsController
中,请在SignUpsController
edit
和update
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.