1
votes

I am building this page where teachers (a kind of user) can edit settings of their students. Every student can tell their teacher what instrument they play and teachers can let their students know what chords they have mastered.

Student page P.S. friend = student

= form_for :friend_instrument do |f|
  %br
  %h4.media-heading.text-post.search-result.center Basic chords
  %hr
  .half
    = f.label :c, "C chord"
    = f.select :c, [['No', false], ['Yes', true]], {}, class: 'form-control'
  .half
    = f.label :d, "D chord"
    = f.select :d, [['No', false], ['Yes', true]], {}, class: 'form-control'
  .half
    = f.label :e, "E chord"
    = f.select :e, [['No', false], ['Yes', true]], {}, class: 'form-control'
  .half
    = f.label :f, "F chord"
    = f.select :f, [['No', false], ['Yes', true]], {}, class: 'form-control'
  .half
    = f.label :g, "G chord"
    = f.select :g, [['No', false], ['Yes', true]], {}, class: 'form-control'
  .half
    = f.label :a, "A chord"
    = f.select :a, [['No', false], ['Yes', true]], {}, class: 'form-control'
  .half
    = f.label :b, "B chord"
    = f.select :b, [['No', false], ['Yes', true]], {}, class: 'form-control'                               
  = f.submit "Update #{instrument.name} chords #{friend.full_name} mastered", class: 'btn btn-primary form-control'

user_controller

  def students
    @instrument = Instrument.all
  end
  private
  def user_attributes
    params.require(:user).permit(:profile_picture, :cover_photo, :username, :full_name, :facebook_url, :featured_song, :soundcloud_url, :twitter_url, :location, :date_of_birth, :about, :website, :phone, :terms, :playlist_id, :subscribed, :password, :sort_user, :school_is, instrument_ids: [])
  end

user model

  has_many :user_instruments, dependent: :destroy
  has_many :instruments, through: :user_instruments
  accepts_nested_attributes_for :instruments

instrument model

class Instrument < ActiveRecord::Base
  has_many :user_instruments
  has_many :users, through: :user_instruments

  has_many :meeting_instruments
  has_many :meetings, through: :meeting_instruments
end

user instrument model

class UserInstrument < ActiveRecord::Base
  belongs_to :user
  belongs_to :instrument
end

I tried to add user_instruments_attributes but no luck! Any help would be much appreciated.

1
you put accepts_nested_attributes_for :user_instruments on user? And, to make sure, you have b, c, d, ect. boolean columns on user_instrument?Mark Swardstrom
Thx for the reply, I will add that I had "accepts_nested_attributes_for :instruments allready"Chris Kroon
And yes, this is my migration files for the chords: class AddChordsToIntruments < ActiveRecord::Migration def change add_column :instruments, :c, :boolean, default: false add_column :instruments, :d, :boolean, default: false add_column :instruments, :e, :boolean, default: false add_column :instruments, :f, :boolean, default: false add_column :instruments, :g, :boolean, default: false add_column :instruments, :a, :boolean, default: false add_column :instruments, :b, :boolean, default: false end endChris Kroon
not instruments in the user class, user_instruments. You need accepts nested attributes_for :instrument in the user_instrument class. It only goes one level, it doesn't go through.Mark Swardstrom
Thx again, then I get this error: 'No association found for name `instruments'. Has it been defined yet?'Chris Kroon

1 Answers

0
votes

You want to keep that on the students side of things. The change you want to make is to allow teachers to update the attributes of students. You want to use cancan or access granted (I like access granted) to set who can update what.