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

时间:2016-02-21 20:25:29

标签: ruby-on-rails ruby

我是Rails的新手(在Stackoverflow中),我正在做我的第一个应用程序。应用程序允许使用route:excursion /:excursion_id / inscriptions等创建Excursions并在另一个视图中管理他们的铭文。对于这个建议,我已经定义了两个模型:Excursions和Inscriptions如下:

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

之后,我用这种方式定义了我的routes.rb:

resources :excursions do
    resources :inscriptions
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 %>

一切都运作良好,但我怀疑我的铭文控制器没有收到:excursion_id。当我尝试保存题字时,我使用创建方法:

def create
    @excursion = Excursion.find(params[:excursion_id])
    @inscription = Inscription.new(inscription_params)
    @inscription.excursion_id = @excursion.id
    if @inscription.save
        redirect_to @inscription
    else
        render 'new'
    end
end

当我尝试保存题词时,数据会被保存,但excursion_id外键在数据库中为空。所以我的问题是:我做错了什么?在铭文控制器中使用excursion.id的正确方法是什么?

感谢。

编辑:我刚刚意识到Inscription表有两列:excursions_id和excursion_id。第一个是由Rails通过模型belongs_to标签创建的。第二列是由外键声明创建的。为什么会这样?在ActiveRecord中手动定义外键是否正确?

EDIT2 AND SOLVED :最后,此问题与表创建有关。我在Inscriptions表中创建了两个外键。对于未来可能的用户,我以这种方式创建了我的Inscription表:

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

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

1 个答案:

答案 0 :(得分:0)

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

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