我不确定我做错了什么,但是当我尝试编辑/更新时,当我点击提交按钮时,我一直收到路由错误,说没有路由匹配[PUT]“/ products”。
我的routes.rb:
devise_for :users
root :to => 'products#latest'
resources :products
match "/popular" => "products#popular"
match "products/like/:id" => "products#like"
match "products/unlike/:id" => "products#unlike"
match "/tags/:tag" => "products#latest", as: :tag
控制器:
def edit
@product = Product.find(params[:id])
p @product.id
end
def update
@product = Product.find(params[:id])
if @product.update_attributes(params[:product])
flash[:notice] = "Product has been updated."
redirect_to @product
else
flash[:alert] = "Product has not been updated."
render :action => "edit"
end
end
表单视图:
<%= form_for @product, :url => products_path, :html => { :multipart => true } do |f| %>
Product Name: <%= f.text_field :name %>
<br>
Product Link: <%= f.text_field :product_link %>
<br>
Blog Link: <%= f.text_field :blog_link, :class => "form-control" %>
<br>
Blog Name: <%= f.text_field :blog_name, :class => "form-control" %>
<br>
Description: <%= f.text_field :description, :class => "form-control" %>
<br>
<%= f.label :tag_list, "Tags (separated by commas)" %><br />
<%= f.text_field :tag_list %>
<%= f.file_field :avatar %>
<br/>
<%= f.submit "Save", :class => "btn btn-large btn-success" %>
<% end %>
答案 0 :(得分:0)
尝试将“resources:products”放在devise_for块之外。 要显示可用的路由,请键入console:bundle exec rake routes
答案 1 :(得分:0)
您将表单提交至/products
而非/products/<id>
。
将form_for
行更改为
<%= form_for @product, :html => { :multipart => true } do |f| %>
和Rails将生成一个包含正确URL的表单。
如果由于某种原因您希望保留:url
选项,则可以使用:url => @product
或:url => product_path(@product)
。