在嵌套路由中创建新子项

时间:2013-07-30 20:28:00

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

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

因此,无论我在何处找到对它的引用,我都会将关系淡化,但我发现自己无法访问该页面来创建新的子对象。

以下是我理解的网址:/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' %>

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

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

3 个答案:

答案 0 :(得分: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

的输入

答案 1 :(得分:0)

我认为您正在将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

答案 2 :(得分:0)

答案很愚蠢。在我的Rails服务器日志中(我需要训练自己以引起更多关注),我看到一些行表示我的_form.html.erb部分第63行出现问题。

该行是:

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

糟糕。为什么我决定将“取消”按钮带到(当然,这不会存在)超出我的范围。我将其更改为festival_path(@festival)并且一切都很好。

谢谢大家,感谢您的帮助。我是StackOverflow和Rails的新手。我真的很高兴能得到如此快速的反应!