Rails checkbox not working - no error when submitting form

I have a checkbox problem in Rails:

I have two User and authorized_users models 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 to check a checkbox for cheking if the Authorized_user should receive an email (true) or not (false):

<%= check_box(:authorized_user, :sendEmail, options = {:checked => true}, checked_value =  true, unchecked_value = false) %> 

      

The same exact code works fine in the "new" Authorized_user view when creating a new user, but when I edit it, with the "edit" user view, no error is displayed when the form is displayed, but the logical cell in the database is not affected.

What do I need to change so that the changes are saved when the form is submitted?

Thank you in advance.

Pd: for more information, I can tell that other data changes without issue in this "edit" view from the user, for exmaple:

<%= f.text_field :phone %>

      

Error log after change:

<% 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 realized:

We have to use: useraccount as a linker action, not: authorized_user:

<%= check_box(:useraccount, :sendEmail, options = {:checked => true}, checked_value =  true, unchecked_value = false) %> 

      

+3


source to share


1 answer


You have this problem because you are not using the actual object when rendering this checkbox. It should probably be:

<%= f.check_box :send_email %>

      



Note that I used the name send_email

in the Rails convention, which you must accept.

+3


source







All Articles