RubyonRails参数将receiver和sender设置为users_id



有一个接收方和发送方,这两个类对于rails中的消息传递系统都是相同的类(Message)。想要为两者设置参数,即如果用户创建了一个消息,则默认情况下发送方为user_id,接收方为从用户联系人列表中选择的联系人。

目前,数据库只接收到receivient_id列的user_id,这是错误的,应该是sender_id列。Sender_id什么也没收到。

看完后,有些人说不要修改参数,因为这是不好的做法。因此,在消息视图中设置一个隐藏字段(如正文和标题),但这并不会进入数据库。

有两个问题,这个过程是一个合适的轨道实践吗?(作为新手问这个问题)如果不是:你能建议另一条路径或方向吗?如果是:有什么想法/想法为什么不保存到数据库中?

用户模型

class User < ActiveRecord::Base
  has_many :messages, class_name: "Message", foreign_key: "recipient_id"
  has_many :sent_messages, class_name: "Message", foreign_key: "sender_id"
  has_many :contacts, dependent: :destroy
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
  validates_presence_of :firstname, allow_blank: false
  validates_presence_of :surname, allow_blank: false
end

消息模型

class Message < ActiveRecord::Base
  belongs_to :sender, class_name: "User", foreign_key: "sender_id"
  belongs_to :recipient, class_name: "User", foreign_key: "recipient_id"
    validates_presence_of :body, :title
end

消息控制器

class MessagesController < ApplicationController
  before_action :message, only: [:show]
  before_action :authenticate_user!
  def index
    @messages = current_user.messages
  end
  def new
    @message = Message.new
  end
  def create
    current_user.messages.create(message_params)
    redirect_to '/messages'
  end
  def show
  end
  private
  def message_params
    params.require(:message).permit(:title, :body, :sender_id, :recipient_id)
  end
  def message
    @message = Message.find(params[:id])
  end
end

消息/新视图

<%= form_for @message do |f| %>
     <%= hidden_field_tag :sender_id, current_user.id %>
     <%= f.text_field :title %>
     <%= f.text_field :body %>
     <%= f.submit %>
<% end %>

模式

ActiveRecord::Schema.define(version: 20160517131719) do
  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"
  create_table "contacts", force: :cascade do |t|
    t.string   "firstname"
    t.string   "surname"
    t.string   "email"
    t.integer  "phone"
    t.datetime "created_at",         null: false
    t.datetime "updated_at",         null: false
    t.string   "image_file_name"
    t.string   "image_content_type"
    t.integer  "image_file_size"
    t.datetime "image_updated_at"
    t.integer  "user_id"
  end
  add_index "contacts", ["user_id"], name: "index_contacts_on_user_id", using: :btree
  create_table "messages", force: :cascade do |t|
    t.string   "title"
    t.text     "body"
    t.integer  "sender_id"
    t.integer  "recipient_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end
  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.inet     "current_sign_in_ip"
    t.inet     "last_sign_in_ip"
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
    t.string   "firstname"
    t.string   "surname"
  end
  add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
  add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
  add_foreign_key "contacts", "users"
end

尝试将您的表单更改为:

<%= form_for @message do |f| %>
  <%= f.hidden_field :sender_id, value: current_user.id %> 
  <%= f.text_field :title %>
  <%= f.text_field :body %>
  <%= f.submit %>
<% end %>

目前,数据库只接收到receivent_id的user_id列,该列是错误的,应该是sender_id列。

在您的create操作中,您有current_user.messages.create(message_params)。这将在DB中创建一个message记录,其中外键的(即,在您的情况下为recipient_id)值为parent's(user) id。这就是recipient_id获取用户id的值的原因。

Sender_id什么也没收到。

这是因为为sender_id设置的hidden_field没有用表单生成器实例包装。您需要更改

<%= hidden_field_tag :sender_id, current_user.id %>

<%= f.hidden_field :sender_id, current_user.id %>

最新更新