Ruby on rails 用户消息的路由

Ruby on rails 用户消息的路由,ruby-on-rails,messages,Ruby On Rails,Messages,我正在尝试构建一种功能,让用户能够互相发送消息。理想情况下,用户可以访问另一个用户的配置文件并单击一个链接,该链接会将他们重定向到一个表单,他们将在该表单中输入消息,然后单击“发送”,消息将发送到第二个用户的收件箱 另外,用户应该能够访问收件箱页面,该页面显示已发送给他们的所有邮件 我现在正在做第一部分,路线有问题。具体来说,当我单击用户配置文件中的“发送消息”链接时,我将被重定向到用户的收件箱页面,而不是重定向到可以输入消息上下文并发送消息的表单。我不确定我做错了什么。有人能建议我如何解决这个

我正在尝试构建一种功能,让用户能够互相发送消息。理想情况下,用户可以访问另一个用户的配置文件并单击一个链接,该链接会将他们重定向到一个表单,他们将在该表单中输入消息,然后单击“发送”,消息将发送到第二个用户的收件箱

另外,用户应该能够访问收件箱页面,该页面显示已发送给他们的所有邮件

我现在正在做第一部分,路线有问题。具体来说,当我单击用户配置文件中的“发送消息”链接时,我将被重定向到用户的收件箱页面,而不是重定向到可以输入消息上下文并发送消息的表单。我不确定我做错了什么。有人能建议我如何解决这个路由问题吗

routes.rb

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'
      get 'create_message'
    end
  end

  resources :messages

  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
用户\u controller.rb

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 create_message
  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

  def index
  end

  def new
  end

  def create
  end

  def destroy
  end

end
class UsersController
消息\u controller.rb

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 create_message
  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

  def index
  end

  def new
  end

  def create
  end

  def destroy
  end

end
class messages控制器
user.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  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

  #validates :first_name, presence: true, length: { maximum: 50 }
  #validates :last_name, presence: true, length: { maximum: 50 }

end
class用户
messages.rb

class Messages < ActiveRecord::Base
  belongs_to :sender
  belongs_to :recipient
  default_scope -> [ order('created_at DESC') ]
  validates :sender_id, presence: true
  validates :recipient_id, presence: true
end
class消息[顺序('created_at DESC')]
验证:发件人id,状态:true
验证:收件人id,状态:true
终止
index.html.erb

<h1>Inbox</h1>
收件箱
创建_messages.html.erb

<h2>Create Message</h2>

<%= form_for @user do |f| %>

    <%= f.label :content %><br />
    <%= f.text_area :first_name, autofocus: true %>

  <div><%= f.submit "Send" %></div>
<% 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
   resources :messages, only: [:new, :create]
end
resources :messages, only: [:index, :show, :destroy] #domain.com/messages -> inbox

#app/controllers/messages/new.html.erb
class MessagesController < ApplicationController
   before_action :set_recipient

   def new
      @message = current_user.messages.new recipient_id: params[:user_id]
   end

   def create
      @message = current_user.messages.new message_params
      @message.sender_id = current_user.id
      @message.recipient_id = @recipient.id
      @message.save
   end

   private

   def message_params
      params.require(:message).permit(:title, :body, :sender_id, :recipient_id)
   end

   def set_recipient
      @recipient = User.find params[:user_id] if params[:user_id].present?
   end
end

#app/views/messages/new.html.erb
<%= form_for [@recipient, @message] do |f| %>
   <%= f.text_field :title %>
   <%= f.text_field :body %>
   <%= f.submit %>
<% end %>
#config/routes.rb
为:用户,路径:,:控制器=>{:注册=>“注册”},路径名称:{注册:“注册”,登录:“登录”,注销:“注销”}
资源:用户可以
资源:消息,仅:[:新建,:创建]
终止
资源:邮件,仅:[:索引,:显示,:销毁]#domain.com/messages->inbox
#app/controllers/messages/new.html.erb
类MessagesController

路线

上述代码将使您能够使用以下链接:

#app/views/users/show.html.erb
<%= link_to "Send Message", user_message_path(@user) %>
#app/views/users/show.html.erb
您必须记住,由于Rails是一个复杂的系统,所以您所做的一切(包括路由)都应该围绕对象旋转。这就是为什么在路由模式中看到
resources
指令的原因——它使您能够定义一系列以各种对象为核心的基于路由


我相信你的问题在于你没有在适当的程度上使用Rails面向对象的特性。如果您像我上面的例子一样应用路由,并使用相关方法进行备份,您应该能够创建非常引人注目的流。

几个问题:1)您没有显示指向
create\u message
操作的链接。2)
create_messages
操作与
create_messages.html.erb
不匹配。3) 视图中使用的
@user
未定义。4) 消息模型和控制器从未使用过。Rich,感谢您的快速响应。当我实现您的更改时,我在执行“发送消息”的链接时出现以下错误,#@Message=current_user.Message.new recipient_id:params[:user_id]对此有何想法?