1

RoRの新機能。音楽エントリを投稿するための簡単なブログを作成しています。チューンがアーティストに属する 2 つのモデル Tune と Artist があります。simple_form を使用してフォームをレンダリングしています。私が直面している問題は、simple_form が正しい関連付けをプルアップしているが、送信をクリックすると、関連付けが正しく保存されず、検証に合格しないことです。コードは以下のとおりです。

クラス

class Tune < ActiveRecord::Base
  belongs_to :artist
  validates :artist, presence: true
end

class Artist < ActiveRecord::Base
  has_many :tunes
end

データベース スキーマ

ActiveRecord::Schema.define(version: 20131027190309) do

  create_table "artists", force: true do |t|
    t.string   "name"
    t.string   "real_name"
    t.string   "gender"
    t.string   "city"
    t.string   "country"
    t.string   "artist_soundcloud_link"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "tunes", force: true do |t|
    t.string   "soundcloud_link"
    t.string   "download_link"
    t.string   "download_label"
    t.string   "name"
    t.string   "style"
    t.boolean  "mix"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "artist_id"
  end

  add_index "tunes", ["artist_id"], name: "index_tunes_on_artist_id"

end

私のフォーム

<%= simple_form_for @tune, :html => { :class => 'form-horizontal' } do |f| %>
  <%= f.input :name %>
  <%= f.input :soundcloud_link %>
  <%= f.input :download_label %>
  <%= f.input :download_link %>
  <%= f.input :mix %>
  <%= f.association :artist%>
  <%= f.button :submit %>
<% end %>
4

1 に答える 1

0

以下を Tune モデルに追加します

 accepts_nested_attributes_for :tunes

次に、コントローラーに強力なパラメーターを追加します。

params.require(:artist).permit( :id, :name, ... , tune_attributes: [:id, :name, :real_name, ...] )

注:Rails 4を使用していると仮定しています

于 2013-10-27T21:27:52.543 に答える