这是我得到的错误:
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 %>
Heres是我的控制器代码:
class SpecialsController&lt; 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实例,但它似乎没有可用的路由。有什么建议吗?
答案 0 :(得分:1)
通常在更新记录时,我们会对路径进行补丁请求。您的表单应如下所示:
Filters.good
Rails将根据<%= form_for(@special) do |f| %>
已保留到数据库的事实确定正确的路由PATCH /specials/:id
。
如果您决定在@special
视图中将此相同的表单用作部分表单,请务必将其添加到您的控制器中:
new
无论您是def new
@special = Special.new
end
路线还是new
路线,edit
始终会有一个@special
对象来推断是否要form_for
1}}或PATCH /specials