简体   繁体   English

在Ruby on Rails中为其他用户进行保存时如何保存参数

[英]How to save params when your saving for a different user in Ruby on Rails

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 PS friend = student 学生页面 PS朋友=学生

= 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 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! 我试图添加user_instruments_attributes,但没有运气! Any help would be much appreciated. 任何帮助将非常感激。

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. 您想使用cancan或访问权限授予 (我喜欢授予访问权限)来设置谁可以更新内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何保护您的参数在轨道上? - how to guard your params in rails? [Ruby on Rails]如何通过不同的控制器视图维护URL参数? - [Ruby on rails]How to maintain URL params trough different controller views? Rails将参数传递给其他模型,然后将其保存不起作用 - Rails passing params to different model and then saving it not working Ruby on Rails-何时使用params.permit! 以及如何更换 - Ruby on Rails - When to use params.permit! and how to replace it Ruby on Rails教程第7.21章,参数[:user] - Ruby on Rails Tutorial chapter 7.21, params[:user] 按原样保存用户输入 - Ruby on Rails - Save user input as it is - Ruby on Rails 在Ruby on Rails中调用模型上的保存而不实际保存它 - Calling save on a model in Ruby on Rails not actually saving it 在Ruby on Rails中,如何解决“用户必须具有地址”和“地址必须具有用户”,以便他们在保存时无法验证? - In Ruby on Rails, how to solve User must have Address and Address must have User so they can't validate when saving? 如何在Rails上使用ruby在一次操作中保存不同的表值 - How to save different table values in single action using ruby on rails 如何允许用户保存模型而不发布模型-Rails上的Ruby - How to allow a user to save a model without publishing it - ruby on rails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM