【问题标题】:ActionView::Template::Error (undefined method `comment_cflags_path' for #<#<Class:0x007f423040d570>:0x007f42300dd170>ActionView::Template::Error (#<#<Class:0x007f423040d570>:0x007f42300dd170> 的未定义方法 `comment_cflags_path'
【发布时间】:2016-07-23 19:10:56
【问题描述】:

我有一个非常基本的照片和评论模型,它可以工作,然后我构建了一个用于标记 cmets 的 Cflag 模型。当我访问 photos/show.html.erb 视图时,我从 Heroku 日志中收到以下错误:

ActionView::Template::Error(未定义的方法 `comment_cflags_path' 对于#

显示:

photos/show.html.erb
.
<% @photo.comments.each do |comment| %>
  <%= form_for([comment, Cflag.new]) do |f| %>
    <%= f.hidden_field :user_id, value: current_user.id %>
    <%= f.submit "Report Inappropiate" %>
  <% end %>
<% end %> 

路线:

resources :photos do
  resources :comments do
    resources :cflags
  end
end

铁路路线:

    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
PhotosController
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

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

架构:

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"

我把表格改成:

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

Heroku:得到以下错误

ActionView::Template::Error (没有路由匹配 {:action=>"index", :controller=>"cflags", :id=>"100"} 缺少必需的键: [:comment_id, :photo_id]):

【问题讨论】:

  • 你需要在路径中传递照片和评论id,form_for([comment, url: photo_comment_cflags_path(@photo, comment)])
  • 我收到以下代码错误:ActionView::Template::Error (undefined method `model_name' for {:url=>"/photos/98/cmets/306/cflags "}:哈希):
  • 我决定开始一个新主题,因为这个主题回答了最初的问题。该主题是stackoverflow.com/questions/38549031/…
  • @Sahil 我仍然无法弄清楚,所以我提供赏金stackoverflow.com/questions/38549031/…
  • 将尝试解决它。我以为已经解决了。

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


【解决方案1】:

看看你的路线:

/photos/:photo_id/comments/:comment_id/cflags(.:format)          

您的路径还需要photo_id

您的form_for 中缺少@photo。只需将form_for 更改为:

<%= form_for([@photo, comment, Cflag.new]) do |f| %>

【讨论】:

  • 谢谢。我没有在表单助手下的 Rails 指南中找到这个 - 我错过了它还是在其他地方被覆盖?
  • 您可以在docs 中找到有关已定义关联的资源的示例。
  • 好的,我现在有机会检查一下,但它不起作用。我从 Heroku 得到的错误是:ActionView::Template::Error (No route matches {:action=&gt;"index", :comment_id=&gt;nil, :controller=&gt;"cflags", :photo_id=&gt;"98"} missing required keys: [:comment_id]): 然后我将代码更改为 [@photo, comment.id, Cflag.new],我从 Heroku 得到以下错误:ActionView::Template::Error (undefined method to_model' for 304:Fixnum` ...在 Heroku 控制台中...@987654333 @
  • 我决定开始一个新主题,因为这个主题回答了最初的问题。该主题是stackoverflow.com/questions/38549031/…
  • 我仍然无法弄清楚这一点,因此我提供了赏金。 stackoverflow.com/questions/38549031/…
猜你喜欢
  • 2014-09-17
  • 2015-06-11
  • 1970-01-01
  • 1970-01-01
  • 2013-04-10
  • 2015-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多