Ok to wrzucam kodzik
Controller Posts
[code]class PostsController < ApplicationController
def index
@posts = Post.all
end
def show
@post = Post.find(params[:id])
@comment = @post.comments.build
end
def new
@post = Post.new
end
def create
@post = Post.new(params[:post])
if @post.save
redirect_to posts_path, :notice => "Post was saved"
else
render "new"
end
end
def edit
@post = Post.find(params[:id])
end
def update
@post = Post.find(params[:id])
if @post.update_attributes(params[:post])
redirect_to posts_path, :notice => "Post was updated"
else
render "edit"
end
end
def destroy
@post = Post.find(params[:id])
@post.destroy
redirect_to posts_path, :notice => "Post has been deleted "
end
end[/code]
Controller comments
[code]class CommentsController < ApplicationController
def index
@post = Post.find(params[:post_id])
@comments = @post.comments
end
def show
@post = Post.find(params[:post_id])
@comment = @post.comments.find(params[:id])
end
def new
@post = Post.find(params[:post_id])
@comment = @post.comments.build
end
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.build(params[:comment])
if @comment.save
redirect_to post_comment_url(@post, @comment)
else
render :action => “new”
end
end
def edit
@post = Post.find(params[:post_id])
@comment = @post.comments.find(params[:id])
end
def update
@post = Post.find(params[:post_id])
@comment = Comment.find(params[:id])
if @comment.update_attributes(params[:comment])
redirect_to post_comment_url(@post, @comment)
else
render :action => “edit”
end
end
def destroy
@post = Post.find(params[:post_id])
@comment = Comment.find(params[:id])
@comment.destroy
redirect_to post_comments_path(@post)
end
end[/code]
oraz widok post#show
[code]
<%= @post.title%>
<%= @post.content %>
<%= @post.created_at %>
Comments
<% @post.comments.each do |c| %>
Commenter:
<%=h c.commenter %>
<p>
<b>Comment:</b>
<%=h c.content %>
</p>
<% end %>
<%= link_to ‘Edit Post’, edit_post_path(@post) %> |
<%= link_to ‘Back to Posts’, posts_path %>
<%= link_to ‘Manage Comments’, post_comments_path(@post) %>[/code]
błąd jaki otrzymuję uninitialized constant Post::Comment
kompletnie nie czaje jak zaimplementować i używać relacji ;(
Pozdrawiam