未找到Rails 3操作控制器记录

时间:2011-10-07 02:02:24

标签: ruby-on-rails-3 controller

我正在使用Ajax的jQuery UI选项卡,我收到一个错误:

Failed to load resource: the server responded with a status of 404 (Not Found)

资源为:3000/messages/profile_messages

如果我在浏览器中转到http://localhost:3000/messages/profile_messages,我会收到动作控制器:异常捕获:

ActiveRecord::RecordNotFound in MessagesController#show

Couldn't find Message with ID=profile_messages

Rails.root: /Users/me/Desktop/myapp

app/controllers/messages_controller.rb:18:in `show'

Parameters:

{"id"=>"profile_messages"}

在我的MessagesController#show

def show
  @message = Message.find(params[:id])
  respond_to do |format|
    format.html # show.html.erb
    format.xml  { render :xml => @message }
    format.js {render :layout => false }
  end
end

MessagesController#show是针对单个消息的,但是我如何使其适用于我正在尝试的内容?我是Rails和编程的新手,很抱歉,如果这是一个明显的答案。

routes.rb中:

resources :users
resources :profiles
resources :session
resources :sessions
resources :messages do
  resources :responses
end
resource :messages do
  collection do
    get :profile_messages
  end
end

佣金路线:

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"}
profile_messages_messages GET    /messages/profile_messages(.:format)               {:action=>"profile_messages", :controller=>"messages"}
         POST   /messages(.:format)  {:action=>"create", :controller=>"messages"}
new_messages GET    /messages/new(.:format)  {:action=>"new", :controller=>"messages"}
edit_messages GET    /messages/edit(.:format)  {:action=>"edit", :controller=>"messages"}
         GET    /messages(.:format)  {:action=>"show", :controller=>"messages"}
         PUT    /messages(.:format)  {:action=>"update", :controller=>"messages"}
         DELETE /messages(.:format)  {:action=>"destroy", :controller=>"messages"}
消息控制器中的

profile_messages方法:

def profile_messages
  @messages = User.find(@profile.user_id).messages
  respond_to do |format|
    format.html # index.html.erb
    format.xml  { render :xml => @messages }
  end
end

1 个答案:

答案 0 :(得分:2)

使用http://localhost:3000/messages/profile_messages,您尝试使用id =>访问该邮件profile_messages

您可能需要做的是:

http://localhost:3000/messages/:id/profile_messages

例如:

http://localhost:3000/messages/1/profile_messages

在routes.rb中,您可能需要添加以下内容:

resources :messages do
  member do
    get :profile_messages
  end
end

========更新========

基于此行

profile_messages_messages GET    /messages/profile_messages(.:format)               {:action=>"profile_messages", :controller=>"messages"}

您能告诉我们控制器消息中的方法profile_messages吗?

========更新========

您应该从

更改路线
resources :users
resources :profiles
resources :session
resources :sessions
resources :messages do
  resources :responses
end
resource :messages do
  collection do
    get :profile_messages
  end
end

resources :users
resources :profiles
resources :session
resources :sessions
resources :messages do
  resources :responses
  get :profile_messages, :on => :collection
end

你也可以添加

match "/messages/profile_messages" => "messages#profile_message"