2016-07-27 15 views
0

这是我得到的错误:Rails的:没有路由匹配[POST]“/特价/ 1”

No route matches [POST] "/specials/1" 

据我所知,这是不能够产生后的路线,或者是不可用。

这是我的看法/表单代码:

<%= form_for(:special, :url => {:action => 'update', :id => @special.id}) do |f| %> 

    <table class="table table-responsive table-striped table-condensed table-hover" summary="Special form fields"> 
     <tr> 
     <th>Order</th> 
     <td><%= f.text_field :order, class: "form-control" %></td> 
     </tr> 
     <tr> 
     <th>Name</th> 
     <td><%= f.text_field :name, class: "form-control" %></td> 
     </tr> 
     <tr> 
     <th>Description</th> 
     <td><%= f.text_field :description, class: "form-control" %></td> 
     </tr> 
     <tr> 
     <th>Fine Print</th> 
     <td><%= f.text_field :fine_print, class: "form-control" %></td> 
     </tr> 
     <tr> 
     <th>Active</th> 
     <td><%= f.text_field :active, class: "form-control" %></td> 
     </tr> 
    </table> 

    <div class="form-buttons"> 
     <%= submit_tag("Update Special") %> 
    </div> 

    <% end %> 

继承人是我的控制器代码:

类SpecialsController < ApplicationController的

def index 
    @specials = Special.sorted 
    end 

    def show 
    @special = Special.find(params[:id]) 
    end 

    def new 
    @special = Special.new 
    end 

    def create 
    #Instantiation of object using form parameters 
    @special = Special.new(special_params) 
    #Save the object 
    if @special.save 
     #If success, redirect to index action 
     redirect_to(:action => 'index') 
    else 
     # Redisplay the form so user can fix problems 
     render('new') 
    end 
    end 

    def edit 
    @special = Special.find(params[:id]) 
    end 

    def update 
    #Find an existing object using form parameters 
    @special = Special.find(params[:id]) 
    #Update the object 
    if @special.update_attributes(special_params) 
     #If succeeds, redirect to index action 
     redirect_to(:action => 'show', :id => @special.id) 
    else 
     # If update fails, redisplay the form so user can fix problems 
     render('edit') 
    end 
    end 

    def delete 
    end 

private 
    def special_params 
    params.require(:special).permit(:name, :description, :fine_print, :active, :order) 
    end 

end 

我注意到,有一个更新的路径:

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

我不明白为什么邮政路线没有被应用。它正在寻找正确的@special实例,但似乎没有可用的路由。有什么建议?

回答

1

通常在更新记录时,我们会向路由发出补丁请求。您的形式应该是这样的:

<%= form_for(@special) do |f| %> 

Rails会确定正确的路线是PATCH /specials/:id基于这样的事实,@special已经被保存到数据库。

如果你决定在你的new视图中使用这种形式相同的部分,只要确保它添加到你的控制器:

def new 
    @special = Special.new 
end 

这样,无论你是new路线或edit路线,总是会有一个@special对象form_for来推断是否POST到/specials或PATCH /specials/:id

+0

这没有用。它扔了不同的错误:未定义的局部变量或方法'subject_params'为# PudparK

+1

是'subject_params'应该是'special_params'或者你没有定义你的'SpecialsController'一个'subject_params'方法。你能用你的'SpecialsController'完整的代码来更新你的答案吗? –

+0

我添加了代码。我改变了special_params,但它没有解决。 – PudparK

相关问题