# Just starting out

**URL:** <https://www.ruby-forum.com/t/just-starting-out/122620>\
**Category:** Rails\
**Created:** [December 10, 2007, 7:47am UTC](https://www.ruby-forum.com/t/just-starting-out/122620 "2007-12-10T07:47:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![milpool](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/82dd89/32.png) [@milpool](https://www.ruby-forum.com/u/milpool)\
**Post date:** [December 10, 2007, 7:47am UTC](https://www.ruby-forum.com/t/just-starting-out/122620/1 "2007-12-10T07:47:51Z")

</div>

Hi,

I am just starting out in my Rails adventure, and I am already having  
some problems. I am using rails 2.0. I am trying to make a really basic  
blog engine with articles and comments. I have been able to make the  
articles and comment objects separately, quite easily. However I am  
having trouble getting them to link up with proper resource mapping, and  
such. I have the relationships set up ie. belongs\_to, has\_many etc. And  
tried to set up my mapping as:

* * *

## map.resources :articles do |article| article.resources :comments end

What I tried to do was to display the comments in the show page for each  
article

– show.html.erb –

# \<%=h @article.title%\>

\<%=h @article.body %\>

\<%=h @article.created\_at%\> \<% for comment in @article.comments %\>
* * *
\<%= comment.body %\> \<%end%\> \<%= render :partial =\> "comments/new" %\> \<%= link\_to 'Edit', edit\_article\_path(@article) %\> | \<%= link\_to 'Back', articles\_path %\> --

The comment loop works properly with the sample comments that I created  
initially. The partial is supposed to display a form to enter new  
comments, which is where the problems are.

– comments/\_new.html.erb –  
\<% form\_for(@comment) do |f| %\>

\<%= f.text\_area :body %\> \<%= f.submit "Create" %\>

\<% end %\> --

Obviously this doesn’t work, I think it is because of the form\_for call  
in the first line, I am not sure what to pass in, I feel there should be  
something related to the article in their.

If anyone has some advice on what I should be doing here, or what a  
better way to do this would be, I would be greatly appreciative. I  
apologize if this is the wrong place to be asking this.

Thanks in advance

-Milpool

---

<div class="post-metadata">

**Author:** ![milpool](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/82dd89/32.png) [@milpool](https://www.ruby-forum.com/u/milpool)\
**Post date:** [December 10, 2007, 5:31pm UTC](https://www.ruby-forum.com/t/just-starting-out/122620/2 "2007-12-10T17:31:51Z")

</div>

Thanks a lot,

I have actually already done most of this stuff; setting up databases,  
migrations and such. I am still a bit confused about how to set up a way  
of adding new comments from the show page of the posts. Also is there a  
way to do it without using the old link\_to method, I am trying to do  
this RESTfully.

---

<div class="post-metadata">

**Author:** ![milpool](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/82dd89/32.png) [@milpool](https://www.ruby-forum.com/u/milpool)\
**Post date:** [December 10, 2007, 9:16am UTC](https://www.ruby-forum.com/t/just-starting-out/122620/3 "2007-12-10T09:16:35Z")

</div>

Milpool Milpool wrote:

> Hi,
> 
> I am just starting out in my Rails adventure, and I am already having  
> some problems. I am using rails 2.0. I am trying to make a really basic  
> blog engine with articles and comments. I have been able to make the  
> articles and comment objects separately, quite easily. However I am  
> having trouble getting them to link up with proper resource mapping, and  
> such. I have the relationships set up ie. belongs\_to, has\_many etc. And  
> tried to set up my mapping as:
> 
> * * *
> 
> ## map.resources :articles do |article| article.resources :comments end
> 
> What I tried to do was to display the comments in the show page for each  
> article
> 
> – show.html.erb –
> 
> # \<%=h @article.title%\>
> 
> \<%=h @article.body %\>
> 
> \<%=h @article.created\_at%\> \<% for comment in @article.comments %\>
> * * *
> \<%= comment.body %\> \<%end%\> \<%= render :partial =\> "comments/new" %\> \<%= link\_to 'Edit', edit\_article\_path(@article) %\> | \<%= link\_to 'Back', articles\_path %\> --
> 
> The comment loop works properly with the sample comments that I created  
> initially. The partial is supposed to display a form to enter new  
> comments, which is where the problems are.
> 
> – comments/\_new.html.erb –  
> \<% form\_for(@comment) do |f| %\>
> 
> \<%= f.text\_area :body %\> \<%= f.submit "Create" %\>
> 
> \<% end %\> --
> 
> Obviously this doesn’t work, I think it is because of the form\_for call  
> in the first line, I am not sure what to pass in, I feel there should be  
> something related to the article in their.
> 
> If anyone has some advice on what I should be doing here, or what a  
> better way to do this would be, I would be greatly appreciative. I  
> apologize if this is the wrong place to be asking this.
> 
> Thanks in advance
> 
> -Milpool

you need the following

a database named your\_db\_name

table called posts

id int(11)  
title varchar(255)  
body text  
created\_on datetime  
updated\_on datetime

table called comments

id int(11)  
comment text  
post\_id int(11) FK \<= this is the reference to the post via the id hence  
post\_id  
created\_on datetime  
updated\_on datetime

then edit/create ./config/database.yml

adapter: mysql  
database: your\_db\_name  
password your\_db\_password  
host: localhost

the above settings need modified for the three dbs you ‘should have’  
your\_db\_name\_production  
your\_db\_name\_development  
your\_db\_name\_test

setup all three ‘trust me’

next create your controller with the appropriate methods for your app.  
A simple blog app should have a controller like so.

./app/controllers/blog\_controller.rb

and looks like

class BlogController \< ActionController::Base  
before\_filter :get\_posts, :only =\> [“index”]  
before\_filter :get\_comments, :only =\> [“post”]

def index  
@page\_title = “Your Blog Title”  
end

def post  
#show a single post  
@post = Post.find\_by\_id(params[:id])  
(@page\_title = @post.title)unless @post.nil?  
end

#------------  
private

#called by the before filter line 2  
def get\_posts  
#grab the latest 10 posts from the db  
@posts = Post.find(:all, :order =\> “created\_on ASC”, :limit =\> 10)  
end

end

then the models

./app/models/post.rb

class Post \< ActiveRecord::Base  
has\_many :comments  
end

then

./app/models/comments.rb

class Comments \< ActiveRecord::Base  
belongs\_to :post  
end

then the routes file

./config/routes.rb

map.resources posts

then the views

./app/views/blog/index.rhtml

\<% @posts.each do |post| %\>

**\<%= post.title %\>** \<%= truncate(30, post.body) %\> \<%= link\_to "Show post", :action =\> post\_url(post) %\>
\<% end %\>

./app/views/post.rhtml

# \<%= @post.title %\>

\<%= @post.body %\>

**\<%= #{@post.comments.length} Comments" %\>** \<% @post.comments.each do |comment| %\> 
 \<%= comment.comment %\> 
\<% end %\> then the layout

./app/views/layouts/application.rhtml

\<%= @page\_title ||= "Default blog title" %\> \<%= yield %\>

thats it you now have yourself a ruby on rails blog that should work. I  
didnt test the code nor did I check for typos this is an example only  
you should take advantage of the ./script/generate feature already  
available in rails.

Hope this helps you.

Best regards,

Tim M.

---

<div class="post-metadata">

**Author:** ![milpool](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/82dd89/32.png) [@milpool](https://www.ruby-forum.com/u/milpool)\
**Post date:** [December 11, 2007, 4:35am UTC](https://www.ruby-forum.com/t/just-starting-out/122620/4 "2007-12-11T04:35:54Z")

</div>

_Posts Controller, Show Action:_

@comment = Comment.new  
\*  
Posts Controller, Show View:\*

\<% form\_for @comment do |@f| %\>  
\<%= render :partial =\> “comments/form” %\>  
\<%= submit\_tag “Comment” %\>  
\<% end %\>

_Comments Controller, Form Partial:_

\<%= @f.text\_field “comment” %\>

On Dec 11, 2007 3:01 AM, Milpool Milpool  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> >

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!
