我是rails新手。我已經用於shop_products
索引定義控制器如下ShopProfiles中的ActionController :: UrlGenerationError#index
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
index.html.erb在shopprofiles
<%= 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
手動我ge t所需的頁面。 在此先感謝您的幫助。
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 "https://stackoverflow.com/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 [email protected]? %>
app/views/shop_profiles/index.html.erb:4:in `_app_views_shop_profiles_index_html_erb___2474323268141556614_25251260'
Rendered /home/mindfire/.rvm/gems/[email protected]/gems/actionpack-4.2.5.1/lib/action_dispatch/middleware/templates/rescues/_source.erb (9.0ms)............
你確定'@ shop_profile'不是'nil'嗎,你是否嘗試過直接傳遞一個ID'shop_profile_shop_products_path(@ shop_profile.id)'? –
試試這個'shop_profile_shop_products_path(shop_profile_id:@ shop_profile.id)' – illusionist
'ShopProfiles#index'中的ActionController :: UrlGenerationError - 你的錯誤說這個問題出現在ShopProfilesController中。你可以發佈你的ShopProfilesController控制器嗎? – dp7