0

我无法使用单选按钮创建/更新值。undefined method 'type_of_user' for nil:NilClass当我单击更新按钮并且在我的数据库浏览器中打开用户模型时,它给了我我发现user_type_id is null

错误在这里第 36 行

 <a class="btn btn-primary" href="#">
34:               <i class="icon-user icon-white"></i>
35:               <% if user_signed_in? %><%= current_user.user_name %>
36:               (<%= current_user.user_type.type_of_user %>)
37:               <% end %></a>
38:             <a class="btn btn-primary dropdown-toggle" data-toggle="dropdown" href="#"><span class="caret"></span></a>
39:             <ul class="dropdown-menu">

用户.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :encryptable, :validatable,:confirmable and :omniauthable
  devise  :database_authenticatable, :registerable, :recoverable, :rememberable,
          :trackable, :lockable, :timeoutable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :user_name, :first_name, :last_name, :password, :password_confirmation, :remember_me,
                  :role_ids, :current_password, :user_type_id
  attr_accessor :current_password
  # attr_accessible :title, :body

  has_many :assignments
  has_many :roles, :through => :assignments
  has_many :articles
  has_many :comments
  has_many :students
  has_many :guardians
  has_many :tickets
  has_many :permissions
  belongs_to :user_type
  accepts_nested_attributes_for :tickets



  def has_role?(role_sym)
    roles.any? { |r| r.role_name.underscore.to_sym == role_sym }
  end
end

用户类型.rb

class UserType < ActiveRecord::Base
  attr_accessible :type_of_user
  has_many :users

  def to_label
   type_of_user.to_s
  end
end

users_controller.rb

class UsersController < ApplicationController
  def index
    @users = User.all
    @users_grid = initialize_grid(User,
                                  :include => [:assignments, :roles])
  end

  def show
    @user = User.find(params[:id])

  end

  def edit
    @user = User.find(params[:id])
  end

  def update
    @user = User.find(params[:id])
    if @user.update_attributes(params[:user])
      sign_in @user
      flash[:success] = 'Profile updated'
      redirect_to users_path
    else
      render 'edit'
    end
  end

  def destroy
    @user = User.find(params[:id])
    if current_user == (@user)
      flash[:error] = "Admin suicide warning: Can't delete yourself."
    else
      @user.destroy
      flash[:success] = 'User deleted'
      redirect_to users_path
    end
  end
end

编辑.html.erb

<%= render 'shared/navbar' %>
<h4>Edit user</h4>
<h5>[*] is a required fields</h5>
<div class="offset3">
<%= simple_form_for(@user , html: { class: 'form-horizontal' }) do |f| %>
    <%= f.error_notification %>

    <div class="inputs">
      <%= f.input :email, disabled: true %>
      <%= f.input :first_name , disabled: true %>
      <%= f.input :last_name , disabled: true %>
      <%= f.input :user_name , required: true ,autofocus: true %>
      <%= f.input :password, :autocomplete => "off",
       :hint => "leave it blank if you don't want to change it", :required => false %>
      <%= f.input :password_confirmation, :required => false %>
      <%= f.input :current_password,
       :hint => "we need your current password to confirm your changes", :required => true %><br>
      <%= f.association :user_type, as: :radio_buttons, value_method: :type_of_user, label: 'Role' %>
    </div><br>

    <div class="offset1 actions">
      <button class="btn btn-success" type="submit">Update</button>
    </div>

new.html.erb 也一样

4

1 回答 1

1

通过使用此解决:

<%= f.association :user_type, as: :radio, label: 'Role' %>

而不是这个:

<%= f.association :user_type, as: :radio_buttons, value_method: :type_of_user, label: 'Role' %>
于 2013-09-08T18:05:13.560 回答