Rails 3:无法让 form_for 充当“删除”功能遵循 RESTful 架构 =>总是给出路由错误
我有一个非常简单的渲染,如下所示:
<%= form_for(:relationships, :url => relationships_path, :html => {:method => 'delete'}) do |f| %>
<div><%= f.hidden_field :user_id_to_unfollow, :value => @user.id %></div>
<div class="actions"><%= f.submit "Unfollow" %></div>
<% end %>
当我提交此表单时,它总是会
Routing Error
No route matches "/relationships"
在我的页面上给我一个。
在我的关系控制器中,我创建了所有正确的方法:
def create
...
end
def destroy
...
end
def update
...
end
def show
...
end
并且在我的路由配置中,我确保允许关系控制器的所有路由
resources :relationships
但我似乎无法进入控制器的 destroy 方法:(
但是如果我删除
:html => {:method => 'delete'}
form_for 中的方法参数,然后我到达控制器的创建方法,没有 pb,
我不明白......
Alex
ps:这是关系的 rake 路由结果:
relationships GET /relationships(.:format) {:action=>"index", :controller=>"relationships"}
POST /relationships(.:format) {:action=>"create", :controller=>"relationships"}
I have a very simple render that goes as follow:
<%= form_for(:relationships, :url => relationships_path, :html => {:method => 'delete'}) do |f| %>
<div><%= f.hidden_field :user_id_to_unfollow, :value => @user.id %></div>
<div class="actions"><%= f.submit "Unfollow" %></div>
<% end %>
When I submit this form it will always give me a
Routing Error
No route matches "/relationships"
on my page.
In my relationships controller, I have created all the propers methods:
def create
...
end
def destroy
...
end
def update
...
end
def show
...
end
And in my routes config I have made sure to allow all routes for the relationships controller
resources :relationships
But I can't seem to get into the destroy method of the controller :(
However if I remove the
:html => {:method => 'delete'}
method parameter in the form_for then I get to the create method of the controller no pb.
I don't get it....
Alex
ps: this is the rake routes results for relationships:
relationships GET /relationships(.:format) {:action=>"index", :controller=>"relationships"}
POST /relationships(.:format) {:action=>"create", :controller=>"relationships"}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该将
delete
请求指向单个资源 url,例如。关系/4325
。运行 rake paths 来查看哪些 url/verb 组合是有效的。--编辑
关系资源的路由:
取消关注按钮(为自己创建一个表单):
You should point the
delete
request to single resource url eg.relationships/4325
. Runrake routes
to view what url/verb combinations are valid.--edit
Routes for relationship resources:
Unfollow button (creates a form for itself):