无法使用单选按钮创建或更新值

时间:2013-09-08 13:41:49

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.2

我无法使用单选按钮创建/更新值。当我点击更新按钮时,它给了我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">

user.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

user_type.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

edit.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相同

1 个答案:

答案 0 :(得分:1)

使用此解决:

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

而不是:

<%= f.association :user_type, as: :radio_buttons, value_method: :type_of_user, label: 'Role' %>