当我转到用户个人资料页面时,如何让它自动提取当前用户名。就像我点击“给我发消息”按钮而不是拉动网站上的所有用户,我想让它自动拉动我正在看的用户。
现在当我按下“给我发消息”按钮时,它会拉起所有用户,我必须选择发送消息的用户。我能做什么?我可以使用<%= user.name %>
来提升用户,但这对我没有帮助。
<button type="button" class="btn btn-info" data-toggle="modal" data-target="#exampleModal" data-whatever="<%= @user.name%>">Message Me</button>
<%= f.collection_select (:recipients, User.all.collect {|p| [ p.name, p.id ] }, {}, { multiple: true , class: "chosen-select form-control" })%>
完整代码:
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<%= form_for :conversation, url: :conversations, html: { class: "" } do |f| %>
<div class="panel2-signup">
<div class="panel-body-signup">
<div class="modal-body">
<form>
<div class="form-group">
To <%= f.collection_select (:recipients, User.all.collect {|p| [ p.name, p.id ] }, {}, { multiple: true , class: "chosen-select form-control" })%>
Subject:
<%= f.text_field :subject, class: "form-control" %>
Message:
<%= f.text_area :body, class: 'form-control', placeholder: "Type your message here", rows: 4 %>
</div>
</div>
这是db info
class CreateMailboxer < ActiveRecord::Migration
def self.up
#Tables
#Conversations
create_table :mailboxer_conversations do |t|
t.column :subject, :string, :default => ""
t.column :created_at, :datetime, :null => false
t.column :updated_at, :datetime, :null => false
end
#Receipts
create_table :mailboxer_receipts do |t|
t.references :receiver, :polymorphic => true
t.column :notification_id, :integer, :null => false
t.column :is_read, :boolean, :default => false
t.column :trashed, :boolean, :default => false
t.column :deleted, :boolean, :default => false
t.column :mailbox_type, :string, :limit => 25
t.column :created_at, :datetime, :null => false
t.column :updated_at, :datetime, :null => false
end
#Notifications and Messages
create_table :mailboxer_notifications do |t|
t.column :type, :string
t.column :body, :text
t.column :subject, :string, :default => ""
t.references :sender, :polymorphic => true
t.column :conversation_id, :integer
t.column :draft, :boolean, :default => false
t.string :notification_code, :default => nil
t.references :notified_object, :polymorphic => true
t.column :attachment, :string
t.column :updated_at, :datetime, :null => false
t.column :created_at, :datetime, :null => false
t.boolean :global, default: false
t.datetime :expires
end
#Indexes
#Conversations
#Receipts
add_index "mailboxer_receipts","notification_id"
#Messages
add_index "mailboxer_notifications","conversation_id"
#Foreign keys
#Conversations
#Receipts
add_foreign_key "mailboxer_receipts", "mailboxer_notifications", :name => "receipts_on_notification_id", :column => "notification_id"
#Messages
add_foreign_key "mailboxer_notifications", "mailboxer_conversations", :name => "notifications_on_conversation_id", :column => "conversation_id"
end
def self.down
#Tables
remove_foreign_key "mailboxer_receipts", :name => "receipts_on_notification_id"
remove_foreign_key "mailboxer_notifications", :name => "notifications_on_conversation_id"
#Indexes
drop_table :mailboxer_receipts
drop_table :mailboxer_conversations
drop_table :mailboxer_notifications
end
end
ConversationsController.rb
class ConversationsController < ApplicationController
before_action :authenticate_user!
before_action :get_mailbox
def new
end
def index
end
def create
recipients = User.where(id: conversation_params[:recipients])
if
conversation = current_user.send_message(recipients, conversation_params[:body], conversation_params[:subject]).conversation
flash[:notice] = "Your message was successfully sent!"
redirect_to conversation_path(conversation)
else
flash[:alert] = "Error. Message was not created"
redirect_to new_conversation_path
end
end
def show
@receipts = conversation.receipts_for(current_user).order("created_at ASC")
# mark conversation as read
conversation.mark_as_read(current_user)
end
def reply
current_user.reply_to_conversation(conversation, message_params[:body])
flash[:notice] = "Your reply message was successfully sent!"
redirect_to conversation_path(conversation)
end
def trash
conversation.move_to_trash(current_user)
redirect_to mailbox_inbox_path
end
def untrash
conversation.untrash(current_user)
redirect_to mailbox_inbox_path
end
def delete
@mailbox.trash.each do |conversation|
conversation.receipts_for(current_user).update_all(deleted: true)
end
flash[:notice] = 'Your trash was cleaned!'
redirect_to mailbox_inbox_path
end
private
def conversation_params
params.require(:conversation).permit(:subject, :body,recipients:[])
end
def get_mailbox
@mailbox ||= current_user.mailbox
if @user = current_user
@post = current_user.posts.build
else
end
if @user = current_user
@post = current_user.posts.build
@purchased = Sale.where(buyer_email: current_user.email).order("created_at DESC").order("created_at DESC").paginate(page:params[:page], per_page: 1 )
@sales = Sale.where(seller_email: current_user.email).order("created_at DESC").order("created_at DESC").paginate(page:params[:page], per_page: 1 )
else
end
end
def message_params
params.require(:message).permit(:body, :subject)
end
end
答案 0 :(得分:1)
查看您的操作过滤器,看起来您正在使用devise
gem进行用户管理。
要在设计中获取当前经过身份验证的用户,您可以使用辅助方法current_user
。