1

当我尝试访问 /messages/new 时,我的应用程序出现以下错误:

No route matches [GET] "/messages/new"

如果我尝试向 config.rb 添加 /messages/new 路由,我会收到以下错误:

No route matches {:controller=>"messages"}

该应用程序本身就像 Gumtree.com,用户来这里创建帖子(例如卖我的车),然后人们回复我向他们发送消息(通过 Simple Pvt Messages 插件)。

对此的任何见解将不胜感激。

谢谢!

费萨尔

路线.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  
4

1 回答 1

2

用这个替换你的路由文件,你在用户内部定义资源消息,也是两次。

Mysalary::Application.routes.draw 做

     resources :messages do
       collection do
         post :delete_selected
       end
     end


       resources :users
       resources :profiles
       resources :pages
       resources :posts

        get "pages/home"
        get "pages/about"
         get "pages/legal"
        get "pages/feedback"

        root :to => 'posts#new'

        end

尝试一下...

于 2012-04-19T09:17:31.983 回答