2015-07-13 38 views
1

我使用layout/_header.html.erb創建導航欄/登錄並註冊欄以顯示在我的應用程序的每個頁面上。它工作正常,直到用戶登錄,然後我得到一個無方法錯誤(當用戶登錄時,我想「登錄」功能更改爲「註銷」)。我相信這是與不能在自己的控制器之外調用某種方法有關的,但我不知道如何去解決這個問題,如果任何人都可以指出我正確的方向,我會很高興。Rails在_header.html.erb中未定義的方法

錯誤消息:

NoMethodError in Users#index 

Showing /home/stephen/ruby/friendlyaccess/app/views/layouts/_header.html.erb where line #14 raised: 

undefined method `user_type' for nil:NilClass 

Extracted source (around line #14): 


<div id="usernav"> 
<ul> 
    <% if user_signed_in? && @user.user_type == 'client' %> 
    <li><%= link_to "Edit Details", edit_user_registration_path %></li> 
    <li><%= link_to "Sign Out", destroy_user_session_path, :method => :delete %></li> 
    <% else if user_signed_in? && @user.user_type == 'company' %> 

_header.html.erb:

<div id="navbar"> 
<table> 
<tr> 
    <th><%= link_to "Home", root_path %></th> 
    <th><%= link_to "Service Index", users_path %></th> 
    <th><%= link_to "About Friendly Access" %></th> 
    <th><%= link_to "Contact Us" %></th> 
</tr> 

<div id="usernav"> 
<ul> 
    <% if user_signed_in? && @user.user_type == 'client' %> 
    <li><%= link_to "Edit Details", edit_user_registration_path %></li> 
    <li><%= link_to "Sign Out", destroy_user_session_path, :method => :delete %></li> 
    <% else if user_signed_in? && @user.user_type == 'company' %> 
    <li><%= link_to "Edit Details", edit_user_registration_path %></li> 
    <li><%= link_to "View Profile", show_user_path %></li> 
    <li><%= link_to "Sign Out", destroy_user_session_path, :method => :delete %></li> 
    <% else %> 
    <li><%= link_to "Sign Up", new_user_registration_path, class: "active" %></li> 
    <li><%= link_to "Sign In", new_user_session_path, class: "active" %></li> 
    <% end %> 
<% end %> 
</ul> 
</div> 

users_controller.rb:

class UsersController < ApplicationController 
    before_action :set_user, only: [:show, :edit, :update, :destroy] 
    before_action :authenticate_user!, except: [:index, :show] 

    def index 
if params[:search] 
    @users = User.search(params[:search].capitalize).order("company_name ASC") 
elsif params[:company_type] 
    @users = User.search(params[:company_type]).order("company_name ASC") 
elsif params[:letter] 
    @users = User.search(params[:letter]).order("company_name ASC") 
else 
    @users = User.all.order("company_name ASC") 
end 
    end 

    def show 
    end 

private 

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

    def user_params 
    params.require(:user).permit(:company_name, :company_type, :photo) 
    end 

end 

user.rb:

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

    mount_uploader :photo, PhotoUploader 

    before_save :capitalize_attributes 

    def self.search(query) 
    where("company_name like ? OR company_type like ?", "%#{query}%", "%#{query}%") 
    end 

    def capitalize_attributes 
capitalizable = ["first_name","last_name", "city", "company_name"] 
self.attributes.each do |attr,val| 
    #based on comment either of these will work 
    #if you want to store nil in the DB then 
    self.send("#{attr}=",val.strip.capitalize) if capitalizable.include?(attr) && !val.nil? 
    #if you want to store a blank string in the DB then 
    self.send("#{attr}=",val.to_s.strip.capitalize) if capitalizable.include?(attr) 
    end 
    end 

end 

回答

0

更改before_action :set_user類似下面,讓index行動

before_action :set_user, only: [:index, :show, :edit, :update, :destroy] 

使用set_user方法,以便@user將可用於index

OR

使用current_user代替@user@userindex action初始化

<div id="usernav"> 
<ul> 
    <% if user_signed_in? && current_user.user_type == 'client' %> 
    <li><%= link_to "Edit Details", edit_user_registration_path %></li> 
    <li><%= link_to "Sign Out", destroy_user_session_path, :method => :delete %></li> 
    <% else if user_signed_in? && current.user_type == 'company' %> 
    <li><%= link_to "Edit Details", edit_user_registration_path %></li> 
    <li><%= link_to "View Profile", show_user_path %></li> 
    <li><%= link_to "Sign Out", destroy_user_session_path, :method => :delete %></li> 
    <% else %> 
    <li><%= link_to "Sign Up", new_user_registration_path, class: "active" %></li> 
    <li><%= link_to "Sign In", new_user_session_path, class: "active" %></li> 
    <% end %> 
<% end %> 
</ul> 
</div> 
+1

非常感謝,那整理出來了。 – stemitchell91