Note that there are some explanatory texts on larger screens.

plurals
  1. PORails ajax form entry shown twice
    primarykey
    data
    text
    <p>Hello I want to write a small blog with Ruby on Rails (3), with posts and comments submitted via a ajax form.</p> <p>But when I submit a comment it is often shown twice, and I got no idea why. when I write <strong>@post.comments.uniq</strong> in the _create.js.rjs file, it works fine but this seems not to be a clean solution. When I reload the page without ajax after inserting a comment the comment is also not shown twice. Only when I insert it via ajax.</p> <p>Here is the sourcecode of my project.</p> <pre><code>Blog::Application.routes.draw do root :to =&gt; 'posts#index' resources :posts do resources :comments end end </code></pre> <p><em>config/routes.rb</em></p> <pre><code>ActiveRecord::Schema.define(:version =&gt; 20100907105618) do create_table "comments", :force =&gt; true do |t| t.text "text" t.integer "post_id" t.datetime "created_at" t.datetime "updated_at" end create_table "posts", :force =&gt; true do |t| t.string "title" t.text "text" t.datetime "created_at" t.datetime "updated_at" end end </code></pre> <p><em>db/schema.rb</em></p> <pre><code>class Comment &lt; ActiveRecord::Base belongs_to :post default_scope :order =&gt; "id DESC" end </code></pre> <p><em>app/models/comment.rb</em></p> <pre><code>class Post &lt; ActiveRecord::Base has_many :comments end </code></pre> <p><em>app/models/post.rb</em></p> <pre><code>class PostsController &lt; ApplicationController def index @posts = Post.all end def show @post = Post.find(params[:id]) end end </code></pre> <p><em>app/controllers/posts_controller.rb</em></p> <pre><code>class CommentsController &lt; ApplicationController respond_to :js def create @post = Post.find(params[:post_id]) # if I write here p @post.comments.inspect # it shows that there where 2 comments with the same id, how could this be? @post.comments.create(params[:comment]) end end </code></pre> <p><em>app/controllers/comments_controller.rb</em></p> <pre><code>&lt;h2&gt;&lt;%= @post.title %&gt;&lt;/h2&gt; &lt;p&gt; &lt;%= @post.text %&gt; &lt;/p&gt; &lt;%= form_for [@post, Comment.new], :remote =&gt; true do |f| %&gt; &lt;%= f.text_area :text, :rows =&gt; 4 %&gt;&lt;br /&gt; &lt;%= f.submit "send" %&gt; &lt;% end %&gt; &lt;div id="comments_box"&gt; &lt;% if @post.comments.any? %&gt; &lt;%= render :partial =&gt; @post.comments %&gt; &lt;% else %&gt; No Comments yet &lt;% end %&gt; &lt;/div&gt; </code></pre> <p><em>app/views/posts/show.html.erb</em></p> <pre><code>&lt;div id="comment_&lt;%= comment.id %&gt;"&gt;&lt;%= comment.text %&gt;&lt;/div&gt; </code></pre> <p><em>app/views/comments/_comment.html.erb</em></p> <pre><code>page[:comment_text].clear page[:comments_box].replace_html :partial =&gt; @post.comments # ^ write here @post.comments.uniq it works page.visual_effect(:highlight, "comment_#{@post.comments.first.id}") </code></pre> <p><em>app/views/comments/create.js.rjs</em></p> <pre><code>&lt;% @posts.each do |post| %&gt; &lt;%= link_to post.title, post %&gt; &lt;% end %&gt; </code></pre> <p><em>app/views/posts/index.html.erb</em></p> <p>EDIT: </p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;Blog&lt;/title&gt; &lt;%= stylesheet_link_tag :all %&gt; &lt;%= javascript_include_tag :defaults %&gt; &lt;%= csrf_meta_tag %&gt; &lt;/head&gt; &lt;body&gt; &lt;%= yield %&gt; </code></pre> <p> <em>app/views/layouts/application.html.erb</em></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload