我正在尝试构建使用户能够发送彼此消息的功能。理想情况下,用户可以访问其他用户的个人资料并点击链接,将他们重定向到他们将输入消息的表单,然后单击发送,它将被传送到第二个用户的收件箱。
另外,用户应该能够访问收件箱页面,该页面显示已发送给他们的所有邮件。
我现在正在处理第一部分,并且遇到路线问题。具体来说,当我点击用户个人资料中的发送消息链接时,我被重定向到用户的收件箱页面,而不是被重定向到我可以输入我的消息的上下文并发送的表单。我不确定我在这里做错了什么。任何人都可以建议我如何解决这个路由问题?
的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
users_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
messages_controller.rb
class MessagesController < ApplicationController
def index
end
def new
end
def create
end
def destroy
end
end
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
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
index.html.erb
<h1>Inbox</h1>
creates_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 %>
答案 0 :(得分:0)
这就是我要做的事情:
#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 %>
<强>路线强>
上述代码将使您能够使用以下链接:
#app/views/users/show.html.erb
<%= link_to "Send Message", user_message_path(@user) %>
你必须记住,由于Rails是object-orientated,你所做的一切(包括路线)都应围绕对象。这就是您在路由模式中看到resources
指令的原因 - 它使您能够定义一系列基于CRUD的路由,其中包含各种对象。
我相信你的问题是你没有在适当程度上使用Rails的面向对象特性。如果您应用路线作为上面的示例,使用相关方法进行备份,则应该能够创建非常引人注目的流程。