我希望能够通过在表单中输入其电子邮件地址来分配用户主持人权限。我目前可以通过Rails控制台将主持人状态分配给用户,但是我不确定如何在实际页面上执行此操作。
我当前的设置如下: 有多个硬币页面。用户可以主持多个“硬币”页面,但“硬币”页面只能有一个主持人。因此,现在将关联设置为:
硬币:
belongs_to :moderator, class_name: "User", :foreign_key => "moderator_id"
用户:
has_many :moderated_coins, class_name: "Coin", :foreign_key => "moderator_id"
然后在控制台中,我可以使用
分配主持人状态@coin.moderator = @user
我想做的是在Coin页面上有一个表单,管理员用户可以在其中输入某些用户的电子邮件地址,然后它将找到该用户并将@ user.moderator设置为true。
所以在我的(硬币)show.html.erb中,我想要类似的东西
<% if current_user.admin? %>
# enter a user's email in form -> get user id
# user.moderator = true
<% end %>
我不确定该怎么做,将不胜感激。
coin.rb
class Coin < ApplicationRecord
validates :currency_name, presence: true
has_many :questions, dependent: :destroy
has_many :events, dependent: :destroy
has_many :links, dependent: :destroy
mount_uploader :picture, PictureUploader
has_and_belongs_to_many :genres
belongs_to :moderator, class_name: "User", :foreign_key => "moderator_id", optional: true
end
user.rb
class User < ApplicationRecord
acts_as_votable
has_many :questions, dependent: :destroy
has_many :events, dependent: :destroy
has_many :links, dependent: :destroy
has_many :posts, dependent: :destroy
has_many :moderated_coins, class_name: "Coin", :foreign_key => "moderator_id"
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable,
:validatable, authentication_keys: [:login]
validates :username, presence: :true, uniqueness: { case_sensitive: false }
validates_format_of :username, with: /^[a-zA-Z0-9_\.]*$/, :multiline => true
validate :validate_username
def validate_username
if User.where(email: username).exists?
errors.add(:username, :invalid)
end
end
def login=(login)
@login = login
end
def login
@login || self.username || self.email
end
def self.find_for_database_authentication(warden_conditions)
conditions = warden_conditions.dup
if login = conditions.delete(:login)
where(conditions.to_h).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first
elsif conditions.has_key?(:username) || conditions.has_key?(:email)
where(conditions.to_h).first
end
end
end
coins_controller.rb
class CoinsController < ApplicationController
load_and_authorize_resource param_method: :question_params
before_action :find_coin, only: [:edit, :update, :destroy ]
before_action :authenticate_user!, except: [:index, :create, :show]
def index
@search = Coin.ransack(params[:q])
@coins = @search.result(distinct: true)
end
def new
@coin = Coin.new
end
def create
@coin = Coin.new(coin_params)
if @coin.save!
flash[:success] = "Coin created"
redirect_to @coin
else
render 'new'
end
end
def show
@coin = Coin.find(params[:id])
end
def edit
authorize! :update, @coin
end
def update
if @coin.update(coin_params)
redirect_to @coin
else
render 'edit'
end
end
def destroy
Coin.find(params[:id]).destroy
redirect_to coins_url
end
def get_moderator
if @coin.moderator
@coin.moderator
end
end
private
def coin_params
params.require(:coin).permit( :currency_name, :currency_abbrev, :working_product, :founder, :mineable, :moderator_id, genre_ids:[])
end
def find_coin
@coin = Coin.find(params[:id])
end
end
users_controller.rb
class UsersController < ApplicationController
before_action :authenticate_user!
def show
@user = User.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @user }
end
end
end
来自schema.rb
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "username"
t.string "wallet"
t.boolean "admin", default: false
t.boolean "moderator", default: false
t.decimal "currentbalance", precision: 8, scale: 2
t.decimal "payout_to_date", precision: 8, scale: 2
t.text "bio"
t.string "link1"
t.string "link2"
t.string "link3"
t.string "link4"
t.string "link5"
t.string "name"
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
t.index ["username"], name: "index_users_on_username", unique: true
end
create_table "coins", force: :cascade do |t|
t.string "link_name"
t.string "currency_name"
t.string "currency_abbrev"
t.float "volume_usd"
t.float "volume_btc"
t.string "picture"
t.text "question1"
t.text "question2"
t.text "question3"
t.text "question4"
t.text "question5"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "permalink"
t.string "genre"
t.integer "moderator_id"
t.boolean "accepted", default: false
t.datetime "accepted_at"
t.integer "genre_id"
end
答案 0 :(得分:1)
您可以通过在Coin模型上为主持人的电子邮件使用自定义属性来实现。我的自定义属性意思是在您的attr_accessor :moderator_email
模型中添加一个Coin
,该模型未链接到任何数据库列,而只是用于临时检索正确的用户。例如:
class Coin < ApplicationRecord
# Add the following
attr_accessor :moderator_email
before_save do
if moderator_email.present?
self.moderator = User.find_by_email(moderator_email) # Tip: use find_by_email! (with the !) if you want the save cancelled when email is not found
end
end
end
有了这个,您应该可以在表单中添加<%= f.text_field :moderator_email %>
。
此外,不要忘记将:moderator_email
添加到控制器中的coin_params
中。但是请记住,如果您不希望所有用户都能够更新它,则需要添加一些额外的检查来验证这一点。我将调整coin_params
方法,使其看起来像这样:
def coin_params
if current_user.try(:admin?)
params.require(:coin).permit( :moderator_email, :currency_name, :currency_abbrev, :working_product, :founder, :mineable, :moderator_id, genre_ids:[])
else
params.require(:coin).permit( :currency_name, :currency_abbrev, :working_product, :founder, :mineable, :moderator_id, genre_ids:[])
end
end