I'm getting this error-
ActiveRecord::RecordNotFound in PagesController#create
Couldn't find Course without an ID
I'm getting this error-
ActiveRecord::RecordNotFound in PagesController#create
Couldn't find Course without an ID
Show your method and filters
I think because the record with that id is not existed in the database. You could check the database to to see whether there
is a record with the id. Or you could also check the id value in the controller, is it valiable?
I’m getting this error-
ActiveRecord::RecordNotFound in PagesController#create
Couldn’t find Course without an ID
Btw, the pages belongs to courses, and the courses has many pages
Now, in my pages controller, for my create action, i have this–
def create
@course = Course.find(params[:id]) @page = @course.pages.build(params[:page])
… if and else stuff…
end
Whats wrong? Why am i getting this message? Thanks, i really need help!
David, you’re assuming that the first line in the method is successful after
execution. Thus, I would recommend doing something like the following:
def create
@course = Course.find( params[:id] ) # The exception is thrown here.
@page = @course.pages.build( params[:page] )
if @page.save
flash[:notice] = “Successfully created page.”
redirect_to course_url( @page.course_id )
else
render :action => ‘new’
end
rescue ActiveRecord::RecordNotFound => exception # The thrown exception is caught and dealt with here.
logger.error( “Error: #{exception.message}”
flash[:error] = “#{exception.message}”
render :action => ‘new’
end
Next, you can test exception handling in the IRB by doing something as simple as the following:
def my_exceptional_method; Post.find( 2000 ); rescue ActiveRecord::RecordNotFound => exception; puts exception.message; end
my_exceptional_method
Lastly, I would recommend reading the relevant sections of “AWDwRails 3ed” by Dave Thomas et al.
Good luck,
-Conrad
I’m getting this error-
ActiveRecord::RecordNotFound in PagesController#create
Couldn’t find Course without an ID
Btw, the pages belongs to courses, and the courses has many pages
Now, in my pages controller, for my create action, i have this–
def create
@course = Course.find(params[:id]) @page = @course.pages.build(params[:page])
… if and else stuff…
end
Whats wrong? Why am i getting this message? Thanks, i really need help!
David, you’re assuming that the first line in the method is successful after
execution. Thus, I would recommend doing something like the following:
def create
@course = Course.find( params[:id] ) # The exception is thrown here.
@page = @course.pages.build( params[:page] )
if @page.save
flash[:notice] = “Successfully created page.”
redirect_to course_url( @page.course_id )
else
render :action => ‘new’
end
rescue ActiveRecord::RecordNotFound => exception # The thrown exception is caught and dealt with here.
logger.error( “Error: #{exception.message}”
flash[:error] = “#{exception.message}”
render :action => ‘new’
end
Next, you can test exception handling in the IRB by doing something as simple as the following:
def my_exceptional_method; Post.find( 2000 ); rescue ActiveRecord::RecordNotFound => exception; puts exception.message; end
Correction, the above line should be the following:
def my_exceptional_method; Course.find( 2000 ); rescue ActiveRecord::RecordNotFound => exception; puts exception.message; end
Good luck,
-Conrad
Hi David This error is mainly because your not getting id in the create function Please follow the following steps, i think this will solve your problem.
I hope you configured your routes like this map.resources :courses do |course| course.resources :pages end
<%form_for :page,:url=>course_pages_path(@course) do %> <%end%>
in controller def new @course = Course.find(params[:course_id]) end
def create @course = Course.find(params[:course_id]) @page = @course.pages.find(params[:page]) end
Keep rocking!
David Zhu wrote:
Hi David
This error is mainly because your not getting id in the create function
Please follow the following steps, i think this will solve your problem.
I hope you configured your routes like this
map.resources :courses do |course|
course.resources :pages
end
<%form_for :page,:url=>course_pages_path(@course) do %>
<%end%>
in controller
def new
@course = Course.find(params[:course_id])
end
def create
@course = Course.find(params[:course_id])
@page = @course.pages.find(params[:page])
end
Srijith, the second isn’t correct because you’re
trying to find a page that doesn’t exist the database.
Thus, this will generate ActiveRecord::RecordNotFound
as would the first line if it doesn’t exist and the second
should like like the following:
@page = @course.pages.build( params[:page] )
@David - make sure that you have a ‘new’ method
on the PagesController defined as follows:
def new
@course = Course.find( params[:course_id] )
@page = @course.pages.build
end
Good luck,
-Conrad
Sorry! that was new method by mistake i had written it as find. map.resources :courses do |course| course.resources :pages end
<%form_for :page,:url=>course_pages_path(@course) do %> <%end%>
in controller def new @course = Course.find(params[:course_id]) end
def create @course = Course.find(params[:course_id]) @page = @course.pages.new(params[:page]) @page.save end
Keep rocking
Conrad Taylor wrote:
can i comment out- map.resources :pages in my routes? or do i need that?
i got it-
Rolling with Rails 2.0 - The First Full Tutorial - Part 1 | AkitaOnRails.com
Thanks guys for all the help
can i comment out- map.resources :pages in my routes? or do i need
that?
David, it really depends on what you’re trying to do. It seems that you were using
nested resources. Thus, I would expect the following within your routes.rb:
map.resources :courses, :has_many => :pages
map.root :courses
Good luck,
-Conrad
can i comment out- map.resources :pages in my routes? or do i need
that?
David, it really depends on what you’re trying to do. It seems that you were using
nested resources. Thus, I would expect the following within your routes.rb:
map.resources :courses, :has_many => :pages
map.root :courses
Correction, it should be
map.root :controller => “courses”
-Conrad
thanks, that really helped! rated all of you guys 5 stars
Srijith nair wrote:
Hi David This error is mainly because your not getting id in the create function Please follow the following steps, i think this will solve your problem.
I hope you configured your routes like this map.resources :courses do |course| course.resources :pages end
<%form_for :page,:url=>course_pages_path(@course) do %> <%end%>
in controller def new @course = Course.find(params[:course_id]) end
def create @course = Course.find(params[:course_id]) @page = @course.pages.find(params[:page]) end
Keep rocking!
David Zhu wrote:
I'm getting this error-
ActiveRecord::RecordNotFound in PagesController#create
Couldn't find Course without an ID
I have a very similar problem I think. I Can't find Story without an ID.
This is from my email_controller.rb
def correspond user = @current_user story = Story.find(params[:id]) recipient = story.user @title = "Email @current_user.login" if param_posted?(:message) @message = Message.new(params[:message]) if @message.valid? UserMailer.deliver_message( :user => user, :recipient => recipient, :message => @message, :user_url => user, :reply_url => url_for(:action => "correspond", :id => user.login) ) flash[:notice] = "Email sent." redirect_to :action => "index", :controller => "stories" end end end
The problem is in the third line.
Any help would be much appreciated.
The error says that it cannot find Story without an id, you are asking find to use params[:id]. So possibly params[:id] is not valid. What is the value of params[:id]? Look in development.log to find out. Also have a look at the rails guide on debugging, it will show you how you can use ruby-debug to break into your code and inspect data, so you can see what params[:id] is. In this case the log may be enough though.
Colin
Colin Law wrote:
...
The problem is in the third line.
. What
is the value of params[:id]? Look in development.log to find out.
This is all I have in the log:
Processing EmailController#correspond (for 127.0.0.1 at 2010-06-30 19:32:58) [GET] e[4;36;1mUser Load (0.5ms)e[0m e[0;1mSELECT * FROM "users" WHERE ("users"."id" = 1) LIMIT 1e[0m
ActiveRecord::RecordNotFound (Couldn't find Story without an ID): app/controllers/email_controller.rb:23:in `correspond'
Rendered rescues/_trace (57.5ms) Rendered rescues/_request_and_response (0.4ms) Rendering rescues/layout (not_found)
I'll look at the debugging section