2013-09-08 2 views
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"> 

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

+0

Я не знаю, нужен ли пользователь user_type, но нет, вы можете использовать это: '<% = current_user.user_type.type_of_user, если current_user.user_type.present? %> ' – overallduka

+0

Это не решение, проблема в том, что я не могу обновить значение! так что, если я попытаюсь обновить любого пользователя, я потеряю его! потому что обновление происходит неправильно. –

+0

вы отвечаете, пропуская мою проблему, а не ее решение –

ответ

1

решена с помощью этого:

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

Вместо этого:

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