gpt4 book ai didi

ruby-on-rails - simple_form 不更新关系

转载 作者:数据小太阳 更新时间:2023-10-29 08:44:49 25 4
gpt4 key购买 nike

我正在尝试使用 simple_form 更新用户权限。当表单加载时,当前值被正确选择。但是在进行更改时,这些更改不会持久保存到数据库中。我正在使用 app_ids 分配权限。如果我在 Rails 控制台中做同样的事情,它似乎工作正常。

rails 控制台

u = User.find 3
u. app_ids = [1,3,4]
u.save
# create the user_subscriptions

app/controllers/users_controller.rb

      def update
@user = User.find(params[:id])
# binding.pry
authorize @user
if @user.update_attributes(user_params)
flash[:notice] = "Details for #{@user.name} updated."
redirect_to users_path
else
render :edit
end
end

def user_params
params.require(:user).permit(:name, :role, :app_tokens, :provider, :uid, :app_ids)
end

app/views/users/_form.html.slim

= simple_form_for(user, html: { class: 'form-horizontal' }) do |form|
.form-group.full-name.pull-left
= form.label "Full Name"
= form.text_field :name, placeholder: "Enter name", class: "form-control"
= hidden_field_tag "uid", user.id
p
= form.label "User Level"
.btn-group.user-level.pull-left[data-toggle="buttons-radio"]
= form.input :role, collection: User.roles, as: :radio_buttons, item_wrapper_class: 'btn btn-default', checked: User.roles[user.role], required: true
.form-group
.btn-group.btn-group-lg.app-access data-toggle="buttons"
= form.input :app_ids, collection: App.order(:name), as: :check_boxes, item_wrapper_class: 'btn btn-default'

.text-center.edit-user-submit.actions
= link_to "Cancel", users_path, class: "btn btn-primary btn-sm pull-right"
= form.button :submit, "Save Changes", data: {disable_with: "Updating ..."}, class: "btn btn-default btn-sm pull-right"

app/models/user.rb

class User < ActiveRecord::Base
has_many :user_subscriptions, dependent: :delete_all
has_many :apps, through: :user_subscriptions

after_save :update_apps_access

devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :omniauthable
attr_reader :app_tokens

enum role: [:super_admin, :admin, :generic]
self.per_page = 10

def app_tokens=(ids)
self.app_ids = ids.split(',')
end

def role=(value)
if value.is_a?(String) && value.to_i.to_s == value
super value.to_i
else
super value
end
end

def update_apps_access
return unless role == 'admin' || role == 'super_admin'
App.all.find_each { |app| user_subscriptions.find_or_create_by(app_id: app.id) }
end

def self.from_omniauth(access_token)
data = access_token.info
user = User.find_by(email: data['email'])
user = User.create(
name: data['name'],
email: data['email'],
oauth_token: access_token['credentials']['token'],
uid: access_token['uid'],
provider: access_token['provider']
) unless user
user
end
end

app/models/user_subscription.rb

class UserSubscription < ActiveRecord::Base
belongs_to :user
belongs_to :app
end

最佳答案

由于有多个 ID,您需要像这样允许数组:

def user_params
params.require(:user).permit(:name, :role, :app_tokens, :provider, :uid, app_ids: [])
end

关于ruby-on-rails - simple_form 不更新关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32699686/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com