Ruby on rails 定义@shops变量nomethoderor时出现问题
我创建了两个名为“用户”和“商店”的用户模型。当我尝试将@shop变量用作shop时(如在shop.name中),它将不起作用,但user.name将起作用。因此,我在某个时候错过了定义商店的机会,无法确定在哪里修复它 下面是我得到的错误:Ruby on rails 定义@shops变量nomethoderor时出现问题,ruby-on-rails,ruby,railstutorial.org,Ruby On Rails,Ruby,Railstutorial.org,我创建了两个名为“用户”和“商店”的用户模型。当我尝试将@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_
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>
<% provide(:title, 'All shops') %>
<h1>All shops</h1>
<ul class="shops">
<% @shops.each do |shop| %>
<li>
<%= link_to shop.name, shop %>
</li>
<% end %>
</ul>
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
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
这是商店控制器:商店控制器.rb
<% provide(:title, 'All users') %>
<h1>All users</h1>
<ul class="users">
<% @users.each do |user| %>
<li>
<%= link_to user.name, user %>
</li>
<% end %>
</ul>
<% provide(:title, 'All shops') %>
<h1>All shops</h1>
<ul class="shops">
<% @shops.each do |shop| %>
<li>
<%= link_to shop.name, shop %>
</li>
<% end %>
</ul>
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
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 %>
|
,
您的一些记录为零,要识别它们,请运行以下代码,您将获得这些记录的索引值
<% @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 %>
我看不到您在哪里分配了@shops
。请您将index.html.erb中的
替换为
,然后再试一次好吗?@shivam:他说“这个用户索引页面工作:user/index.html.erb”。你可以完全忽略这一点,它不会给问题增加任何东西,依我看。你可以添加raise@shops。检查行上方的吗:这样做是一种代码气味(将模型抓取推到视图层被认为是不好的)-我想@shivam只是建议它,看看你的Shop
类是否坏了,或者您获取@shops
@TarynEast的方式,我在原始代码上添加了提升线,整个店铺列表都在顶部添加了提升线。拆下提升管,现在工作正常。不知道为什么。每个
都应该意味着不调用循环。。。看起来,(奇怪的是,@shops不是空的,但其中有nil值,但是正如你在问题中提到的,你得到的是NoMethodError(nil:NilClass的未定义方法名)
error对吗?@virgeasault你没有抓住要点。答案中的代码并不是试图确定“商店是否填写了字段”。它试图解决包含nil的@shops
变量的问题。@VirgeAssault如果您得到NilClass错误,因为@shops有nil记录,请尝试nil.name
在您的控制台中,您将得到相同的错误听起来像是一个新问题:)可能最好作为问题而不是注释提问。