更改 Rails 3 应用程序中的布尔值会产生 ArgumentError
在我的 Rails 3.0.5 应用程序中,我的 Profile 模型上有一些列以保护隐私。在每个用户的设置中,我希望用户能够更改其设置,以便他们可以确定显示其个人资料的哪些部分。在我的迁移中,我为此添加了四个布尔属性。但是,当我尝试更新值(在 true/false 之间切换)时,我收到一个 ArgumentError:
ArgumentError (wrong number of arguments (1 for 2)):
app/controllers/profiles_controller.rb:162:in `edit_settings'
An example of the params Passed:
{"utf8"=>"✓",
"authenticity_token"=>"0uySkgRNsIIQSX6PtXl3e0e+MXCTo4yuoU/QjuBxENw=",
"show_hometown"=>"1"}
Here is the boolean in my migration:
t.boolean :show_hometown, :default => true
Here is the controller action edit_settings
I'我用来更新。 update_attribute 有四个 if
语句,但就长度而言,我只包含两个:
def edit_settings
@profile = current_user.profile
if @profile.update_attribute(:show_hometown)
redirect_to settings_path, :notice => 'Updated user information successfully.'
else
redirect_to settings_path, :notice => 'Oops, something went wrong. Please try again.'
end
...
if @profile.update_attribute(:show_current_location)
redirect_to settings_path, :notice => 'Updated user information successfully.'
else
redirect_to settings_path, :notice => 'Oops, something went wrong. Please try again.'
end
end
以及更新 :show_hometown
的表单:
<%= form_tag({:action => "edit_settings", :controller => "profiles"}, :html => {:multipart => true }) do %>
<%= check_box_tag :show_hometown %>
<%= @user.profile.hometown %>
<% end %>
FOLLOW-UP:
如果我在 :show_hometown 上使用验证并将以下内容插入到我的控制器中,我可以看到值切换:
def edit_settings
@profile = current_user.profile
if @profile.show_hometown == true
if @profile.update_attributes(:show_hometown => false)
redirect_to settings_path
else
redirect_to settings_path, :notice => 'Oops, something went wrong. Please try again.'
end
elsif @profile.show_hometown == false
if @profile.update_attributes(:show_hometown => true)
redirect_to settings_path
else
redirect_to settings_path, :notice => 'Oops, something went wrong. Please try again.'
end
end
end
但是,由于我有四个布尔值,我将在其中应用相同的代码,是否有一种方法可以在一个控制器操作中处理每个更新?或者我是否需要为每个要更新的布尔属性创建一个单独的操作?
In my Rails 3.0.5 app I have columns on my Profile model for privacy. In each User's settings, I want the user to be able to change their settings so they can determine which parts of their profile are shown. In my migration I added four boolean
attributes for this. However when I try to update the value (switch between true/false) I get an ArgumentError:
ArgumentError (wrong number of arguments (1 for 2)):
app/controllers/profiles_controller.rb:162:in `edit_settings'
An example of the params passed:
{"utf8"=>"✓",
"authenticity_token"=>"0uySkgRNsIIQSX6PtXl3e0e+MXCTo4yuoU/QjuBxENw=",
"show_hometown"=>"1"}
Here is the boolean in my migration:
t.boolean :show_hometown, :default => true
Here is the controller action edit_settings
I'm using to update. There are four if
statements to update_attribute, but for length I'm only including two:
def edit_settings
@profile = current_user.profile
if @profile.update_attribute(:show_hometown)
redirect_to settings_path, :notice => 'Updated user information successfully.'
else
redirect_to settings_path, :notice => 'Oops, something went wrong. Please try again.'
end
...
if @profile.update_attribute(:show_current_location)
redirect_to settings_path, :notice => 'Updated user information successfully.'
else
redirect_to settings_path, :notice => 'Oops, something went wrong. Please try again.'
end
end
And the form for updating :show_hometown
:
<%= form_tag({:action => "edit_settings", :controller => "profiles"}, :html => {:multipart => true }) do %>
<%= check_box_tag :show_hometown %>
<%= @user.profile.hometown %>
<% end %>
FOLLOW-UP:
If I use validations on :show_hometown and insert the following into my controller I can see the value toggle:
def edit_settings
@profile = current_user.profile
if @profile.show_hometown == true
if @profile.update_attributes(:show_hometown => false)
redirect_to settings_path
else
redirect_to settings_path, :notice => 'Oops, something went wrong. Please try again.'
end
elsif @profile.show_hometown == false
if @profile.update_attributes(:show_hometown => true)
redirect_to settings_path
else
redirect_to settings_path, :notice => 'Oops, something went wrong. Please try again.'
end
end
end
However, since I have four booleans where I'll apply the same code is there a way to handle each update in one controller action? Or do I need to create a separate action for each boolean attribute I want to update?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你必须告诉
update_attribute
列和值:这意味着将
show_hometown
列的值设置为params[:show_hometown]
中的值。请注意,使用
update_attribute
会跳过验证。如果要运行验证,则必须使用update_attributes
:要回答您的后续问题:使用
update_attributes
您可以在一次调用中更新多个列:还有一个
toggle
方法,但使用该方法将导致 4 次数据库调用,而不是 1 次。可以写成一行:You have to tell
update_attribute
the column and the value:This means set the value of column
show_hometown
to the value inparams[:show_hometown]
.Note that with
update_attribute
validations are skipped. If you want validations to run, you have to useupdate_attributes
:To answer your follow-up: with
update_attributes
you can update several columns in one call:There is also a
toggle
method, but using that would result in 4 database calls instead of 1. It can be written in one line: