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
非常感谢,那整理出来了。 – stemitchell91