Rails 3:无法让 form_for 充当“删除”功能遵循 RESTful 架构 =>总是给出路由错误

发布于 2024-10-12 12:40:37 字数 1167 浏览 6 评论 0原文

我有一个非常简单的渲染,如下所示:

<%= 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁与争疯 2024-10-19 12:40:37

您应该将 delete 请求指向单个资源 url,例如。 关系/4325。运行 rake paths 来查看哪些 url/verb 组合是有效的。

--编辑

关系资源的路由:

resources :relationships, :only => [:index, :create, :destroy]

取消关注按钮(为自己创建一个表单):

= button_to "Unfollow", relationship_path(relationship), :method => 'delete'

You should point the delete request to single resource url eg. relationships/4325. Run rake routes to view what url/verb combinations are valid.

--edit

Routes for relationship resources:

resources :relationships, :only => [:index, :create, :destroy]

Unfollow button (creates a form for itself):

= button_to "Unfollow", relationship_path(relationship), :method => 'delete'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文