没有路线匹配{:action =>" show",:controller =>" statics"},缺少必需的键:[:id]

时间:2017-08-05 05:00:12

标签: ruby-on-rails

我收到此错误。

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

这是项目的佣金路线。

1 个答案:

答案 0 :(得分:0)

因为你的软件与rails方式有点不同 如果您不想使用具有控制器名称的不同模型

,这可能会有所帮助

更改您的routes.rb,如下所示

resources :products, path: "statics"
相关问题