定义@shops变量NoMethodError的问题

时间:2015-03-02 06:11:04

标签: ruby-on-rails ruby railstutorial.org

我创建了两个名为" 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'

此用户的索引页面有效: 的用户/ 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> 

但是商店的这个不是shop.name的b / c: 的店/ 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 %>

1 个答案:

答案 0 :(得分: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 %>