On Sat, Apr 3, 2010 at 7:05 PM, David Zhu <[email protected]> wrote:
> 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
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<rubyonrails-talk%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-talk?hl=en.
>
>
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.