views:

2084

answers:

4

Hi,

I've got a really simple rails question here but I can't seem to find the answer anywhere. I guess some of the problems stem from me following a tutorial for Rails 1.2 with Rails 2.1. Anyway..

I'm writing a blog system and I'm implementing the comments bit. I have comments displaying fine once I've created them using script/console, but getting the comment form itself working is the hard bit.

In posts_controller.rb I have

  def comment
    Post.find(params[:id]).comments.create(params[:comment])
    flash[:notice] = "Added comment"
    #render :action => show
    redirect_to :action => show
  end

and in show.html.erb (the view) I have

<%= form_tag :action => "comment", :id => @post %>
  <%= text_area "comment", "body" %><br>
  <%= submit_tag "Post Comment" %>

When I submit the form it tries to go to the urb /posts/comment/1 which is obviously incorrect, and it complains that it can't find a template. Obviously I don't want a template there because I've told it to redirect to the show action because I want it to just re-display the post's show page, with the new comment there.

I've tried both the commented out line (render :action => show) and the redirect_to line, and neither seem to do anything at all.

I'm sure I'm missing something simple, but what is it?

A: 

yes, you use old rails style.

Something new:

   form_for :comment, :url => { :post_id => @post } do |f|
     f.text_area :body
     submit_tag "Post"
   end

you can use resources for posts and comments, search google for better tutorial or install rails 1.2.6:

gem install -v 1.2.6 rails
VitalieL
A: 

Rails 2.1 embraces "RESTful resources". show just happens to be the name of one of the predefined REST actions that all rails controllers use.

Rails does some magic behind the scenes, and :show is equivalent to "display this one specific element with a specific given ID". Sounds like it's getting mixed up with that. The ID is probably defaulting to "1". Hence the generated URL you're seeing from the render call

The Rails 2.1 way of doing it would use the following actions and templates:

  • index - displays the full list of comments
  • create - add a new comment
  • show - display a specific comment only (not the full list). Doesn't sound like this is what you want, but the "magic" inside rails will default to this.

There are also actions for new (show view to enter a new comment) edit (show view to do an edit of an existing comment) update (handle update submission) and destroy (duh), but it doesn't look like you'd use them in this example.

Do you have a link to the tutorial? Wouldn't be too hard to port it to Rails 2.1 style.

madlep
The tutorial is at http://www.sapphiresteel.com/How-To-Create-A-Ruby-On-Rails-Blog,168. I also have posted another question about this kind of issue http://stackoverflow.com/questions/224669/correct-rails-21-way-of-doing-things
robintw
A: 

You must put in the post model as well the has_many (post has many comments) and in the comments model you must add the belong_to (many comments belong to one post) then you use it as:
p = Post.find(:first) p.comments

Regards,

Victor

VP
+6  A: 

Does redirect_to :action => 'show', :id => params[:id] with quotes around show work?

RichH