_header.html.erb

时间:2015-07-13 10:04:53

标签: ruby-on-rails layout nomethoderror

我使用layout / _header.html.erb创建导航栏/登录并注册栏以显示在我的应用的每个页面上。它工作正常,直到用户登录,然后我得到一个无方法错误(当用户登录时,我希望"登录"功能更改为"注销")。我认为这与无法在其自身的控制器之外调用方法有关,但我不知道如何解决这个问题,如果有人能指出我正确的方向我和#39;非常高兴。

错误讯息:

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

1 个答案:

答案 0 :(得分:0)

如下所示更改before_action :set_user以允许index操作

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

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

<强> OR

使用current_user代替@user,而@user不是 已初始化 index 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>