单一资源的Rails嵌套路线 [英] Rails Nested Route For Singular Resource

查看:55
本文介绍了单一资源的Rails嵌套路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在单个资源上有一条嵌套的路线

I have a nested route on a singular resource

map.resource :account, :controller => "users" do |page|
  page.resources :feeds
end

我正在尝试制作一个表单以添加供稿。我从这里开始...

I'm trying to make a form to add a feed. I start with this...

<% form_for @feed do |f| %>
undefined method `feeds_path' for #<ActionView::Base:0x2123174>

所以我尝试

<% form_for [current_user,@feed] do |f| %>
undefined method `user_feeds_path' for #<ActionView::Base:0x20b3e00>

我想这是由于将其从用户重命名为帐户吗?所以我试过了

I guess that is due to renaming it from "users" to "account"? So I tried

<% form_for account_feeds_path(@feed) do |f| %>
`@/account/feeds.%23%3Cfeed:0x22ea5c0%3E' is not allowed as an instance variable name

不确定该错误是什么意思。因此,我采取了这种有效的方法:

Not sure what that error means. So I've resorted to using doing this which works:

<% form_for @feed, :url=>{:action=>:create} do |f| %>

这真的是最好的方法吗?换句话说,在这种情况下没有办法使用命名路由吗?

Is that really the best way? In other words, is there no way to use named routes in this situation?

推荐答案

我认为您想要做的是

<% form_for [:account, @feed] do |f| %>

然后,当@feed是新记录时,form_for会在POST中使用account_feeds_path,而account_feed_pa​​th(@ @feed不是新记录时使用PUT。

form_for will then look to use the account_feeds_path with POST when @feed is a new record and account_feed_path(@feed) with PUT when @feed is not a new record.

这篇关于单一资源的Rails嵌套路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆