Reputation:
My app is a web forum. Root page is a list of user-submitted categories. I click one and it links to a list of user-submitted posts about that category. I click a post and it links to a list of comments about that post. Those are the 3 tiers.
CATEGORIES INDEX This is a list of clickable categories
<% @categories.each do |category| %>
<%= link_to category.title, category %>
<% end %>
CATEGORIES SHOW I clicked a category, now I'm here looking at a list of posts
<%= render :partial => @category.posts %>
<% end %>
_POSTS The posts are rendered from this here partial
<%= div_for(post) do %>
<%= link_to post.body, Post %>
Clicking that post link takes me to POSTS INDEX.
I'm not sure if this is a desirable flow of a Rails app. It seems odd to go from Categories, to Posts, to Comments using Categories_Index, Categories_Show, and Posts_Index, respectively. I don't know how to display or submit comments from this POSTS INDEX. @comments.each do |comments| provides an error and so does the render: partial method. I can not use the same methods for Comments that I used for Categories and Posts.
MODELS Models are complete with has_many, belongs_to, etc.
CATEGORIES CONTROLLER def index @categories = Category.all end def create @category = current_user.categories.build(categories_params) end
POSTS CONTROLLER def create @category = Category.find(params[:category_id]) @post = @category.posts.new(post_params)
COMMENTS CONTROLLER def index @subcomments = Subcomment.all end def create @subcomment = current_user.subcomments.build(subcomment_params) end
ROUTES
Rails.application.routes.draw do
resources :comments
resources :posts
devise_for :users
resources :categories do
resources :posts do
end
resources :comments
end
root "categories#index"
I successfully added posts to categories. How can I add comments to posts? Is my approach correct?
Upvotes: 0
Views: 32
Reputation: 3635
I assumed you have the following Model Relationships:
Model Category
has_many :posts
Model Post
has_many :comments
belongs_to :category
Model Comment
belongs_to :post
You are asking "How can I add comments to posts?"
In the page where you render all POSTS data, you should USE posts ID as you main parameter.
So, meaning you should have post_id column/field inside Comments Table.
After saving the comments data, usually like [title, message, date ....].
In your Post Controller, you can get comments like:
// multiple Posts data
@posts = Post.all
@post.each do |post|
post.comments
...
end
//single Post
@post = Post.first // or Post.find(:id => params[:post_id])
@post.comments
If you are sending data using form, just put some hidden text field,
setting the name & value:
name="post_id"
// or something like:
name="comment[:post_id]"
//depends on how you constract the form.
Then set the value:
value="<%= params[:post_id ]%>"
Finnally, you can get the value like getting the other comments_field names.
Usually you should have this in in config/routes.rb,
resources :commets
Then your FORM path is like:
<%= form_for @comment, :url => @comments_path %>
Your Comments Controller should have like:
def index
...
end
def show
...
end
def edit
...
end
def new
@comment = Comment.new
end
def create
@comment = Comment.create(comment_params)
if @comment.save
....
redirect_to comments_path
else
.....
end
end
# For params permit in Rails 4 ^
def comment_params
params.require(:comment).permit!
end
Upvotes: 0