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.