2012-04-19 221 views
1

我讓我的應用程序下面的錯誤,當我試圖訪問/消息/新:路由錯誤 - 的Rails 3

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

如果我嘗試添加/消息/新航線config.rb,我出現以下錯誤:

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

該應用程序本身就像Gumtree.com,其中用戶來創造帖子(如賣我的車),人們應對我向他們發送信息(通過簡單的私人郵件插件)。

對此的任何見解將不勝感激。

謝謝!

費薩爾

的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 

信息>新建VIEW

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

RAKE ROUTES輸出

  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 
+0

如何在用戶來到你嵌套消息? – 2012-04-19 08:01:32

+0

'rake routes'的輸出是什麼? – klump 2012-04-19 08:06:26

+0

爲什麼你要在頂層爲用戶提供兩次資源? – klaffenboeck 2012-04-19 08:15:26

回答

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 

嘗試...

+0

謝謝!有效。 – hikmatyar 2012-04-19 09:44:50