2015-03-02 50 views
1

我创建了两个名为“user”和“shop”的用户模型。当我尝试使用@ shop变量作为商店时(如shop.name中),它将无法工作,但user.name将起作用。所以我错过了在某个时候定义商店,并且无法弄清楚在哪里解决问题。定义@shops变量的问题NoMethodError

这是我得到的错误:

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' 

这对于用户索引页面的工作原理: 用户/ 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的shop.name店铺/ 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 %> 
+0

我不能看到您已经分配'索引值@ shops'。您能否替换<%@ shops.each do | shop | %>'在index.html.erb中用<%Shop.find_each do | shop | %>'然后重试? – shivam 2015-03-02 06:18:07

+2

@shivam:他说:“这个用户工作的索引页面:user/index.html.erb”。你可以完全忽略它,它不会给问题添加任何东西,imo。 – limekin 2015-03-02 06:19:33

+0

您可以在行的上方添加'raise @ shops.inspect':'<%@ shops.each'并再次运行 - 这样我们就可以看到'@ shops'实际发生了什么? – 2015-03-02 06:24:32

回答

3

你的一些记录是零,以确定它们运行下面的代码,你会得到这些记录

<% @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 %> 
+2

'每个'*应该*意味着你只是不要调用循环......它似乎,(奇怪),@shops不是空的,但它有零值在某种程度上 – 2015-03-02 06:26:54

+1

我想downvotes应该现在被删除! – RSB 2015-03-02 06:45:21

+0

这些商店都有自己的领域填补,错误消息和验证将抓住任何空白。不知道Taryn提到 – 2015-03-02 06:56:48