【问题标题】:Rails has_one belongs_to Routing and form_forRails has_one belongs_to 路由和 form_for
【发布时间】:2015-01-12 17:51:34
【问题描述】:

所以我有两个模型

class First < ActiveRecord::Base
  belongs_to :story
end

class Story < ActiveRecord::Base
    has_one :first
end

我想创建一个新的第一个,它与一个故事相关联。每个故事只能有一个第一。我正在尝试将其用作我的 form_for

<%= form_for ([@story, @first]) do |f| %>
 <%= f.label :message %><br/>
 <%= f.text_field :message %><br/>
 <%= f.submit %>
 <% end %>

但是,我不知道如何在我的路线中进行设置以适应这种情况。目前,我收到“未定义的方法story_firsts_path”错误。这是我的第一个控制器

class FirstsController < ApplicationController
  def new
    @story = Story.new
    @first = @story.build_first
  end
end

我离这里很远还是我在正确的轨道上?

谢谢!

【问题讨论】:

    标签: ruby-on-rails routing form-for belongs-to has-one


    【解决方案1】:

    您应该使用嵌套路由来定义故事和第一。像这样:

    resources :stories do
      resources :firsts
    end
    

    【讨论】:

    • 单一资源就足够了。他的控制器也不正确。
    猜你喜欢
    • 2012-07-05
    • 2014-10-01
    • 2014-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多