[英]Rails 4 Create form to update only one attribute
I have a user model, but I need to create a form to update only one attribute. 我有一个用户模型,但是我需要创建一个表单来仅更新一个属性。
EDIT: I added the create_mailbox
patch in :users
resource but the form throws a undefined method
settings_create_mailbox_path'` error. 编辑:我在
:users
资源中添加了create_mailbox
补丁,但该表单引发了undefined method
settings_create_mailbox_path'`错误。 Can anyone give me some insight to how this member patch/resource routes work? 谁能给我一些关于这个成员补丁/资源路由如何工作的见解?
Here's the form: 形式如下:
<%= form_for @user, url: settings_create_mailbox_path(@user) do |f| %>
<%= f.text_field :rss_mailbox, autocomplete: 'off'%>
<button type="submit" class="button">Create Mailbox</button>
<% end %>
Here's the Users route: 这是用户路线:
resources :users, id: /.*/ do
member do
patch :settings_update, controller: :settings
patch :create_mailbox, controller: :settings
patch :view_settings_update, controller: :settings
patch :sharing_services_update, controller: :sharing_services
patch :actions_update, controller: :actions
end
end
And here's the settings route: 这是设置路线:
get :settings, to: 'settings#settings'
namespace :settings do
get :account
get :billing
get :import_export
get :feeds
get :help
post :update_credit_card
post :mark_favicon_complete
post :update_plan
post :font
post :font_increase
post :font_decrease
post :entry_width
end
It would depend on the attribute and what you're changing it to. 这将取决于属性以及您将其更改为什么。
If it's a simple toggling of a boolean field, for example, you could get away with a link that fires off an ajax request, that makes the change and returns some HTML (eg. to update the link you clicked). 例如,如果这是对布尔字段的简单切换,则可以摆脱触发ajax请求的链接,该链接进行更改并返回一些HTML(例如,更新您单击的链接)。
If it's something else, eg. 如果还有其他事情,例如。 a text field or a string or something, a
form_for @user
would be the typical way. 一个文本字段或一个字符串之类的东西,
form_for @user
是典型的方式。 But the form would only have one field, and the controller that processes the form post would have its strong parameters (or attr_accessible for Rails 3) set so that it will only accept data for that one field. 但是该表单只有一个字段,并且处理该表单发布的控制器将设置其强大的参数(对于Rails 3而言,是attr_accessible),因此它将仅接受该字段的数据。
Was using the wrong route name. 使用了错误的路线名称。 This was the right one:
这是正确的:
create_mailbox_user_path
Rails 4 Use Strong Parameters to determine which attribute is allow to update like so: Rails 4使用Strong Parameters来确定允许更新哪个属性,如下所示:
def user_param
params.require(:user).permit(:attr)
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.