定义@shops 变量 NoMethodError 的问题
Problems with defining @shops variable NoMethodError
我创建了两个用户模型,名为 "user" 和 "shop"。当我尝试将@shop 变量用作商店时(如在shop.name 中),它将不起作用,但user.name 会起作用。所以我在某个时候错过了定义商店的时间,无法弄清楚在哪里修复它。
这是我得到的错误:
NoMethodError (undefined method `name' for nil:NilClass):
app/views/shops/index.html.erb:7:in `block in _app_views_shops_index_html_erb__1785913569146145211_70081842378680'
app/views/shops/index.html.erb:5:in `_app_views_shops_index_html_erb__1785913569146145211_70081842378680'
此用户索引页有效:
user/index.html.erb
<% provide(:title, 'All users') %>
<h1>All users</h1>
<ul class="users">
<% @users.each do |user| %>
<li>
<%= link_to user.name, user %>
</li>
<% end %>
</ul>
但是这个商店没有 b/c of shop.name
:
shop/index.html.erb
<% provide(:title, 'All shops') %>
<h1>All shops</h1>
<ul class="shops">
<% @shops.each do |shop| %>
<li>
<%= link_to shop.name, shop %>
</li>
<% end %>
</ul>
这是商店管理员:shops_controller.rb
class ShopsController < ApplicationController
#add before action for index
before_action :correct_shop, only: [:edit, :update]
def index
@shops = Shop.all
end
def show
@shop = Shop.find(params[:id])
end
def new
@shop = Shop.new
end
def create
@shop = Shop.new(shop_params)
if @shop.save
shop_log_in @shop
flash[:success] = "Thank you for signing up, welcome to AccessOBD!"
redirect_to shop_home_path
else
render 'new'
end
end
def edit
@shop = Shop.find(params[:id])
end
def update
@shop = Shop.find(params[:id])
if @shop.update_attributes(shop_params)
flash[:success] = "Profile updated"
redirect_to @shop
else
render 'edit'
end
end
private
def shop_params
params.require(:shop).permit(:name, :address, :city, :state, :zip, :email, :phone, :password,
:password_confirmation, :picture)
end
def correct_shop
@shop = Shop.find(params[:id])
redirect_to(root_url) unless current_shop?(@shop)
end
end
这是商店的会话助手:ShopSessionsHelper.rb
module ShopSessionsHelper
# Logs in the given shop.
def shop_log_in(shop)
session[:shop_id] = shop.id
end
# Remembers a shop in a persistent session.
def shop_remember(shop)
shop.remember
cookies.permanent.signed[:shop_id] = shop.id
cookies.permanent[:remember_token] = shop.remember_token
end
def current_shop?(shop)
shop == current_shop
end
# Returns the shop corresponding to the remember token cookie.
def current_shop
if (shop_id = session[:shop_id])
@current_shop ||= Shop.find_by(id: shop_id)
elsif (shop_id = cookies.signed[:shop_id])
shop = Shop.find_by(id: shop_id)
if shop && shop.authenticated?(cookies[:remember_token])
shop_log_in shop
@current_shop = shop
end
end
end
# Returns true if the shop is logged in, false otherwise.
def shop_logged_in?
!current_shop.nil?
end
def shop_forget(shop)
shop.forget
cookies.delete(:shop_id)
cookies.delete(:remember_token)
end
# Logs out the current shop.
def shop_logout
shop_forget(current_shop)
session.delete(:shop_id)
@current_shop = nil
end
# Redirects to stored location (or to the default).
def shop_redirect_back_or(default)
redirect_to(shop)
session.delete(:forwarding_url)
end
# Stores the URL trying to be accessed.
def store_location
session[:forwarding_url] = request.url if request.get?
end
end
index.html.erb 前
<% @shops.each do |shop| %>
<li>
<div class= "shop-name pull-left">
<%= link_to shop.name, shop %>
<% if current_shop.admin? && !current_shop?(shop) %>
| <%= link_to "(Delete Shop)", shop, method: :delete,
data: { confirm: "You sure?" } %>
<% end %>
</div>
<div class= "shop-address pull-right">
<p><%= shop.address %> <br> <%= shop.city %>, <%= shop.state %> <%= shop.zip %> <br> <%= shop.phone %></p>
</div>
</li>
<% end %>
你的一些记录是nil,要识别它们运行下面的代码,你会得到那些记录的索引值
<% @shops.each_with_index do |shop, index| %>
<li>
<% unless shop.blank? %>
<%= link_to shop.name, shop %>
<% else %>
<%= "Record nil at " + index.to_s %>
<% end %>
</li>
<% end %>
我创建了两个用户模型,名为 "user" 和 "shop"。当我尝试将@shop 变量用作商店时(如在shop.name 中),它将不起作用,但user.name 会起作用。所以我在某个时候错过了定义商店的时间,无法弄清楚在哪里修复它。
这是我得到的错误:
NoMethodError (undefined method `name' for nil:NilClass):
app/views/shops/index.html.erb:7:in `block in _app_views_shops_index_html_erb__1785913569146145211_70081842378680'
app/views/shops/index.html.erb:5:in `_app_views_shops_index_html_erb__1785913569146145211_70081842378680'
此用户索引页有效: user/index.html.erb
<% provide(:title, 'All users') %>
<h1>All users</h1>
<ul class="users">
<% @users.each do |user| %>
<li>
<%= link_to user.name, user %>
</li>
<% end %>
</ul>
但是这个商店没有 b/c of shop.name
:
shop/index.html.erb
<% provide(:title, 'All shops') %>
<h1>All shops</h1>
<ul class="shops">
<% @shops.each do |shop| %>
<li>
<%= link_to shop.name, shop %>
</li>
<% end %>
</ul>
这是商店管理员:shops_controller.rb
class ShopsController < ApplicationController
#add before action for index
before_action :correct_shop, only: [:edit, :update]
def index
@shops = Shop.all
end
def show
@shop = Shop.find(params[:id])
end
def new
@shop = Shop.new
end
def create
@shop = Shop.new(shop_params)
if @shop.save
shop_log_in @shop
flash[:success] = "Thank you for signing up, welcome to AccessOBD!"
redirect_to shop_home_path
else
render 'new'
end
end
def edit
@shop = Shop.find(params[:id])
end
def update
@shop = Shop.find(params[:id])
if @shop.update_attributes(shop_params)
flash[:success] = "Profile updated"
redirect_to @shop
else
render 'edit'
end
end
private
def shop_params
params.require(:shop).permit(:name, :address, :city, :state, :zip, :email, :phone, :password,
:password_confirmation, :picture)
end
def correct_shop
@shop = Shop.find(params[:id])
redirect_to(root_url) unless current_shop?(@shop)
end
end
这是商店的会话助手:ShopSessionsHelper.rb
module ShopSessionsHelper
# Logs in the given shop.
def shop_log_in(shop)
session[:shop_id] = shop.id
end
# Remembers a shop in a persistent session.
def shop_remember(shop)
shop.remember
cookies.permanent.signed[:shop_id] = shop.id
cookies.permanent[:remember_token] = shop.remember_token
end
def current_shop?(shop)
shop == current_shop
end
# Returns the shop corresponding to the remember token cookie.
def current_shop
if (shop_id = session[:shop_id])
@current_shop ||= Shop.find_by(id: shop_id)
elsif (shop_id = cookies.signed[:shop_id])
shop = Shop.find_by(id: shop_id)
if shop && shop.authenticated?(cookies[:remember_token])
shop_log_in shop
@current_shop = shop
end
end
end
# Returns true if the shop is logged in, false otherwise.
def shop_logged_in?
!current_shop.nil?
end
def shop_forget(shop)
shop.forget
cookies.delete(:shop_id)
cookies.delete(:remember_token)
end
# Logs out the current shop.
def shop_logout
shop_forget(current_shop)
session.delete(:shop_id)
@current_shop = nil
end
# Redirects to stored location (or to the default).
def shop_redirect_back_or(default)
redirect_to(shop)
session.delete(:forwarding_url)
end
# Stores the URL trying to be accessed.
def store_location
session[:forwarding_url] = request.url if request.get?
end
end
index.html.erb 前
<% @shops.each do |shop| %>
<li>
<div class= "shop-name pull-left">
<%= link_to shop.name, shop %>
<% if current_shop.admin? && !current_shop?(shop) %>
| <%= link_to "(Delete Shop)", shop, method: :delete,
data: { confirm: "You sure?" } %>
<% end %>
</div>
<div class= "shop-address pull-right">
<p><%= shop.address %> <br> <%= shop.city %>, <%= shop.state %> <%= shop.zip %> <br> <%= shop.phone %></p>
</div>
</li>
<% end %>
你的一些记录是nil,要识别它们运行下面的代码,你会得到那些记录的索引值
<% @shops.each_with_index do |shop, index| %>
<li>
<% unless shop.blank? %>
<%= link_to shop.name, shop %>
<% else %>
<%= "Record nil at " + index.to_s %>
<% end %>
</li>
<% end %>