anexo
anexo

Reputation: 505

Rails Checkbox not working - no error when submitting form

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) %> 

Upvotes: 4

Views: 3519

Answers (1)

Marek Lipka
Marek Lipka

Reputation: 51151

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.

Upvotes: 3

Related Questions