Ruby on Rails 参数设置收件人和发件人 users_id
Ruby on Rails Params set recipient and sender with users_id
rails 中的消息系统有一个收件人和发件人,两者都是相同的 class(Message)。想要为两者设置参数,即如果用户创建消息,默认情况下发件人是 user_id,收件人将是从用户联系人列表中选择的联系人。
目前数据库只接收到 recipient_id 列的 user_id,这是错误的,应该是 sender_id 列。 Sender_id 什么也没收到。
看完后,有人说不要修改参数,因为这是不好的做法。因此,在消息视图中设置一个隐藏字段(如 body 和标题),但这并没有推送到数据库中。
两个问题,这个过程是否合适 rails 实践? (问这个作为rails的新手)如果不是:你能建议另一条路或方向吗?如果是:任何 ideas/thoughts 为什么这没有保存到数据库中?
用户模型
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
message/new 查看
<%= 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 %>
Currently the database is only receiving a user_id to the recipient_id
column which is wrong and should be to sender_id column.
在您的 create
操作中,您有 current_user.messages.create(message_params)
。这会在 DB 中使用 外键的 (即 recipient_id
在你的例子中)值与 parent's(user) id
。就是这个原因,recipient_id
得到的值是user's id.
Sender_id receives nothing.
这是因为 sender_id
的 hidden_field
集没有用 form builder 实例包装。你需要改变
<%= hidden_field_tag :sender_id, current_user.id %>
至
<%= f.hidden_field :sender_id, current_user.id %>
rails 中的消息系统有一个收件人和发件人,两者都是相同的 class(Message)。想要为两者设置参数,即如果用户创建消息,默认情况下发件人是 user_id,收件人将是从用户联系人列表中选择的联系人。
目前数据库只接收到 recipient_id 列的 user_id,这是错误的,应该是 sender_id 列。 Sender_id 什么也没收到。
看完后,有人说不要修改参数,因为这是不好的做法。因此,在消息视图中设置一个隐藏字段(如 body 和标题),但这并没有推送到数据库中。
两个问题,这个过程是否合适 rails 实践? (问这个作为rails的新手)如果不是:你能建议另一条路或方向吗?如果是:任何 ideas/thoughts 为什么这没有保存到数据库中?
用户模型
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
message/new 查看
<%= 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 %>
Currently the database is only receiving a user_id to the recipient_id column which is wrong and should be to sender_id column.
在您的 create
操作中,您有 current_user.messages.create(message_params)
。这会在 DB 中使用 外键的 (即 recipient_id
在你的例子中)值与 parent's(user) id
。就是这个原因,recipient_id
得到的值是user's id.
Sender_id receives nothing.
这是因为 sender_id
的 hidden_field
集没有用 form builder 实例包装。你需要改变
<%= hidden_field_tag :sender_id, current_user.id %>
至
<%= f.hidden_field :sender_id, current_user.id %>