【问题标题】:Creating new child in nested routes在嵌套路由中创建新子级
【发布时间】:2013-07-31 04:09:27
【问题描述】:

这个让我发疯。我的项目中有两个模型之间的嵌套关系,我决定我确实希望它变浅,因为子对象(年)在父对象的上下文之外没有意义(节日)。

因此,我在可以找到对它的引用的任何地方都对关系进行了去浅化,但我发现自己无法访问该页面来创建新的子对象。

这是我理解的网址:/festivals/1/years/new

来自 routes.rb:

resources :festivals do
   resources :years
end

来自years_controller.rb:

# GET festivals/1/years/new
# GET festivals/1/years/new.json
def new
  @festival = Festival.find(params[:festival_id])
  @year = @festival.years.build

  respond_to do |format|
    format.html # new.html.erb
    format.json { render :json => @year }
  end
end

用户按下按钮进入新页面(在父对象的显示页面上):

<%= link_to 'Add Year', new_festival_year_path(@festival), :class => 'btn' %>

这会将用户带到正确的 URL,但我得到:

No route matches {:action=>"show", :controller=>"years", :festival_id=>#<Festival id: 7, name: "Improganza", founded: nil, logo: "", mission: "This is that one that people spend a lot of money t...", city: "Honolulu", state_code: "HI", country_code: "US", created_at: "2013-07-26 14:49:19", updated_at: "2013-07-26 14:49:19">}

我创建了一个新的 Rails 项目并使用 Akria Matsuda 的 nested_scaffold gem 设置了脚手架,只是为了将该输出与我的代码进行比较......生成的文件看起来就像我在这里展示的那样。我不知道我可能会错过什么。

只是为了衡量,我的rake routes 的输出:

            festival_years GET        /festivals/:festival_id/years(.:format)          years#index
                           POST       /festivals/:festival_id/years(.:format)          years#create
         new_festival_year GET        /festivals/:festival_id/years/new(.:format)      years#new
        edit_festival_year GET        /festivals/:festival_id/years/:id/edit(.:format) years#edit
             festival_year GET        /festivals/:festival_id/years/:id(.:format)      years#show
                           PUT        /festivals/:festival_id/years/:id(.:format)      years#update
                           DELETE     /festivals/:festival_id/years/:id(.:format)      years#destroy
                 festivals GET        /festivals(.:format)                             festivals#index
                           POST       /festivals(.:format)                             festivals#create
              new_festival GET        /festivals/new(.:format)                         festivals#new
             edit_festival GET        /festivals/:id/edit(.:format)                    festivals#edit
                  festival GET        /festivals/:id(.:format)                         festivals#show
                           PUT        /festivals/:id(.:format)                         festivals#update
                           DELETE     /festivals/:id(.:format)                         festivals#destroy
                           GET        /festivals(.:format)                             festivals#index
                           POST       /festivals(.:format)                             festivals#create
                           GET        /festivals/new(.:format)                         festivals#new
                           GET        /festivals/:id/edit(.:format)                    festivals#edit
                           GET        /festivals/:id(.:format)                         festivals#show
                           PUT        /festivals/:id(.:format)                         festivals#update
                           DELETE     /festivals/:id(.:format)                         festivals#destroy

【问题讨论】:

  • 当您看到“无路由”错误时,它是否与模板中的一行相关联?
  • 不,只是我引用的文字。

标签: ruby-on-rails ruby ruby-on-rails-3 routes nested-routes


【解决方案1】:

试试这个:

<%= link_to 'Add Year', new_festival_year_path(@festival.id, :class => 'btn' %>    

<%= link_to 'Add Year', new_festival_year_path({festival_id: @festival.id}, :class => 'btn' %>

根据您遇到的错误

:festival_id=>#<Festival id: 7, name: "Improganza", founded: nil, logo: "", mission: "This is that one that people spend a lot of money t...", city: "Honolulu", state_code: "HI", country_code: "US", created_at: "2013-07-26 14:49:19", updated_at: "2013-07-26 14:49:19">}

路由器正在获取您的整个节日参数作为:festival_id 的输入

【讨论】:

  • 谢谢,但事实证明并非如此......事实上,如果我不使用“添加年份”链接而是转到`/festivals/1/,则会发生同样的异常年/新'直接!
  • 你的意思是在视图中?我认为这不会有帮助,因为如果我直接访问 URL,它会给出同样的错误。不过我会试试看的。
  • 实际上,@dax 打破了视野。
【解决方案2】:

我认为您正在将 years_controller 中的 #new#year 操作合并在一起,这可能会导致一些问题。

# GET festivals/1/years/new
# GET festivals/1/years/new.json
def new
  @festival = Festival.find(params[:festival_id])
  @year = @festival.years.build
end

def create
  @festival = Festival.find(params[:festival_id])
  @year = @festival.years.create(...)
  #...fill in the rest of the method...
end

您还应该更新您的链接:

<%= link_to 'Add Year', new_festival_year_path(festival_id: @festival), :class => 'btn' %>

我创建了一个short quiz on nested resources,可能会有所帮助。

【讨论】:

  • 事实证明也不是这样。而#create 操作实际上已经和你的一样了。
  • 更新:我发现问题出在视图而不是控制器中,特别是 _form.html.erb 部分。我现在应该能够准确地缩小范围。谢谢大家的帮助。
【解决方案3】:

答案相当愚蠢。在我的 Rails 服务器日志中(我需要训练自己多加注意),我在_form.html.erb partial 的第 63 行中看到了一些表明问题的行。

那行是:

<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
   festival_year_path(@festival), :class => 'btn' %>

哎呀。为什么我曾经决定“取消”按钮应该带你到一个 year(当然,不存在)是我无法理解的。我把它改成了festival_path(@festival),一切都很好。

感谢大家的帮助。我是 StackOverflow 和 Rails 的新手。收到这么快的回复真的让我感到很受欢迎!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-10
    • 2022-08-17
    • 1970-01-01
    • 1970-01-01
    • 2018-12-23
    • 2013-08-19
    相关资源
    最近更新 更多