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 --
> <h1><%=h @article.title%></h1>
> <p><%=h @article.body %></p>
> <%=h @article.created_at%>
> <% for comment in @article.comments %>
> <hr>
> <%= 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| %>
> <p>
> <%= f.text_area :body %>
> <%= f.submit "Create" %>
> </p>
> <% 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| %>
<div id="post_<%= post.id %>" class="post">
<strong><%= post.title %></strong>
<dd><%= truncate(30, post.body) %></dd>
<%= link_to "Show post", :action => post_url(post) %>
</div>
<% end %>
./app/views/post.rhtml
<h1><%= @post.title %></h1>
<p><%= @post.body %></p>
<strong><%= #{@post.comments.length} Comments"
%></strong>
<% @post.comments.each do |comment| %>
<div id="comment_<%= comment.id %>"
class="comment">
<%= comment.comment %>
</div>
<% end %>
then the layout
./app/views/layouts/application.rhtml
<html>
<head>
<title><%= @page_title ||= "Default blog title"
%></title>
</head>
<body>
<%= yield %>
</body>
</html>
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 Matheson
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---