[英]No route matches [GET] error in custom put action even though I already specified method: put?
I'm trying to create a new action in my post controller called move_up
which changes the position
attribute of a post. 我试图在我的帖子控制器中创建一个名为
move_up
的新动作,该动作会更改帖子的position
属性。 I got all set up: 我已经完成所有设置:
routes.rb 的routes.rb
resources :posts do
member do
put :move_up
end
end
posts_controller.rb: before_action :set_post, only: [:show, :edit, :update, :destroy, :move_post_up] posts_controller.rb: before_action:set_post,仅:[:show,:edit,:update,:destroy,:move_post_up]
def move_up
@post.update(position: 0)
end
index.html.erb: index.html.erb:
<% @posts.each do |post| %>
<tr>
<td><%= post.content %></td>
<td><%= post.position %></td>
<td><%= link_to 'Show', post %></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<td><%= link_to 'Move up', move_up_post_path(post) %></td>
</tr>
<% end %>
But when I click the link I get this error: 但是,当我单击链接时,出现此错误:
Started GET "/posts/1/move_up?method=put" for 127.0.0.1 at 2014-02-23 12:09:00 +0800
ActionController::RoutingError (No route matches [GET] "/posts/1/move_up"):
How to fix this? 如何解决这个问题?
The problem is that you are sending a GET
request to an action that only has a PUT
route defined for it. 问题是您正在向仅为其定义了
PUT
路由的操作发送GET
请求。 In this case you would want to specify the put method in your link_to
, like so: 在这种情况下,您需要在
link_to
指定put方法,如下所示:
<td><%= link_to 'Move up', move_up_post_path(post) , method: :put %></td>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.