/ messages / newにアクセスしようとすると、アプリで次のエラーが発生します。
No route matches [GET] "/messages/new"
/ messages / newルートをconfig.rbに追加しようとすると、次のエラーが発生します。
No route matches {:controller=>"messages"}
アプリ自体はGumtree.comのようなもので、ユーザーが来て投稿を作成し(たとえば、私の車を売る)、人々はメッセージの送信に応答します(Simple Pvtメッセージプラグインを介して)。
これに関する洞察をいただければ幸いです。
ありがとう!
ファイサル
ROUTES.RB
Mysalary::Application.routes.draw do
resources :users do
resources :messages do
collection do
post :delete_selected
end
end
end
resources :users
resources :profiles
resources :pages
resources :posts
resources :messages
get "pages/home"
get "pages/about"
get "pages/legal"
get "pages/feedback"
root :to => 'posts#new'
end
メッセージ>新しいビュー
<% form_for @message, :url => user_messages_path(@user) do |f| %>
<p>
To:<br />
<%= f.text_field :to %>
<%= error_message_on @message, :to %>
</p>
<p>
Subject:<br />
<%= f.text_field :subject %>
<%= error_message_on @message, :subject %>
</p>
<p>
Message<br />
<%= f.text_area :body %>
<%= error_message_on @message, :body %>
</p>
<p>
<%= submit_tag "Send" %>
</p>
<% end %>
メッセージコントローラー
class MessagesController < ApplicationController
before_filter :set_user
def index
if params[:mailbox] == "sent"
@messages = @user.sent_messages
else
@messages = @user.received_messages
end
end
def show
@message = Message.read_message(params[:id], current_user)
end
def new
@message = Message.new
if params[:reply_to]
@reply_to = @user.received_messages.find(params[:reply_to])
unless @reply_to.nil?
@message.to = @reply_to.sender.login
@message.subject = "Re: #{@reply_to.subject}"
@message.body = "\n\n*Original message*\n\n #{@reply_to.body}"
end
end
end
def create
@message = Message.new(params[:message])
@message.sender = @user
@message.recipient = User.find_by_login(params[:message][:to])
if @message.save
flash[:notice] = "Message sent"
redirect_to user_messages_path(@user)
else
render :action => :new
end
end
def delete_selected
if request.post?
if params[:delete]
params[:delete].each { |id|
@message = Message.find(:first, :conditions => ["messages.id = ? AND (sender_id = ? OR recipient_id = ?)", id, @user, @user])
@message.mark_deleted(@user) unless @message.nil?
}
flash[:notice] = "Messages deleted"
end
redirect_to :back
end
end
private
def set_user
@user = User.first
end
end
メッセージモデル
class Message < ActiveRecord::Base
is_private_message
attr_accessor :to
end
レイクルート出力
delete_selected_user_messages POST /users/:user_id/messages/delete_selected(.:format) {:action=>"delete_selected", :controller=>"messages"}
user_messages GET /users/:user_id/messages(.:format) {:action=>"index", :controller=>"messages"}
POST /users/:user_id/messages(.:format) {:action=>"create", :controller=>"messages"}
new_user_message GET /users/:user_id/messages/new(.:format) {:action=>"new", :controller=>"messages"}
edit_user_message GET /users/:user_id/messages/:id/edit(.:format) {:action=>"edit", :controller=>"messages"}
user_message GET /users/:user_id/messages/:id(.:format) {:action=>"show", :controller=>"messages"}
PUT /users/:user_id/messages/:id(.:format) {:action=>"update", :controller=>"messages"}
DELETE /users/:user_id/messages/:id(.:format) {:action=>"destroy", :controller=>"messages"}
messages GET /messages(.:format) {:action=>"index", :controller=>"messages"}
POST /messages(.:format) {:action=>"create", :controller=>"messages"}
new_message GET /messages/new(.:format) {:action=>"new", :controller=>"messages"}
edit_message GET /messages/:id/edit(.:format) {:action=>"edit", :controller=>"messages"}
message GET /messages/:id(.:format) {:action=>"show", :controller=>"messages"}
PUT /messages/:id(.:format) {:action=>"update", :controller=>"messages"}
DELETE /messages/:id(.:format) {:action=>"destroy", :controller=>"messages"}
pages_home GET /pages/home(.:format) {:action=>"home", :controller=>"pages"}
pages_about GET /pages/about(.:format) {:action=>"about", :controller=>"pages"}
pages_legal GET /pages/legal(.:format) {:action=>"legal", :controller=>"pages"}
pages_feedback GET /pages/feedback(.:format) {:action=>"feedback", :controller=>"pages"}
messages_new GET /messages/new(.:format) {:action=>"new", :controller=>"messages"}
root