所以我的问题大致如下。我有一个具有以下类的rails应用程序:
每个用户都可以拥有多种技能,它们是从用户配置文件屏幕中添加的,这是用户控制器的显示操作。目前,我有技能设置和能够被创建,但我不能添加一个预先构建的技能到用户配置文件。当我尝试提交时,我得到以下错误Empty list of attributes to change
以下是我对用户操作的添加技巧:
def add_skill_to_user
user = User.find(params[:id])
user.skills.create(skill_params) #skill name, level...
@skills_options = Skill.all.map{|s| [ s.name, s.id] }
#whatever happens when this is is done, redirect, json answer etc...
if user.skills.update_all(skill_params)
flash[:success] = "Skill Added"
else
render 'add_skill_to_user'
end
end
private
# Set skills params whitelist
def skill_params
params.permit(:name, :user_id)
end 和下面的路线
post 'users/:id/add_skill_to_user' => 'users#add_skill_to_user'这是我的表格
<%= form_tag({controller: "users", action: "add_skill_to_user"}, method: "put") do %>
<%= collection_select(:skill, :name, Skill.all, :id, :name) %>
<%= submit_tag 'Submit' %>
<%end%>请告诉我您需要的任何其他信息。
User.rb
class User < ActiveRecord::Base
attr_accessor :remember_token, :activation_token, :reset_token
before_save :downcase_email
before_create :create_activation_digest
belongs_to :group
has_many :ranks
has_many :skills
has_many :mission_notes
has_and_belongs_to_many :training_events
accepts_nested_attributes_for :skills
validates :username, presence: true
validates :email, presence: true
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
validates :group_id, presence: true
has_secure_password
validates :password, length: { minimum: 6 }, allow_blank: true
# Returns the hash digest of the given string.
def User.digest(string)
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
BCrypt::Engine.cost
BCrypt::Password.create(string, cost: cost)
end
# Returns a random token.
def User.new_token
SecureRandom.urlsafe_base64
end
# Remembers a user in the database for use in persistent sessions.
def remember
self.remember_token = User.new_token
update_attribute(:remember_digest, User.digest(remember_token))
end
# Returns true if the given token matches the digest.
def authenticated?(remember_token)
return false if remember_digest.nil?
BCrypt::Password.new(remember_digest).is_password?(remember_token)
end
# Forgets a user.
def forget
update_attribute(:remember_digest, nil)
end
# Converts email to all lower-case.
def downcase_email
self.email = email.downcase
end
# Creates and assigns the activation token and digest.
def create_activation_digest
self.activation_token = User.new_token
self.activation_digest = User.digest(activation_token)
end
# Returns true if the given token matches the digest.
def authenticated?(attribute, token)
digest = send("#{attribute}_digest")
return false if digest.nil?
BCrypt::Password.new(digest).is_password?(token)
end
# Activates an account.
def activate
update_attribute(:activated, true)
update_attribute(:activated_at, Time.zone.now)
end
# Sends activation email.
def send_activation_email
UserMailer.account_activation(self).deliver
end
# Sets the password reset attributes.
def create_reset_digest
self.reset_token = User.new_token
update_attribute(:reset_digest, User.digest(reset_token))
update_attribute(:reset_sent_at, Time.zone.now)
end
# Sends password reset email.
def send_password_reset_email
UserMailer.password_reset(self).deliver
end
def password_reset_expired?
reset_sent_at < 2.hours.ago
end
endSkill.rb
class Skill < ActiveRecord::Base
belongs_to :user
end发布于 2014-12-15 01:41:46
您的collection_select将生成类似于
<select name="skill[name]">
<option value="1">First Skill</option>
...
</select>其中"skill[name]"没有反映选项值中的内容(技能ids)。
此外,params白名单并不处理由select生成的POST数据是一个数组这一事实。
我建议
<%= collection_select(:skill, :id, Skill.all, :id, :name) %>在控制器中使用此白名单筛选器:
params.require(:skill).permit(:id)但是您的add_skill_to_user方法令人困惑。如果您想要做的只是将预先存在的技能分配给用户,这有一个RailsCast。
https://stackoverflow.com/questions/27474753
复制相似问题