SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Sep 2007
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Ajax commenting help!

    Hi

    I am trying to implement Ajax commenting to 'stories' and am having troubles. I have a Story, Comment, and User model. At the moment, i can add comments using Ajax, but when i refresh the page, the comments dissappear. When i look in my database, i can see that the story_id field of the comments table is always NULL. So this is not being saved. Below is my code, how do i get the story_id field to be saved on creation of a new comment? I have tried changing the code, particularly in the create action of the comments controller. But whenever i do, i get errors and cannot even add any comments at all....

    comments_controller.rb:
    class CommentsController < ApplicationController

    def new
    @comment = Comment.new
    end

    def create

    @comment = Comment.new(params[:comment])
    @comment.user = User.find(current_user)
    @comment.story = Story.find_by_id
    if @comment.save
    respond_to do |format|
    format.html # all html requests
    format.js #{ render :action => "create.rjs" }# all ajax requests
    format.xml # all XML requests
    end
    end
    end

    end

    stories_controller.rb:
    class StoriesController < ApplicationController

    def show
    @story = Story.find(params[:id])
    @comments = @story.comments.find(:all)
    respond_to do |format|
    format.html # show.rhtml
    format.xml { render :xml => @story.to_xml }
    end
    end

    end

    story/show.rhtml:
    <h3>Comments:</h3>

    <div id="comments">
    <%= render :partial => 'comments/comment', :collection => @story.comments %>
    </div>

    <div id="new_comments">
    <% remote_form_for(:comment, :url => comments_path) do |f| %>
    <%= f.text_area :body, :rows => '7', :cols => '61' %> <br />
    <%= submit_tag "Add a Comment!" %>
    <% end %>
    </div>

    comment/_comment.rhtml:
    <div id="comment_<%= comment.id %>" class="comment">
    <p><%= comment.body %><br />
    Commented on <%= comment.created_at.to_s(:long) %> by <%= comment.user.login %></p>
    </div>

    In the create action;
    Doing this gave me errors: if @story.comments << @comments
    So did putting: @story = Story.find(params[:id])

    How can i alter the code to fix this? What am i doing wrong?

    Many thanks,

    PJ.

  2. #2
    SitePoint Addict ruby-lang's Avatar
    Join Date
    Aug 2007
    Posts
    389
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can pass the story_id in a hidden field. Inside you form declaration, add this line:
    Code:
    <&#37;= f.hidden_field :story_id, @story.id %>
    You can then remove this line, because the foreign key will already be set:
    Code:
    @comment.story = Story.find_by_id

  3. #3
    SitePoint Member
    Join Date
    Sep 2007
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i got it working now, thank you!

    Pj.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •