- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对 Rails 很陌生,所以请在您的回复中详细说明。我正在构建一个使用设计进行身份验证的网络应用程序。我现在坚持的部分是用户到用户的消息传递系统。这个想法是用户 A 登录应用程序并可以访问用户 B 的个人资料,并且在用户 B 的个人资料上可以单击允许用户 A 向用户 B 撰写消息的链接。然后用户 B 可以登录应用程序并访问将在其中找到用户 A 的消息的收件箱。
我相信我在这里定义发件人和收件人角色时遇到了麻烦,现在我正在尝试显示用户将在其中撰写邮件的表单。谁能看到我在这里做错了什么?我收到以下错误。我已经读到要做的事情是将 User_id 字段添加到表中,但我希望使用 sender_id 和 recipient_id 将这些消息链接起来,它们都等于 user_id(例如,用户 1[sender] 向用户 2 发送消息[接受者]):
未知属性:user_id
定义新
@message = current_user.messages.new 收件人 ID:参数 [:发件人 ID]
结尾
此外,对于您的铁路专家或做过类似事情的任何人,您能否建议我是否朝着正确的方向前进,或提供任何指导?我在这里有点编码盲,只是想在我前进的过程中弥补它。任何指导将不胜感激,并为我节省大量时间,我敢肯定。下面的代码:
用户迁移
class DeviseCreateUsers < ActiveRecord::Migration
def change
create_table(:users) do |t|
t.string :first_name
t.string :last_name
t.string :email, null: false, default: ""
t.string :encrypted_password, null: false, default: ""
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.timestamps
end
add_index :users, :email, unique: true
add_index :users, :reset_password_token, unique: true
end
end
class CreateMessages < ActiveRecord::Migration
def change
create_table :messages do |t|
t.string :content
t.integer :sender_id
t.integer :recipient_id
t.timestamps
end
end
end
ActiveRecord::Schema.define(version: 20140909174718) do
create_table "messages", force: true do |t|
t.string "content"
t.integer "sender_id"
t.integer "recipient_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "users", force: true do |t|
t.string "first_name"
t.string "last_name"
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"
t.datetime "updated_at"
t.string "current_industry"
t.integer "years_in_current_industry"
t.string "hobbies"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
Catalyst::Application.routes.draw do
devise_for :users, :controllers => { :registrations => "registrations" }
devise_scope :user do
get 'register', to: 'devise/registrations#new'
get 'login', to: 'devise/sessions#new', as: :login
get 'logout', to: 'devise/sessions#destroy', as: :logout
end
resources :users do
member do
get 'edit_profile'
end
resources :messages, only: [:new, :create]
end
resources :messages, only: [:index, :show, :destroy]
root to: "home#index"
match '/about', to: 'static_pages#about', via: 'get'
match '/contact', to: 'static_pages#contact', via: 'get'
match '/help', to: 'static_pages#help', via: 'get'
match '/legal', to: 'static_pages#legal', via: 'get'
end
class UsersController < ApplicationController
before_filter :authenticate_user!
def index
@users = User.all
end
def show
@user = User.find(params[:id])
end
def new
end
def create
end
def edit
end
def update
@user = User.find(params[:id])
@user.update!(user_params)
redirect_to @user
end
def destroy
end
def edit_profile
@user = User.find(params[:id])
end
def user_params
params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :current_industry, :years_in_current_industry, :hobbies)
end
def sender
@user = User.find(params[:id])
end
def recipient
@user = User.find(params[:id])
end
end
class MessagesController < ApplicationController
before_action :set_recipient
def new
@message = Message.new
@recipient = User.find(params[:user_id])
end
def create
@message = Message.new message_params
if @message.save
flash[:success] = "Your message has been sent!"
redirect_to user_messages_path
else
flash[:failure] = "Please try again."
redirect_to users_path
end
end
private
def message_params
params.require(:message).permit(:content, :sender_id, :recipient_id)
end
end
class User < ActiveRecord::Base
has_many :from_messages, class_name: 'Message', :foreign_key => "sender_id"
has_many :to_messages, class_name: 'Message', :foreign_key => "recipient_id"
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :first_name, :last_name, :email, :password, :password_confirmation, :remember_me, :current_industry, :years_in_current_industry, :hobbies
end
class Message < ActiveRecord::Base
belongs_to :sender, class_name: "User"
belongs_to :recipient, class_name: "User"
validates :content, presence: true, length: { maximum: 500 }
validates :sender_id, presence: true
validates :recipient_id, presence: true
end
<h2>Inbox</h2>
<h1>Create Message</h1>
<%= form_for [@recipient, @message] do |f| %>
<%= f.hidden_field :recipient_id, value: @recipient.id %>
<%= f.label "Enter your message below" %><br />
<%= f.text_area :content %>
<%= f.submit "Send" %>
<% end %>
user_messages POST /users/:user_id/messages(.:format) messages#create
new_user_message GET /users/:user_id/messages/new(.:format) messages#new
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
messages GET /messages(.:format) messages#index
message GET /messages/:id(.:format) messages#show
DELETE /messages/:id(.:format) messages#destroy
最佳答案
型号
#app/models/user.rb
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"
end
#app/models/message.rb
class Message < ActiveRecord::Base
belongs_to :recipient, class_name: "User", foreign_key: "recipient_id"
belongs_to :sender, class_name: "User", foreign_key: "sender_id"
scope :unread, -> { where read: false }
end
#app/controllers/messages_controller.rb
class MessagesController < ApplicationController
before_action :set_recipient, only: [:new, :create]
def new
@message = current_user.sent_messages.new
end
def create
@message = current_user.sent_messages.new message_params
@message.recipient_id = @recipient.id
@message.save
end
def index
@messages = current_user.messages
end
def destroy
@message = current_user.messages.destroy params[:id]
end
def show
@message = current_user.messages.find params[:id]
end
private
def message_params
params.require(:message).permit(:content, :recipient_id, :sender_id)
end
def set_recipient
@recipient = User.find params[:user_id]
end
end
#config/routes.rb
devise_for :users, path: "", controllers: { :registrations => "registrations" }, path_names: {sign_up: "register", sign_in: "login", sign_out: "logout"}
resources :users do
get :profile
resources :messages, only: [:new, :create] #-> domain.com/users/:user_id/messages/new
end
resources :messages, only: [:index, :show, :destroy] #-> domain.com/messages/:id
#app/views/users/show.html.erb (user to send message to)
<%= link_to "Send Message", user_messages_path(@user.id) %>
#app/views/messages/new.html.erb
<%= form_for [@recipient, @user] do |f| %>
<%= f.text_field :content %>
<%= f.submit %>
<% end %>
#app/views/messages/index.html.erb
<h2>Inbox</h2>
<% @messages.each do |message| %>
<%= message.content %>
<% end %>
I've read that the thing to do is add the User_id field to the table, but I'm hoping to link this messages up using sender_id and recipient_id, which both equal user_id (e.g. User 1[sender] sends a message to User 2 [recipient])
user_id
到你的 table 。
user_id
只是一个
foreign_key
,您已在
models
中覆盖它.
recipient_id
和
sender_id
, 我们在
create
方法:
def create
@message = current_user.message.new message_params
@message.recipient_id = @recipient.id
@message.save
end
sender_id
外键调用
current_user.messages
.如果您调用
Message.new
,这将是一个完全不同的故事(必须设置
sender_id
)
@recipient
您在
before_action
中设置的变量方法给我们
id
对于
recipient_id
.
inverse_of
除非您尝试访问子/嵌套模型中的“父”模型数据。
Message
模型完全独立于您的
User
.这是通过您的设置实现的,允许您创建所需的各种对象。
Ancestry
或
Closure_Tree
Ancestry
或
Closure_Tree
),因为它们为您提供了您可以调用您的项目的“方法”。它们通过在您的数据库中创建多个列来工作,并在您保存/创建所需的对象时填充它们
Recursion is the process of repeating items in a self-similar way. For instance, when the surfaces of two mirrors are exactly parallel with each other, the nested images that occur are a form of infinite recursion.
- Make sure you save your objects with the correct parents
- To display the "threaded" conversation, loop through those parents
- Use recursion to loop through their children
ancestry
gem,它存储层次结构与
closure_tree
略有不同此后我们发现的 gem(打算很快使用闭包树 gem)。
#app/views/comments/index.html.erb
<%= render partial: "comments", locals: { collection: @comments } %>
#app/comments/_comments.html.erb
<% collection.arrange.each do |comment, sub_item| %>
<%= link_to comment.title, comment_path(comment) %>
<% if category.has_children? %>
<%= render partial: "category", locals: { collection: category.children } %>
<% end %>
<% end %>
关于ruby-on-rails - Rails 用户到用户消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25752438/
我一直在读到,如果一个集合“被释放”,它也会释放它的所有对象。另一方面,我还读到,一旦集合被释放,集合就会释放它的对象。 但最后一件事可能并不总是发生,正如苹果所说。系统决定是否取消分配。在大多数情况
我有一个客户端-服务器应用程序,它使用 WCF 进行通信,并使用 NetDataContractSerializer 序列化对象图。 由于服务器和客户端之间传输了大量数据,因此我尝试通过微调数据成员的
我需要有关 JMS 队列和消息处理的帮助。 我有一个场景,需要针对特定属性组同步处理消息,但可以在不同属性组之间同时处理消息。 我了解了特定于每个属性的消息组和队列的一些知识。我的想法是,我想针对
我最近开始使用 C++,并且有一种强烈的冲动 #define print(msg) std::cout void print(T const& msg) { std::cout void
我已经为使用 JGroups 编写了简单的测试。有两个像这样的简单应用程序 import org.jgroups.*; import org.jgroups.conf.ConfiguratorFact
这个问题在这里已经有了答案: Firebase messaging is not supported in your browser how to solve this? (3 个回答) 7 个月前关
在我的 C# 控制台应用程序中,我正在尝试更新 CRM 2016 中的帐户。IsFaulted 不断返回 true。当我向下钻取时它返回的错误消息如下: EntityState must be set
我正在尝试通过 tcp 将以下 json 写入 graylog 服务器: {"facility":"GELF","file":"","full_message":"Test Message Tcp",
我正在使用 Django 的消息框架来指示成功的操作和失败的操作。 如何排除帐户登录和注销消息?目前,登录后登陆页面显示 已成功登录为“用户名”。我不希望显示此消息,但应显示所有其他成功消息。我的尝试
我通过编写禁用qDebug()消息 CONFIG(release, debug|release):DEFINES += QT_NO_DEBUG_OUTPUT 在.pro文件中。这很好。我想知道是否可以
我正在使用 ThrottleRequest 来限制登录尝试。 在 Kendler.php 我有 'throttle' => \Illuminate\Routing\Middleware\Throttl
我有一个脚本,它通过die引发异常。捕获异常时,我想输出不附加位置信息的消息。 该脚本: #! /usr/bin/perl -w use strict; eval { die "My erro
允许的消息类型有哪些(字符串、字节、整数等)? 消息的最大大小是多少? 队列和交换器的最大数量是多少? 最佳答案 理论上任何东西都可以作为消息存储/发送。实际上您不想在队列上存储任何内容。如果队列大部
基本上,我正在尝试创建一个简单的 GUI 来与 Robocopy 一起使用。我正在使用进程打开 Robocopy 并将输出重定向到文本框,如下所示: With MyProcess.StartI
我想将进入 MQ 队列的消息记录到数据库/文件或其他日志队列,并且我无法修改现有代码。是否有任何方法可以实现某种类似于 HTTP 嗅探器的消息记录实用程序?或者也许 MQ 有一些内置的功能来记录消息?
我得到了一个带有 single_selection 数据表和一个命令按钮的页面。命令按钮调用一个 bean 方法来验证是否进行了选择。如果不是,它应该显示一条消息警告用户。如果进行了选择,它将导航到另
我知道 MSVC 可以通过 pragma 消息做到这一点 -> http://support.microsoft.com/kb/155196 gcc 是否有办法打印用户创建的警告或消息? (我找不到谷
当存在大量节点或二进制数据时, native Erlang 消息能否提供合理的性能? 情况 1:有一个大约 50-200 台机器的动态池(erlang 节点)。它在不断变化,每 10 分钟大约添加或删
我想知道如何在用户登录后显示“欢迎用户,您已登录”的问候消息,并且该消息应在 5 秒内消失。 该消息将在用户成功登录后显示一次,但在同一 session 期间连续访问主页时不会再次显示。因为我在 ho
如果我仅使用Welcome消息,我的代码可以正常工作,但是当打印p->client_name指针时,消息不居中。 所以我的问题是如何将消息和客户端名称居中,就像它是一条消息一样。为什么它目前仅将消
我是一名优秀的程序员,十分优秀!