I am trying to render a partial and am getting a 500 error when I try to pass variables in :locals. -I created a new table using scaffolding -I rake db:migrate my new table -I copied the new.html.erb file and renamed it _new_note.html.erb in the directory of the view that will be pulling it. -I made sure to update the _new_note view to reference the variable "note" instead of "@note" ** -In the View that I am trying to add the partial to, I added the following code: <%= render :partial=>"new_note", :locals=>{:note=>Note.new} %> In the _new_note partial, I put one line of text for testing. If I don''t include " :locals=>{:note=>Note.new} " in my render statement, the text will pull in fine. Otherwise, I will get a 500 error. Is there something wrong with the Class (causing the object to not be created)? This works ok on my computer using Mongrel, but doesn''t work on the production environment for my real website. Any ideas? Thanks **While I will eventually want note to be set, I deleted everything out except for a line of text in the _new_note.html.erb file while debugging. -- Posted via http://www.ruby-forum.com/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Laim Bee wrote:> > -In the View that I am trying to add the partial to, I added the > following code: > <%= render :partial=>"new_note", :locals=>{:note=>Note.new} %> > > In the _new_note partial, I put one line of text for testing. If I don''t > include " :locals=>{:note=>Note.new} " in my render statement, the text > will pull in fine. Otherwise, I will get a 500 error.That''s normal. When you create a partial / view and use a local variable (like "note" for instance), then that variable needs to be passed to the view. When rendering a partial, the way to pass in that variable is just as you are doing. You could also do something pretty ugly at the top of your partial like.. <% note = note rescue Note.new %> But, that is just a band-aid, and it''s pretty easy to lose track of quick-fixes like that - what you are doing, passing in a new class, is much closer to the behavior you want to emulate, so it''s better. -- Posted via http://www.ruby-forum.com/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Aldric Giacomoni wrote:> Laim Bee wrote: >> >> -In the View that I am trying to add the partial to, I added the >> following code: >> <%= render :partial=>"new_note", :locals=>{:note=>Note.new} %> >> >> In the _new_note partial, I put one line of text for testing. If I don''t >> include " :locals=>{:note=>Note.new} " in my render statement, the text >> will pull in fine. Otherwise, I will get a 500 error. > > That''s normal. When you create a partial / view and use a local variable > (like "note" for instance), then that variable needs to be passed to the > view. When rendering a partial, the way to pass in that variable is just > as you are doing. > > You could also do something pretty ugly at the top of your partial > like.. > > <% note = note rescue Note.new %> > But, that is just a band-aid, and it''s pretty easy to lose track of > quick-fixes like that - what you are doing, passing in a new class, is > much closer to the behavior you want to emulate, so it''s better.Do you know what would cause the entire page to not load properly then and cause a 500 error? What does the rescue Note.new do? Thanks, Joe -- Posted via http://www.ruby-forum.com/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Dec 21, 1:12 am, Laim Bee <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> In the _new_note partial, I put one line of text for testing. If I don''t > include " :locals=>{:note=>Note.new} " in my render statement, the text > will pull in fine. Otherwise, I will get a 500 error. Is there something > wrong with the Class (causing the object to not be created)? This worksJust to isolate things a little further, can you call Note.new elsewhere ? (eg from script/console on your production machine) ? Fred> ok on my computer using Mongrel, but doesn''t work on the production > environment for my real website. Any ideas? Thanks > > **While I will eventually want note to be set, I deleted everything out > except for a line of text in the _new_note.html.erb file while > debugging. > -- > Posted viahttp://www.ruby-forum.com/.-- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Laim Bee wrote:> Aldric Giacomoni wrote: >> >> <% note = note rescue Note.new %> >> But, that is just a band-aid, and it''s pretty easy to lose track of >> quick-fixes like that - what you are doing, passing in a new class, is >> much closer to the behavior you want to emulate, so it''s better. > > Do you know what would cause the entire page to not load properly then > and cause a 500 error? What does the rescue Note.new do?Keeping in mind that the proper thing to do is to pass the local variable with the :locals hash, I''ll answer your question (and, in doing so, I''ll realize I made a mistake!) <% note = note rescue Note.new %> That''s actually wrong, and if you did that, well, that''s why it''s broken! <% note = note || Note.new %> That''s better. You can check the behavior yourself with irb: $ irb irb(main):001:0> note NameError: undefined local variable or method `note'' for main:Object from (irb):1 from :0 irb(main):002:0> note = note => nil irb(main):003:0> note => nil The "rescue Note.new" was meant to tell Ruby "Should you encounter an error with this assignment, create a new Note instead" -- but clearly it is not failing to assign. -- Posted via http://www.ruby-forum.com/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Aldric Giacomoni wrote:> Laim Bee wrote: >> Aldric Giacomoni wrote: >>> >>> <% note = note rescue Note.new %> >>> But, that is just a band-aid, and it''s pretty easy to lose track of >>> quick-fixes like that - what you are doing, passing in a new class, is >>> much closer to the behavior you want to emulate, so it''s better. >> >> Do you know what would cause the entire page to not load properly then >> and cause a 500 error? What does the rescue Note.new do? > > Keeping in mind that the proper thing to do is to pass the local > variable with the :locals hash, I''ll answer your question (and, in doing > so, I''ll realize I made a mistake!) > <% note = note rescue Note.new %> > That''s actually wrong, and if you did that, well, that''s why it''s > broken! > <% note = note || Note.new %> > That''s better. > > You can check the behavior yourself with irb: > $ irb > irb(main):001:0> note > NameError: undefined local variable or method `note'' for main:Object > from (irb):1 > from :0 > irb(main):002:0> note = note > => nil > irb(main):003:0> note > => nil > > The "rescue Note.new" was meant to tell Ruby "Should you encounter an > error with this assignment, create a new Note instead" -- but clearly it > is not failing to assign.I tried pasting <% note = note || Note.new %> into my new Notes partial and it still didn''t render. I also tried creating a new Note in the production environment (using ruby script/server production) and got the following error (See below). It looks like a table doesn''t exist? Is it possible that when I db:migrate ''d my table, it did it only to the development environment and not the production environment? scratch=Note.new ActiveRecord::StatementINvalid:Could not find table ''notes'' from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection)adapters/sqlite3_adapter.rb:29:in ''table_structure'' from /usr/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active)support/core-ext/object/misc.rb:39: in ''returning'' from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active)recrod/connection)adapters/sqlite3_adapter.rb:28:in ''table_structure'' from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/sqlite3_adapter.rb:213:in ''columns'' from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:1276: in ''columns'' from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:3008: in ''attributes_from_column_definition_without_lock'' from /usr/lib/ruby/gems/1.8/gems/activerecord02.3.2/lib/active_record/locking/optimistic,rb:66:in ''attributes_from_column_definition'' from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:2435: in ''initialize'' from <irb>:1:in ''new'' from <irb>:1 -- Posted via http://www.ruby-forum.com/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Dec 28, 3:56 am, Laim Bee <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> I also tried creating a new Note in the production environment (using > ruby script/server production) and got the following error (See below). > It looks like a table doesn''t exist? Is it possible that when I > db:migrate ''d my table, it did it only to the development environment > and not the production environment? >did you do rake db:migrate RAILS_ENV=production (or setting RAILS_ENV to production by another method) ? if not then you probably did only run it against development Fred -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung wrote:> On Dec 28, 3:56�am, Laim Bee <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> I also tried creating a new Note in the production environment (using >> ruby script/server production) and got the following error (See below). >> It looks like a table doesn''t exist? Is it possible that when I >> db:migrate ''d my table, it did it only to the development environment >> and not the production environment? >> > did you do rake db:migrate RAILS_ENV=production (or setting RAILS_ENV > to production by another method) ? if not then you probably did only > run it against development > > Fred > > -- > > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en.Thank you this helped. I won''t forget to actually migrate to production again! My site has a front page that shows an index of blog entries. I made a new table to hold comments and my front page will now show the index from my blog table, along with an index of comments (from the comment table) and a new comment form (so visitors can just add a new comment on the front page). The New Comment form displays ok on the front page (I just took the new view from the comments controller moved a copy of it into my blogs view). I then rendered it as a partial. When I click on the "Create" button, I get a 404 error. There''s nothing in the mongrel log or the production log. I noticed that the toolbar now says www.<domainname>.com/comments I tried tweaking it thinking that it was not seeing the create method in the comments controller, but this didn''t work. How does Rails know when you click the Submit button, that you want to initiate the create method in the comments controller? -- Posted via http://www.ruby-forum.com/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jan 2, 5:21 pm, Laim Bee <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Frederick Cheung wrote:> The New Comment form displays ok on the front page (I just took the new > view from the comments controller moved a copy of it into my blogs > view). I then rendered it as a partial. When I click on the "Create" > button, I get a 404 error. There''s nothing in the mongrel log or the > production log. I noticed that the toolbar now says > www.<domainname>.com/comments > > I tried tweaking it thinking that it was not seeing the create method in > the comments controller, but this didn''t work. > > How does Rails know when you click the Submit button, that you want to > initiate the create method in the comments controller?The conventions (in particular REST) that rails uses dictate that to create a foo you post to /foos Fred> -- > Posted viahttp://www.ruby-forum.com/.-- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung wrote:> On Jan 2, 5:21�pm, Laim Bee <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> Frederick Cheung wrote: > >> How does Rails know when you click the Submit button, that you want to >> initiate the create method in the comments controller? > > The conventions (in particular REST) that rails uses dictate that to > create a foo you post to /foos > > Fred >> -- >> Posted viahttp://www.ruby-forum.com/. > > -- > > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en.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? -- Posted via http://www.ruby-forum.com/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
2010/1/5 Laim Bee <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>:>... > > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law wrote:> 2010/1/5 Laim Bee <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>: >>... >> >> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > 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.<domain name>.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): <h1> New Blog </h1> <% 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: <h1> New Comment </h1> <% 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.<domain name>.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. -- Posted via http://www.ruby-forum.com/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
I also tried creating a new Note object directly in the partial itself and this didn''t work. Is this the correct action that will occur when you click the Post button: Looks in Routes > Runs method from Controller > Resolves View I have tried putting logger.debug in the create method of the Note controller in hopes of proving that I am entering it. I doubt that I am , but this is where I expect it to look. If I am using map.resources :notes is this the method it should be looking at? -- Posted via http://www.ruby-forum.com/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
2010/1/16 Laim Bee <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>:> > I also tried creating a new Note object directly in the partial itself > and this didn''t work. Is this the correct action that will occur when > you click the Post button: > > Looks in Routes > Runs method from Controller > Resolves ViewYes, though I would say renders view rather than resolves. So the correct place to make the new note is in the create action, and populate it from the posted params. Also remember that all the ruby in the view is run on the server before the resulting html is sent to the browser.> > I have tried putting logger.debug in the create method of the Note > controller in hopes of proving that I am entering it. I doubt that I am > , but this is where I expect it to look. If I am using map.resources > :notes is this the method it should be looking at?You should be able to see from development.log what is happening. You can see all the params etc. Sometimes ruby-debug is useful when you are not sure what is going on. With ruby-debug you can simply put a break point in the create action and check that it is getting there and what all the variables are set to. Did I already point you to the rails guide on debugging? 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.