0

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

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

这是我理解的网址:/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
4

3 回答 3

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

于 2013-07-30T20:46:39.037 回答
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' %>

我创建了一个关于嵌套资源的简短测验,可能会有所帮助。

于 2013-07-30T20:50:22.037 回答
0

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

那行是:

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

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

谢谢大家,你们的帮助。我是 StackOverflow 和 Rails 的新手。得到如此迅速的回应真的让我感到很受欢迎!

于 2013-07-31T18:31:52.343 回答