1
votes

New to RoR. I am making a simple blog to post music entries. I have two models Tune and Artist where a tune belongs_to an artist. I am using simple_form to render my form. The problem I am running into is that the simple_form is pulling up the correct associations but when I click submit it is not saving the association correctly and the validation isn't passing. Code is below:

Classes

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

class Artist < ActiveRecord::Base
  has_many :tunes
end

Database schema

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

My form

<%= 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 %>
1

1 Answers

0
votes

Add following to your Tune model

 accepts_nested_attributes_for :tunes

and then add strong params to your controller, e.g.

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

NOTE: I am assuming your are using rails 4