2014-03-30 42 views
0

構建我的第一個博客,頭正在旋轉。Link_to rails中的關係問題

我想在儀表板視圖中顯示註釋。我能夠提出評論和ID,但我真正想做的事情還是將評論的標題和評論生活並鏈接到它。從我非常業餘的理解來看,我認爲我並沒有將評論與帖子聯繫起來。

這裏是我...

Post模型的has_many:評論 評論模型belongs_to的:帖子

路線

資源:帖子做 資源:評論 結束

儀表板控制器:

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模式

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 

的最終目標是,當用戶發表新評論時,將其添加到儀表板中的警報,然後我可以從儀表板到帖子的鏈接,然後批准或刪除它。

感謝您給我的任何幫助。

回答

0

因此,從你的問題,我的理解是,你不能創建一個鏈接到帖子? 現在忘記ajax。這就是你喜歡它的後顯示頁面。

板:

[email protected] do |comment| 
    .comment 
    =link_to comment.body, comment.post 

,並在展後頁面

[email protected] do |comment| 
    .comment 
    =link_to 'Approve', approve_comment_path(comment) 
    =link_to 'Delete', comment_path(comment), method: :delete 

您需要自定義操作來設置批准或您可以重新更新操作。

+0

非常感謝,回想起來它非常有意義,我想我只是盯着太久。再次感謝。 – Gillzilla