2017-03-11 74 views
0

嗨,我想添加評論到我的引腳模型。我可以添加評論,但我似乎無法獲得我的編輯和刪除操作的正確鏈接。Ruby on Rails編輯和刪除帖子評論

評論控制器

  class CommentsController < ApplicationController 
       before_action :find_pin 
       before_action :find_comment, only: [:destroy, :edit, :update] 
       def index 

       end 

       def show 

       end 

       def new 
        @comment = Comment.new 
       end 

       def create 

        @comment = @pin.comments.build(comment_params) 
        @comment.user_id = current_user.id 

        @comment.save 

        if @comment.save 
         flash[:notice] = "Created comment" 
         redirect_to pin_url(@pin.id) 
        else 
         render 'new' 
        end 


       end 

       def edit 

       end 

      def update 
      @comment = @pin.comments.find(params[:id]) 
       end 

       def destroy 

       @comment.destroy 
       flash[:notice] = "Deleted Comment" 
       redirect_to pin_url(@pin.id) 
       end 


      private 


       def find_pin 

       @pin = Pin.find(params[:id]) 
       end 

       def find_comment 

        @comment = Comments.find(params[:pin_id]) 

       end 

       def comment_params 
        params.require(:comment).permit(:content) 
       end 
      end 

註釋形式視圖

  <%= form_for ([@comment.build]) do |f| %> 
       <p><%= f.text_area :content %></p> 

       <p><%= f.submit %></p> 
      <% end %> 

評論顯示視圖

 <%= @comment.each do |comment| %> 

      <p><%= comment.content %></p> 


      <% if current_user == comment.user %> 
      <%= link_to "Edit comment", edit_comment_path(@pin, comment)%> 
      <%= link_to "Delete comment", [comment.pin,comment], method: :delete %> 
      <% end %> 




     <% end %> 

路由

  Rails.application.routes.draw do 
      match '/users', to: 'users#index', via: 'get' 
      match '/users/:id',  to: 'users#show',  via: 'get' 




      get 'homes/show' 

      devise_for :users, :path_prefix => 'd' 
      resources :pins do 
      member do 
       resources :comments 
       put "like", to: "pins#upvote" 
      end 
      end 
      resources :users, :only =>[:show] 

      root "pins#index" 
     end 

銷控制器

    class PinsController < ApplicationController 
         before_action :find_pin, only: [:show, :edit, :update, :destroy, :upvote] 
         before_action :authenticate_user!, except: [:index, :show] 

         def index 
          @pins = Pin.all.order("created_at DESC") 

         end 

         def show 

          @comment= Comment.where(pin_id: @pin).order("created_at DESC") 

         end 


         def new 
          @pin = current_user.pins.build 
         end 

         def create 
          @pin = current_user.pins.build(pin_params) 

          if @pin.save 
           redirect_to @pin, notice: "Successfully created new Pin" 
          else 
           render 'new' 
          end 
         end 

         def edit 
          if @pin.user != current_user 
          redirect_to root_path 
         end 
         end 

         def update 
          if @pin.update(pin_params) && @pin.user == current_user 
          redirect_to @pin, notice: "Pin was successfully updated" 
         else 
          render 'edit' 
         end 
         end 

         def destroy 
          if @pin.user == current_user 
          @pin.destroy 
          redirect_to root_path 
          else 
          redirect_to root_path, notice: "Not Your Pin!" 
          end  
         end 

         def upvote 
          @pin.upvote_by current_user 
          redirect_to :back 
         end 


         private 

         def pin_params 
          params.require(:pin).permit(:title, :description, :image) 
         end 

         def find_pin 
          @pin = Pin.find(params[:id]) 
         end 
        end 

在此先感謝我一直都停留在這一段時間 出於某種原因,引腳ID和評論ID落得相同或東西 http://localhost:3000/pins/30/comments/30

+0

既然你已經嵌套資源在你的路由,父資源的型號名稱作爲前綴的路徑的輔助方法。即'pin_comment_path'。 –

+0

好還在學習,我的下一步是什麼我應該用什麼編輯註釋link_到「<%= link_to」編輯註釋「,」? –

+0

我知道不幸的是重建這最終大聲笑。只是想獲得一些東西來展現我無數個小時的笑聲 –

回答

1

運行rake routes檢查所有路由你有你的應用程序。你會發現所有的路線都將以複數命名。所以edit_pins_comments_path將只適用於您的應用程序。但正如你的模型所表明的那樣,每個銷都會有很多評論。要做到這一點,您需要將您的評論資源嵌入到每個引腳的成員中。所以你的路線就像下面一樣。

resources :pins do 
    member do 
    resources :comments 
    put "like", to: "pins#upvote" 
    put "favorite", to: "pins#favorite" 
    end 
end 

此代碼將產生edit_comment_pathcomment_path

相關問題