没有路由匹配{:action =>“ show”,:controller =>“ shops”,:id => nil},缺少必需的键:[:id]

时间:2019-06-30 12:03:17

标签: ruby-on-rails ruby

我正在构建一个电子商务应用程序,每个用户都可以在其中创建自己的商店。用户与商店之间的关联应为

user has_one shop
shop belongs_to user

到目前为止,创建商店的用户运转良好。但是对于那些没有的人,它向我显示了一个错误:

No route matches {:action=>"show", :controller=>"shops", :id=>nil},  missing required keys: [:id]

在我的shops_controller.rb

class ShopsController < ApplicationController
  before_action :find_shop, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!
  before_action :find_user

  def show
    if current_user.shop.blank?
        render 'new'
    else
        @items = Item.where(shop_id: @shop.id)   
    end
  end

  def index    
    @shops = Shop.all.order("created at DESC")    
  end

  def new
    @shop = current_user.build_shop
  end

  def create 
    @shop = current_user.build_shop(shop_params)
    if @shop.save
        session[:shop_id] = @shop.id
       flash[:success] = "Creating item success"
       redirect_to @shop, notice: 'success'
    else
        render 'new'
    end
  end

private 

  def shop_params
    params.require(:shop).permit( :name , :user_id)
  end

  def find_store
    @shop = Shop.find(params[:id])
  end

  def find_user
    @user = UrStore.find_by(params[:user_id])
  end
end

application.html.erb

 <% if user_signed_in?%>
   <%= link_to "profile", user_path(current_user.id) %>
   <% if current_user.shop == nil %>
     <li><%= link_to "Your shop", new_shop_path %></li>
   <% else %>
     <li><%= link_to "Your shop", shop_path(current_user.shop.id)%></li>    
   <% end %>   
 <% end %>

current_user由gem'devise'自动生成。

当我单击“您的商店”时发生错误,并且该错误仅发生在未创建商店的用户中

在routes.rb中:

devise_for :users
root 'static_pages#home'
as :user do
  get "signin" => "devise/sessions#new"
  post "signin" => "devise/sessions#create"
  delete "signout" => "devise/sessions#destroy"
end
resources :shops

此行引发了错误:

<li><%= link_to "Your Shop",shop_path(current_user.shop.id)%></li>

我正在寻找解决此问题的方法:-)

2 个答案:

答案 0 :(得分:3)

当您转到new_shop_path时,控制器操作new似乎建立了一个用户商店:

@shop = current_user.build_shop

所以从这里current_user.shop != nil

但是由于当时尚未保存,因此该商店没有id。因此,在您看来,它在else中使用,因为shop不是nil,但是没有id,因此会引发错误。

<% if current_user.shop == nil %>
  <li><%= link_to "Your shop", new_shop_path %></li>
<% else %>
  <li><%= link_to "Your shop", shop_path(current_user.shop.id)%></li>    
<% end %>   

将其更改为:

<% if !current_user.shop || !current_user.shop.id %>
  <li><%= link_to "Your shop", new_shop_path %></li>
<% else %>
  <li><%= link_to "Your shop", shop_path(current_user.shop.id)%></li>    
<% end %>   

答案 1 :(得分:2)

我认为您要重定向而不是渲染,并且还要确保shop被持久保存到数据库中。

def show
  if current_user.shop&.persisted?
    redirect_to :new
  else
    @items = Item.where(shop_id: @shop.id)   
  end
end

在您看来,您也可以使用安全导航并使用.persisted?来执行此操作,因为您更关心shop.id,并且这种逻辑比其他方法更有意义。

<% if current_user.shop&.persisted? %>
  <li><%= link_to "Your shop", shop_path(current_user.shop.id)%></li>
<% else %>
  <li><%= link_to "Your shop", new_shop_path %></li>
<% end %>