我是铁杆新手。我为shop_products
的索引定义了控制器,如下所示
shop_profile.rb
class ShopProfile < ActiveRecord::Base
has_and_belongs_to_many :users
has_one :shop_inventory_detail
end
shop_product.rb
class ShopProduct < ActiveRecord::Base
belongs_to :shop_profile
end
shop_products_controller.rb
class ShopProductsController < ApplicationController
def index
@shop_profile = ShopProfile.find(params[:shop_profile_id])
@products = @shop_profile.shop_products
end
end
shopprofiles中的index.html.erb
<%= link_to 'All Products', shop_profile_shop_products_path(@shop_profile) ,class: 'btn btn-primary' %>
在这一行我得到错误
ActionController::UrlGenerationError in ShopProfiles#index
Showing /home/mindfire/Desktop/project/training/Rails/grocery-shop/app/views/shop_profiles/index.html.erb where line #4 raised:
No route matches {:action=>"index", :controller=>"shop_products", :shop_profile_id=>nil} missing required keys: [:shop_profile_id]
路线
shop_profile_shop_products GET /users/shop_profiles/:shop_profile_id/shop_products(.:format) shop_products#index
POST /users/shop_profiles/:shop_profile_id/shop_products(.:format) shop_products#create
new_shop_profile_shop_product GET /users/shop_profiles/:shop_profile_id/shop_products/new(.:format) shop_products#new
edit_shop_profile_shop_product GET /users/shop_profiles/:shop_profile_id/shop_products/:id/edit(.:format) shop_products#edit
shop_profile_shop_product GET /users/shop_profiles/:shop_profile_id/shop_products/:id(.:format) shop_products#show
PATCH /users/shop_profiles/:shop_profile_id/shop_products/:id(.:format) shop_products#update
PUT /users/shop_profiles/:shop_profile_id/shop_products/:id(.:format) shop_products#update
DELETE /users/shop_profiles/:shop_profile_id/shop_products/:id(.:format) shop_products#destroy
当我手动传递shop_profile_id
时,我会得到所需的页面。
提前感谢您的帮助。
shop_profiles_controller.rb
class ShopProfilesController < ApplicationController
before_action :authenticate_user!, except: :show
after_action :verify_authorized, only: :shop_index
def new
@shop = ShopProfile.new
end
def index
@shops = current_user.shop_profiles
end
def show
@shop_profile = ShopProfile.find_by(id: params[:id])
@items = @shop_profile.shop_products.group(:category_id).where(category_id: params[:category_id])
end
def create
@shop = ShopProfile.new(shop_params)
@shop.build_address(address_params_shopkeeper)
if current_user.shop_profiles << @shop
flash[:success] = 'Shop Details added'
redirect_to root_path
else
flash[:error] = 'Shop Details not added'
render 'new'
end
end
def edit
@shop = current_user.shop_profiles.find_by(id: params[:id])
end
def update
@shop = current_user.shop_profiles.find_by(id: params[:id])
if @shop.update_attributes(shop_params) and @shop.address.update_attributes(address_params_shopkeeper)
flash[:success] = 'Updated Successfully'
redirect_to shop_profiles_path
else
flash[:danger] = 'Shop Details not Updated'
render 'edit'
end
end
end
但我认为这与shop_profiles_controller无关。 我从那里调用了shop_product索引页面。
错误日志
Started GET "/users/shop_profiles" for 127.0.0.1 at 2016-03-31 16:36:34 +0530
Processing by ShopProfilesController#index as HTML
User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 3 ORDER BY `users`.`id` ASC LIMIT 1
Rendered shop_profiles/index.html.erb within layouts/application (2.3ms)
Completed 500 Internal Server Error in 8ms (ActiveRecord: 0.4ms)
ActionView::Template::Error (No route matches {:action=>"index", :controller=>"shop_products", :shop_profile_id=>nil} missing required keys: [:shop_profile_id]):
1: <div>
2: <%= link_to 'Add Shop' ,new_shop_profile_path, class: 'btn btn-primary' %>
3: <%= link_to 'Add New Product', new_product_path, class: 'btn btn-primary', method: :get %>
4: <%= link_to 'All Products', shop_profile_shop_products_path(@shop_profile) ,class: 'btn btn-primary' %>
5: </div>
6: <div>
7: <% if !@shops.nil? %>
app/views/shop_profiles/index.html.erb:4:in `_app_views_shop_profiles_index_html_erb___2474323268141556614_25251260'
Rendered /home/mindfire/.rvm/gems/ruby-2.2.0@localshop/gems/actionpack-4.2.5.1/lib/action_dispatch/middleware/templates/rescues/_source.erb (9.0ms)............
答案 0 :(得分:0)
谢谢@Зелёный
从您的错误日志中,您发现正在尝试在导致此问题的nil
中发送shop_profile_shop_products_path
。
确保@shop_profile
不是nil
。
如果您想避免此问题,可以执行以下操作:
<%= link_to 'All Products', shop_profile_shop_products_path(@shop_profile) ,class: 'btn btn-primary' if @shop_profile.present? %>
希望它能解决你的问题!
答案 1 :(得分:0)
谢谢大家的回复。 问题是用户可以拥有多个商店档案。因此,我遍历所有商店配置文件并调用索引方法并获得所需的页面。