Вопрос

Having problems creating radio buttons in a form using has_one

Model

Class User
has_one :role
accepts_nested_attributes_for :role

Class Role
attr_accessible :name
belongs_to :user

Controller

@user = build_role

Form for radio buttons

<div class="field">
 <% Role.offset(1).all.each do |role_fields| %>
  <%= radio_button_tag "user[role_fields_id][]", role_fields.id, @user.role_id == role_fields.id %>
  <%= role_fields.name %>
 <% end %>
</div>

Getting the error

undefined method `role_id' for #<User id: nil, name: nil, created_at: nil, updated_at: nil>

I'm sure I set up the relationship in the controller, why is this not working?

Это было полезно?

Решение

I think this line:

<%= radio_button_tag "user[role_fields_id][]", role_fields.id, @user.role_id == role_fields.id %>

Should be corrected to this:

<%= radio_button_tag "user[role_fields_id][]", role_fields.id, @user.role.id == role_fields.id %>

I hope that helps.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top