2017-08-05 71 views
0

我收到此錯誤。沒有路線匹配{:action =>「show」,:controller =>「statics」},缺少必需的鍵:[:id]

enter image description here

<%= link_to "New Product", 
new_static_path %> 

靜力學控制器

class StaticsController < ApplicationController 
def index 
    @products = Product.all 
end 

def new 
    @product = Product.new 
end 

def show 
    product_value = Product.find(params[:id]) 
    @product_attribute = ProductAttribute.where(value: product_value.value) 
end 

def create 
    @product = Product.new(product_params) 
    if @product.save 
     redirect_to root_url 
    else 
     render 'new' 
    end 
end 

def edit 
    @product = Product.find(params[:id]) 
end 

def update 
    @product = Product.find(params[:id]) 
    if @product.update(product_params) 
     redirect_to root_url 
    else 
     render 'edit' 
    end 
end 

def destroy 
    Product.find(params[:id]).destroy 
    flash[:success] = "Profile Updated" 
    redirect_to root_url 
end 

private 

    def product_params 
     params.require(:product).permit(:name,:value) 
    end 

Index.html.erb
<h1>Listing the products in home page</h1> 

<% @products.each do |product| %> 
    <li><%=link_to product.name, static_path(product) %></li> 
    <li><%=link_to "Edit", edit_static_path(product) %></li> 
<% end %> 

的routes.rb 條
root 'statics#index' 
resources :statics 
resources :products 
resources :productattributes 

耙路線

   Prefix Verb URI Pattern       Controller#Action 
       root GET /         statics#index 
       statics GET /statics(.:format)     statics#index 
         POST /statics(.:format)     statics#create 
      new_static GET /statics/new(.:format)    statics#new 
      edit_static GET /statics/:id/edit(.:format)   statics#edit 
       static GET /statics/:id(.:format)    statics#show 
         PATCH /statics/:id(.:format)    statics#update 
         PUT /statics/:id(.:format)    statics#update 
         DELETE /statics/:id(.:format)    statics#destroy 
      products GET /products(.:format)     products#index 
         POST /products(.:format)     products#create 
      new_product GET /products/new(.:format)    products#new 
     edit_product GET /products/:id/edit(.:format)   products#edit 
       product GET /products/:id(.:format)    products#show 
         PATCH /products/:id(.:format)    products#update 
         PUT /products/:id(.:format)    products#update 
         DELETE /products/:id(.:format)    products#destroy 

    productattributes GET /productattributes(.:format) productattributes#index 

POST /productattributes(.:format)  productattributes#create 

new_productattribute GET /productattributes/new(.:format)  productattributes#new 

edit_productattribute GET /productattributes/:id/edit(.:format) productattributes#edit 

productattribute  GET /productattributes/:id(.:format)  productattributes#show 

PATCH /productattributes/:id(.:format)  productattributes#update 

PUT /productattributes/:id(.:format)  productattributes#update 

    DELETE /productattributes/:id(.:format)  productattributes#destroy 

這是該項目的耙路線。

+0

我讓你提出這些變化,但其不接受statics_path時,提示static_path。@ widjajayd –

+0

我只是改變我的回答對按照您的要求 – widjajayd

回答

0

與導軌不同軟件點點的方式 或許這可以幫助,如果你不希望使用不同的模型與控制器名稱

改變你的routes.rb如下

resources :products, path: "statics" 
+0

廣東話我直接到靜態控制器的路徑。@ widjajayd –

+0

我爲打擾你感到難過,但我仍然堅持同樣的錯誤要麼。 –

+0

好吧請改變你的routes.rb後請重新啓動rails服務器命令 – widjajayd

相關問題