【发布时间】:2014-05-09 10:56:11
【问题描述】:
构建我的第一个博客,头晕目眩。
我正在尝试在我的仪表板视图中显示 cmets。我可以提取 cmets 和 ID,但我真正想做的是将帖子的标题放在评论中并链接到它。从我非常业余的理解来看,我认为我没有建立从 cmets 到 post 的连接。
这是我得到的……
发布模型 has_many :cmets 评论模型 belongs_to :posts
路线
资源:帖子做 资源:cmets 结束
仪表板控制器:
class DashboardController < ApplicationController
before_filter :authorize, only: [:index]
def index
@posts = Post.all
@comments = Comment.all
end
end
评论控制器:
class CommentsController < ApplicationController
before_filter :authorize, only: [:destroy, :show]
def create
@post = Post.find(params[:post_id])
@comment =
@post.comments.create(comments_params)
redirect_to post_path(@post)
end
def destroy
@post = Post.find(params[:post_id])
@comment = @post.comments.find(params[:id])
@comment.destroy
redirect_to post_path(@post)
end
private
def comments_params
params.require(:comment).permit(:commenter, :body)
end
end
帖子控制器:
class PostsController < ApplicationController
before_filter :authorize, only: [:edit, :update, :destroy, :create, :new]
def index
@posts = Post.where(:state => "published").order("created_at DESC")
end
def new
@post = Post.new
end
def show
@post = Post.find(params[:id])
redirect_to_good_slug(@post) and return if bad_slug?(@post)
end
def create
@post = Post.new(post_params)
if @post.save
redirect_to dashboard_path
else
render 'new'
end
end
def edit
@post = Post.find(params[:id])
end
def update
@post = Post.find(params[:id])
if @post.update(params[:post].permit(post_params))
redirect_to dashboard_path
else
render 'edit'
end
end
def destroy
@post = Post.find(params[:id])
@post.destroy
redirect_to dashboard_path
end
private
def post_params
params.require(:post).permit(:title, :text, :author, :short, :photo, :state)
end
end
仪表板视图:
</div>
<div class="fluid dash">
<div class="gridContainer clearfix">
<div class="fluid dash_post_list">
<h3>Manage Posts</h3>
<% @posts.each do |post| %>
<div class="fluid list-items">
<div class="fluid list_each">
| <%= post.id %> | <%= link_to post.title, post %>, <%= post.created_at.strftime('%b, %d') %> - <%= post.state %>
</div>
<div class="fluid crud">
<i class="fa fa-pencil-square-o"></i><%= link_to 'Edit', edit_post_path(post) %>
<i class="fa fa-times-circle"></i><%= link_to 'Delete', post_path(post),
method: :delete, data: { confirm: 'Are you sure?' } %>
</div>
</div>
<% end %>
</div>
<div class="fluid dash_right">
<div class="fluid create_new">
<h3>Create New</h3>
<%= link_to 'New Post', new_post_path %>
</div>
<div class="fluid alerts">
<h3>Alerts!</h3>
<% @comments.each do |comment| %>
<%= comment.post_id %>
<% end %>
</div>
</div>
</div>
</div>
数据库架构
create_table "comments", force: true do |t|
t.string "commenter"
t.text "body"
t.integer "post_id"
t.datetime "created_at"
t.datetime "updated_at"
t.boolean "flag", default: false
end
create_table "posts", force: true do |t|
t.string "title"
t.text "text"
t.datetime "created_at"
t.datetime "updated_at"
t.text "short"
t.text "author"
t.string "photo_file_name"
t.string "photo_content_type"
t.integer "photo_file_size"
t.datetime "photo_updated_at"
t.string "state"
end
最终目标是,当用户发表新评论时,将其添加到仪表板中的警报中,然后我可以从仪表板获取帖子的链接,然后批准或删除它。
感谢您能给我的任何帮助。
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4