71062

Partial rendering one time too many for collection

Question:

Creating a simple blog application,

I have this partial

.comment %p %b Namn: = comment.name %p %b kommentar: = comment.content %p = link_to 'Destroy Comment', [comment.post, comment], :confirm => 'Are you sure?', :method => :delete

and its called from

= render :partial => 'comment', :collection => @post.comments

it always renders the partial one time to many?

edit:

It has this form

= form_for ([@post,@post.comments.build])

Answer1:

As depicted in the comments of your question, the @post.comments.build is the culprit. Change it to

= form_for ([@post, @post.comments.new])

and the additional item when rendering the collection should be gone.

There is a good post about the difference of build and new here: <a href="https://stackoverflow.com/questions/4954313/build-vs-new-in-rails-3" rel="nofollow">Build vs new in Rails 3</a>

Recommend

  • Rails: How to review content before submit/save?
  • Using js.rjs to render inline content
  • Rails help ActionView::Template::Error HINT: No operator matches the given name and argument type(s
  • Rails Kaminari pagination error with Ransack
  • Giving right align to numeric data in datatables
  • How to make Rails do not ignore trailing slashes in the routes?
  • Custom routes with rails 3.1
  • Does derived class' member functions inherit virtualness from base class?
  • Using Resque and resque_mailer to send emails raises Routing error
  • Getting webGL error in autodesk viewer
  • Is there a way to clear some session data from ALL sessions?
  • PHPUnit: Expected status code 200 but received 419 with Laravel
  • Nginx rewrite equivalent to Apache RewriteRule that converts URL params into QueryString key/value p
  • Can't connect Entity Framework to local SQL Server Express
  • How to extract a number from a string [duplicate]
  • Undefined navigator.push React-native 0.43.4
  • redirect_to root_url and return unless @user.activated
  • How to synchronize jQuery dialog box to act like alert() of Javascript
  • Django invalid literal for int() with base 10
  • Custom Tabgroup Appcelerator
  • $wpdb not working in file of WordPress plugin
  • Spring security and special characters
  • How to convert from System.Drawing.Color to Excel.ColorFormat in C#? Change comment color
  • Modifying destination and filename of gulp-svg-sprite
  • Trying to switch camera back to front but getting exception
  • Why winpcap requires both .lib and .dll to run?
  • Arrays break string types in Julia
  • Free memory of cv::Mat loaded using FileStorage API
  • How to set the response of a form post action to a iframe source?
  • Angular 2 constructor injection vs direct access
  • Memory offsets in inline assembly
  • Turn off referential integrity in Derby? is it possible?
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • unknown Exception android
  • Programmatically clearing map cache
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize