rails中的Link_to关系问题

时间:2014-03-30 00:50:46

标签: ruby-on-rails ruby-on-rails-4

建立我的第一个博客,头脑正在旋转。

我想在仪表板视图中显示评论。我能够提取评论和ID,但我真正想要做的还是将评论生效的帖子的标题和链接。从我非常业余的理解,我认为我没有建立从评论到帖子的联系。

这是我得到的......

发布模型has_many:评论 评论模型belongs_to:posts

路由

资源:帖子做   资源:评论 端

仪表板控制器:

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>

DB Schema

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

最终目标是,当用户发表新评论时,将其添加到信息中心中的警报,然后我可以从信息中心获取指向帖子的链接,然后批准或删除它。

感谢您提供的任何帮助。

1 个答案:

答案 0 :(得分:0)

所以从你的问题来看,我的理解是你无法创建帖子的链接? 暂时忘记ajax。这就是你喜欢它的后期展示页面。

控制板:

-@comments.each do |comment|
  .comment
    =link_to comment.body, comment.post

并在帖子显示页面

 -@comment.each do |comment|
   .comment
     =link_to 'Approve', approve_comment_path(comment)
     =link_to 'Delete', comment_path(comment), method: :delete

您需要自定义操作才能将其设置为批准,或者您可以重复使用更新操作。