提交表单时没有错误 - Rails的复选框不工作 [英] Rails Checkbox not working - no error when submitting form
问题描述
我在Rails的复选框问题:
我有两个型号,用户和authorized_users,具有下列关联关系:
类AuthorizedUser< ActiveRecord的::基地
HAS_ONE:用户,如= GT; :用户帐号
和
类用户< ActiveRecord的::基地
belongs_to的:useraccount,多态=>真的,:依赖=> :破坏
在用户的编辑的观点我想一个复选框cheking如果Authorized_user应该收到一封电子邮件(true)还是没有(假):
<%= check_box(:authorized_user,:sendEmail,选择= {:检查=>真},checked_value = TRUE,unchecked_value = FALSE)%GT;
完全相同code在Authorized_user的新视图中工作完美,创建新用户时,但是当我编辑,与用户的编辑的观点,不显示任何错误submiting的形成,但在该数据库的布尔细胞不被影响。
什么我需要修改,这样,当我提交表单,保存更改?
非常感谢你在前进。
PD:欲了解更多信息,我可以说,其他的数据被修改,没有问题,从用户的这种编辑的观点,对于〔实施例:
<%= f.text_field:电话%GT;
错误日志转换后:
<%f.check_box:sendEmail%GT;
由@马立克-Lipka的
建议 NoMethodError在用户编辑#显示应用程序/视图/用户/ edit.html.erb,其中线#64提出:未定义的方法`sendEmail为#<使用者:0xb5dbd96c>提取的源(左右线#64):63:< P>¿Desea recibir电子邮件/ P>
64:其中p为H.;&下;%= f.check_box:sendEmail%GT;&下; / P>
65:<%结束%GT;
66:< BR />
与@马立克-Lipka的长时间的讨论后,我们得到了线索:
我们必须使用:useraccount作为连接动作,而不是:authorized_user:
<%= check_box(:useraccount,:sendEmail,选择= {:检查=>真},checked_value = TRUE,unchecked_value = FALSE)%GT;
由于在渲染时,此复选框不使用实际的对象,您有这个问题。这大概应该是:
<%= f.check_box:SEND_EMAIL%GT;
请注意,我在Rails的约定,你应该采取使用 SEND_EMAIL
名称。
I have a problem with a checkbox in Rails:
I have two models, User and authorized_users, with the following association:
class AuthorizedUser < ActiveRecord::Base
has_one :user, as => :useraccount
and:
class User < ActiveRecord::Base
belongs_to :useraccount, :polymorphic => true, :dependant => :destroy
In the "edit" view of the User I want a checkbox for cheking if the Authorized_user should recieve an email (true) or not (false):
<%= check_box(:authorized_user, :sendEmail, options = {:checked => true}, checked_value = true, unchecked_value = false) %>
The same exact code is working perfect in the "new" view of the Authorized_user, when creating a new user, but when I edit them, with the "edit" view of user, no error is displayed submiting the form, but the boolean cell in the database is not being affected.
What do I need to modify so that when I submit the form, the changes are saved?
Thank you very much in advance.
Pd: For more information I can say that other data is being modified with no problem in this "edit" view from user, for exmaple:
<%= f.text_field :phone %>
Error log after changing:
<% f.check_box :sendEmail %>
suggested by @marek-lipka
NoMethodError in Users#edit
Showing app/views/users/edit.html.erb where line #64 raised:
undefined method `sendEmail' for #<User:0xb5dbd96c>
Extracted source (around line #64):
63: <p>¿Desea recibir e-mails?/p>
64: <p><%= f.check_box :sendEmail %></p>
65: <%end%>
66: <br />
After a long discussion with @marek-lipka we got the clue:
We must use :useraccount as the linker action and not :authorized_user:
<%= check_box(:useraccount, :sendEmail, options = {:checked => true}, checked_value = true, unchecked_value = false) %>
You have this problem because you don't use actual object when rendering this check box. It should probably be:
<%= f.check_box :send_email %>
Note that I used send_email
name in Rails convention, which you should adopt.
这篇关于提交表单时没有错误 - Rails的复选框不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!