Ruby on rails RubyonRails-如何在嵌套资源保存中保存外键?

Ruby on rails RubyonRails-如何在嵌套资源保存中保存外键?,ruby-on-rails,ruby,Ruby On Rails,Ruby,我是Rails新手(这里是Stackoverflow),我正在做我的第一个应用程序。应用程序允许在另一个视图中创建游览并管理其铭文,使用路径:游览/:游览id/铭文等。对于此提议,我定义了两个模型:游览和铭文,如下所示: class Excursion < ActiveRecord::Base has_many :inscriptions validates :name, presence: true, length: { minimum: 5 } validates :busSpots

我是Rails新手(这里是Stackoverflow),我正在做我的第一个应用程序。应用程序允许在另一个视图中创建游览并管理其铭文,使用路径:游览/:游览id/铭文等。对于此提议,我定义了两个模型:游览和铭文,如下所示:

class Excursion < ActiveRecord::Base
has_many :inscriptions

validates :name, presence: true, length: { minimum: 5 }
validates :busSpots, presence: true, length: { minimum: 0 }
validates :lunchSpots, presence: true, length: { minimum: 0 }
end

class Inscription < ActiveRecord::Base
    belongs_to :excursion, :autosave => true

    validates :name, presence: true, length: { minimum: 5 }
    validates :phoneNumber, presence: true, length: { is: 9 }
    validates :email, presence: true #Afegir regexp per a email validation.
    validates :busSpots, presence: true, length: { minimum: 0 }
    validates :lunchSpots, presence: true, length: { minimum: 0 }
end
这是我的index.html.erb游览视图:

<% @excursions.each do |excursion| %>
<tr>
    <td><%= excursion.name %></td>
    <td><%= excursion.busSpots %></td>
    <td><%= excursion.lunchSpots %></td>
    <td><%= excursion.active %></td>
    <td><%= link_to 'Show details', excursion_path(excursion) %></td>
    <td><%= link_to 'Edit excursion', edit_excursion_path(excursion) %></td>
    <td><%= link_to 'Delete excursion', excursion_path(excursion),
    method: :delete,
    data: { confirm: 'Are you sure?' } %></td>
    <td><%= link_to 'Show inscriptions', excursion_inscriptions_path(:excursion_id => excursion.id) %></td>
    <td><%= link_to 'Do an inscription', new_excursion_inscription_path(:excursion_id => excursion.id) %></td>
</tr>
<% end %>
当我试图保存铭文时,数据被保存,但数据库上的偏移id外键为空。所以我的问题是:我做错了什么?在铭文控制器中,哪一种方法是正确的,可以使用shiftion.id?

谢谢

编辑:我刚刚意识到,铭文表有两列:偏移id和偏移id。第一列由Rails通过模型
创建,属于
标签。第二列由外键声明创建。为什么会这样?在ActiveRecord中手动定义外键正确吗

EDIT2并解决了:最后,这个问题与表的创建有关。我在铭文表中创建了两个外键。对于未来可能的用户,我已通过以下方式创建了我的题词表:

resources :excursions do
    resources :inscriptions
end
class CreateInscriptions < ActiveRecord::Migration
    def change
        create_table :inscriptions do |t|
            t.belongs_to :excursion, index:true, foreign_key: true
            t.string :name
            t.integer :phoneNumber
            t.string :email
            t.integer :busSpots
            t.integer :lunchSpots

            t.timestamps null: false
        end

        #THIS LINE WAS NOT NECESSARY -> add_reference :inscriptions, :excursion, index: true, foreign_key: true
    end
end
class-createdintermints添加参考:铭文,:偏移,索引:真,外键:真
结束
结束

感谢Andrew Hendrie和Frederick Cheung的帮助和耐心。

这可能是因为强大的参数。您需要将偏移id添加到允许的参数中

def excursion_params
  params.require(:excursion).permit(:name, :busStops, :lunchStops, :active, inscriptions_attributes: :excursion_id)
end 

我已将你的建议加入到远足计划中,但没有成功。我认为主要的问题是,在铭文控制器中没有定义偏移id。您显示的偏移控制器看起来不错。如果params[:execution_id]不存在,则显示的代码将引发异常。确实调用了create方法吗?(尝试在其中添加记录器语句或断点)正在调用该方法。我刚刚意识到,在铭文表中定义了两个外键。
def excursion_params
  params.require(:excursion).permit(:name, :busStops, :lunchStops, :active, inscriptions_attributes: :excursion_id)
end