Colin L. wrote:
2010/1/5 Laim B. [email protected]:
…
So what’s the best way to debug why the create method is not working
properly? Is there a way to set a global variable that would be viewable
from ruby script/console?
Have a look at the RoR guide on Debugging at
http://guides.rubyonrails.org/
Colin
–
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.
Thanks Colin. I took a look at the link (along with Head First Rails /
Simply 2 Rails and other websites) and I better understand why clicking
on the Submit button is redirecting to http://www..com/notes (It’s trying to perform a POST action…go REST!). I
tracked this to the create Method and have been playing with this method
thinking that the breakdown is happening here.
I tried using logger.debug since I believe the problem may exist in the
controller,(as mentioned here:
http://jeremyhubert.com/articles/debugging-in-rails.html) but wasn’t too
successful with it.
In the mean time, I fired up the console and manually created a new Note
object to verify that there wasn’t a problem with the model or my
database. I was able to create a new Note fine and display it on my
homepage with a partial that borrows from the index view of my Notes
object.
In my blogs “new” templtae, I noticed that I passed the action
explicitly with the following statement (blogs works great for me):
New Blog
<% form_for(@blog, :url=>{:action=>'create'}) do |f| %>
.
.
etc
I tried passing the “create” action to my “new” note partial (which is
rendered in the index of my blog template) and the interesing thing is
that it redirected back to the “create” method of my blogs controller
and created a new blog entry! (Although it was blank because the columns
in the blogs table are different than the notes table). I thought all I
had to do was pass in the correct controller and then I would be golden:
New Comment
<% form_for(note, :url=>{:controller=>"Notes",:action=>'create'}) do |f|
%>
.
.
etc
Unfortunately this did not work out (There was no change in behavior).
I looked at the html source code for my home page to see if the form was
referencing the blogs controller since the note partial is on the blogs
template, but it did not look like it was. I checked it against the
source code for the same project on my local machine (where this partial
works) and the html source code for both forms look the same.
I think the problem may be related to the fact that I am using the
following line to render the partial:
<%= render :partial=>“new_note”, :locals=>{:note=>Note.new} %>
Could the problem be that when I submit the form, :note gets
overwritten??
=====================================
def create
@note = Note.new(params[:note])
@note.save
redirect_to ‘http://www.google.com’
#respond_to do |format|
# if @note.save
# flash[:notice] = 'Comment was successfully created.'
# format.html { redirect_to("http://www.google.com")}
# format.xml { render :xml => @note, :status => :created,
:location => @note }
#else
# format.html { render :action => “new” }
# format.xml { render :xml=>@note.errors, :status=>
:unprocessable_entity }
#end
#end
end
====================================
Perhaps params[:note] gets set incorrectly? The odd thing is that I
can’t get the redirection to work and take me to google (Which I would
expect to happen if I was hitting the create method). This leads me to
believe that I’m not hitting the create method, however if this was the
case, why would it be trying to access ‘http:www..com/notes’? I would expect it to hit the controller and THEN try
to render the view.
I’m kind of out of ideas at this point of things to try. Since my
hosting service runs the webserver, I don’t see the server output like I
do when I run Mongrel from my desktop for testing. Also, there is no
information in the production log suggesting what may be causing the 404
error when I click the Submit button. I think it would be a good idea to
set a global variable as a marker to figure out for sure whether that
method is being hit.
Any ideas are appreciated. Thanks.