4

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) %> 
2
  • 1
    You should adopt Ruby/Rails naming conventions. Commented Oct 27, 2014 at 13:23
  • 1
    What is check_box? Did you mean to use check_box_tag or f.check_box? Commented Oct 27, 2014 at 13:25

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

10 Comments

Hi @marek-lipka, Thanks for your help. I'm quite new to rails and i did "inherit" a project where I need to start making some changes... When I put: <%= f.check_box :send_email %> I have teh following error: undefined method 'sendEmail' I suppose i must tell checkbox that sendEmail belongs to AuthorizedUser action somehow. Trying to figuring out how... I will take good note about the naming conventions.
@anexo not exactly. The question here is if this sendMail is a field in authorized_users table because these fields are bound to ActiveRecord object, not controller actions.
Yes it is a Field inside authorized_users table with Type: tinyint(1)
@anexo so it should work. If it doesn't, please post full error message with backtrace.
Here is the error log, hope it helps, thanks a lot for the effort: 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 qué este usuario autorizado reciba e-mails de notificaciónes?</p> 64: <p><%= f.check_box :sendEmail %></p> 65: <%end%> 66: <br />
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.