Just starting out

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

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.

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.

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
[email protected]
wrote:


Ryan B.
http://www.frozenplague.net

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs