Rails 3 Action Controller Record Not Found
I'm using jQuery UI Tabs with Ajax and I am getting an error:
Failed to load resource: the server responded with a status of 404 (Not Found)
The resource is :3000/messages/profile_messages
If I go to http://localhost:3000/messages/profile_messages in my browser I get Action Controller: Exception caught:
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"}
In my 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
The MessagesController#show is for individual messages, but how can I make it work for what I'm trying to do? I'm new to Rails and programming so sorry if this is an obvious answer.
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
rake routes:
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=>"mess开发者_如何学编程ages"}
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 method in MessagesController:
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
With http://localhost:3000/messages/profile_messages you are trying to access to the message with the id => profile_messages
What you might need to do is :
http://localhost:3000/messages/:id/profile_messages
for example:
http://localhost:3000/messages/1/profile_messages
In your routes.rb, you might need to add something like:
resources :messages do
member do
get :profile_messages
end
end
======== UPDATE ========
Based on this line
profile_messages_messages GET /messages/profile_messages(.:format) {:action=>"profile_messages", :controller=>"messages"}
Can you show us the method profile_messages from your controller messages ?
======== UPDATE ========
You should change your routes from
resources :users
resources :profiles
resources :session
resources :sessions
resources :messages do
resources :responses
end
resource :messages do
collection do
get :profile_messages
end
end
to
resources :users
resources :profiles
resources :session
resources :sessions
resources :messages do
resources :responses
get :profile_messages, :on => :collection
end
You could also add
match "/messages/profile_messages" => "messages#profile_message"
精彩评论