Virge Assault
Virge Assault

Reputation: 1396

Problems with defining @shops variable NoMethodError

I've created two user models called "user" and "shop". When I try to use the @shop variable as shop (like in shop.name) it will not work, but user.name will work. So I missed defining shop at some point, and cannot figure out where to fix it.

Here's the error I get:

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'

This index page for user works: 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> 

But this one for shops does not 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>  

Here is the shops controller: 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

And here is the session helper for shops: 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 former

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

Upvotes: 1

Views: 124

Answers (1)

Rajdeep Singh
Rajdeep Singh

Reputation: 17834

Some of your records are nil, to identify them run the following code, you will get index values of those records

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

Upvotes: 3

Related Questions