2016-07-24 67 views
-2

我從Heroku的收到下面的錯誤日誌,當我參觀photos/show.html.erb觀點:編輯... ::的ActionView ::模板錯誤(沒有路由匹配...缺少必需的鍵:[:COMMENT_ID])

ActionView::Template::Error (No route matches {:action=>"index", :comment_id=>nil, :controller=>"cflags", :photo_id=>"100"} missing required keys: [:comment_id])

我有非常基本的PhotoComment模型,正常工作,然後我建立了Cflag模型,用於標記註釋。我使用@photo.comments列出了photos/show.html.erb視圖中的註釋。

顯示:

# photos/show.html.erb 

<% @photo.comments.each do |comment| %> 
    <%= form_for(comment, url: photo_comment_cflags_path(@photo, comment)) do |f| %> 
    <%= f.hidden_field :user_id, value: current_user.id %> 
    <%= f.submit "Report Inappropiate" %> 
    <% end %> 
<% end %> 

CFLAGS控制器:

class CflagsController < ApplicationController 
before_action :logged_in_user 

def new 
end 

def create 
    @comment = Comment.find(params[:comment_id]) 
    @cflag = @comment.cflags.build(cflag_params) 
    if @cflag.save 
     if @comment.cflags_count > 1 
     @comment.update_attribute(:approved, false) 
     flash[:success] = "Flag created! Comment ##{@comment.id} has been removed for review. Thank you for your feedback" 
     redirect_to :back 
     else  
     flash[:success] = "Flag created! Thank you for your feedback" 
     redirect_to :back 
     end 
    else 
     redirect_to :back, notice: @cflag.errors.full_messages 
    end  
    end  

    private 
    def cflag_params 
     params.require(:cflag).permit(:user_id, :comment_id).merge(user_id: current_user.id) 
    end 
end 

路線:

resources :photos do 
    resources :comments, only: [:create, :edit, :destroy] do 
    resources :cflags, only: :create 
    end 
end 

Rails的路線:

photo_comment_cflags GET  /photos/:photo_id/comments/:comment_id/cflags(.:format)   cflags#index 
         POST  /photos/:photo_id/comments/:comment_id/cflags(.:format)   cflags#create 
new_photo_comment_cflag GET  /photos/:photo_id/comments/:comment_id/cflags/new(.:format)  cflags#new 
edit_photo_comment_cflag GET  /photos/:photo_id/comments/:comment_id/cflags/:id/edit(.:format) cflags#edit 
    photo_comment_cflag GET  /photos/:photo_id/comments/:comment_id/cflags/:id(.:format)  cflags#show 
         PATCH  /photos/:photo_id/comments/:comment_id/cflags/:id(.:format)  cflags#update 
         PUT  /photos/:photo_id/comments/:comment_id/cflags/:id(.:format)  cflags#update 
         DELETE  /photos/:photo_id/comments/:comment_id/cflags/:id(.:format)  cflags#destroy 

照片控制器:

def show 
    @photo = Photo.approved.find(params[:id]) 
end 

評論控制器:

def create 
    @photo = Photo.find(params[:photo_id]) 
    @comment = @photo.comments.build(comment_params) 
    @comment.save 
    respond_to do |format| 
    format.html { redirect_to :back } 
    format.js 
    end 
end 

CFLAG型號:

class Cflag < ActiveRecord::Base 
    belongs_to :comment, counter_cache: true 
    belongs_to :user, counter_cache: true 
    validates :user_id, presence: true 
    validates :comment_id, presence: true 
    validates :user_id, uniqueness: { 
    scope: [:comment_id], 
    message: 'You can only flag a comment once. Thank you for your feedback.' 
    } 
    default_scope -> { order(created_at: :desc) } 
end 

模式:

create_table "cflags", force: :cascade do |t| 
    t.integer "comment_id" 
    t.integer "user_id" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
end 

add_index "cflags", ["comment_id"], name: "index_cflags_on_comment_id" 
add_index "cflags", ["user_id"], name: "index_cflags_on_user_id" 

在Heroku的控制檯

p = Photo.find(100) 
p.comments = => #<ActiveRecord::Associations::CollectionProxy [#<Comment id: 309, content: "hi", photo_id: 100, user_id: 1, created_at: "2016-07-24 04:43:17", updated_at: "2016-07-24 04:43:17", approved: true, cflags_count: nil> 
+0

嗨,如果註釋是在展示頁面已報告會發生什麼? – sahil

+0

嗯,我還沒有得到頁面加載的形式,但如果它確實加載和一個cflag已經存在,那麼它會觸發:'else redirect_to:回來,注意:@ cflag.errors.full_messages'由於唯一性範圍在模型中:'validates:user_id,唯一性:{0} {0} {scope:[:comment_id],' –

回答

1

變更表單動作此, 更新

<% if @photo.comments.any? %> 
    <% @photo.comments.each do |comment| %>  
    # you do not have to use comment in form_for, because this form is for creating cflag 
    <%= form_for(:cflag, url: photo_comment_cflags_path(@photo, comment), method: "post") do |f| %> 
      <%= f.hidden_field :user_id, value: current_user.id %> 
      <%= f.submit "Report Inappropiate" %> 
    <% end %> 
    <% end %> 
<% end %> 
+0

Till現在您正在使用索引操作而不是創建操作,因爲** photo_comment_cflags **代表索引操作(注意額外的** s會更改整個含義)。 – sahil

+0

我得到以下錯誤:'ActionView :: Template :: Error(未定義的方法photo_comment_cflag_path'。但是,將其更改爲'photo_comment_cflags_path'使其工作。我已將cflag路由設置爲'only:create'。 ,因爲我試着使用相同的代碼Cflag.new和@cflag,我猜''cflag'實際上是代碼工作/不工作的區別 –

+0

你能在我的代碼中提到的url預先加上'new_'嗎? – sahil

0

你的第一個參數是數組,試試這個:

<%= form_for(comment, url: photo_comment_cflags_path(@photo, comment)) do |f| %> 
+0

我得到以下錯誤代碼:ActionView :: Template :: Error(No route matches {:action =>「index」 ,::comment_id => nil,:controller =>「cflags」,:photo_id =>「100」}缺少必需的鍵:[:comment_id]):' –

1

由於每Rails documentation,form_for方法預期的參數是(record, options = {}, &block)

form_for(record, options = {}, &block)public

Creates a form that allows the user to create or update the attributes of a specific model object.

The method can be used in several slightly different ways, depending on how much you wish to rely on Rails to infer automatically from the model how the form should be constructed. For a generic model object, a form can be created by passing form_for a string or symbol representing the object we are concerned with:

所以,你可能想要刪除您在form_for方法插入額外的[],它應該是這樣的:

<% @photo.comments.each do |comment| %> 
    <%= form_for(Cflag.new, url: photo_comment_cflags_path(@photo, comment)) do |f| %> 
    <%= f.hidden_field :user_id, value: current_user.id %> 
    <%= f.submit "Report Inappropiate" %> 
    <% end %> 
<% end %> 

我建議你試試這樣,看看它是否解決了您當前的問題。

+0

我得到以下錯誤代碼:ActionView :: Template ::錯誤(無路線匹配{:action =>「index」,:comment_id => nil,:controller =>「cflags」,:photo_id =>「100」}缺少必需的鍵:[:comment_id]):' –

+0

@TimmyVonHeiss ,我已經更新了我的答案。從表單中你需要一個新的'Cflag'而不是一個評論嗎? – Lahiru

+0

我仍然收到相同的錯誤:ActionView :: Template :: Error(無路由匹配{:action =>「index」,:comment_id => nil,:controller =>「cflags」,:photo_id =>「100 「}缺少必需的鍵:[:comment_id]):' –

相關問題