首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在嵌套路由中创建新的子节点

在嵌套路由中创建新的子节点
EN

Stack Overflow用户
提问于 2013-07-30 20:28:00
回答 3查看 327关注 0票数 0

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

所以,我在任何地方都可以找到它的引用,但我发现自己无法访问这个页面来创建一个新的子对象。

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

来自routes.rb:

代码语言:javascript
复制
resources :festivals do
   resources :years
end

来自years_controller.rb:

代码语言:javascript
复制
# 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

用户按下按钮进入New页面(在“显示”页面上显示父对象):

代码语言:javascript
复制
<%= link_to 'Add Year', new_festival_year_path(@festival), :class => 'btn' %>

这会让用户找到正确的URL,但我得到:

代码语言:javascript
复制
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输出

代码语言:javascript
复制
            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
EN

回答 3

Stack Overflow用户

发布于 2013-07-30 20:46:39

试试这个:

代码语言:javascript
复制
<%= link_to 'Add Year', new_festival_year_path(@festival.id, :class => 'btn' %>    

代码语言:javascript
复制
<%= link_to 'Add Year', new_festival_year_path({festival_id: @festival.id}, :class => 'btn' %>

根据你所犯的错误

代码语言:javascript
复制
: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
EN

Stack Overflow用户

发布于 2013-07-30 20:50:22

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

代码语言:javascript
复制
# 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

您还应该更新您的链接:

代码语言:javascript
复制
<%= link_to 'Add Year', new_festival_year_path(festival_id: @festival), :class => 'btn' %>

我创建了一个可能有用的嵌套资源的简短测验

票数 0
EN

Stack Overflow用户

发布于 2013-07-31 18:31:52

答案相当愚蠢。在我的Rails服务器日志(我需要训练自己更多地注意这个日志)中,我看到了_form.html.erb部分第63行中出现问题的一些行。

这一行是:

代码语言:javascript
复制
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
   festival_year_path(@festival), :class => 'btn' %>

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

谢谢大家的帮助。我是StackOverflow和Rails的新手。这真的让我感到很受欢迎,因为我得到了如此迅速的反应!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17956737

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档