编辑... ActionView :: Template ::错误(没有路由匹配...缺少必需的键:[:comment_id])

时间:2016-07-24 05:28:42

标签: ruby-on-rails

当我访问photos/show.html.erb视图时,我从Heroku日志中收到以下错误:

  

ActionView :: Template ::错误(没有路由匹配{:action =>" index",:comment_id => nil,:controller =>" cflags", :photo_id =>" 100"}缺少必需的密钥:[: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>

3 个答案:

答案 0 :(得分:1)

根据Rails documentationform_for方法预期参数为(record, options = {}, &block)

  

form_for(记录,选项= {},&amp;阻止) 公开

     

创建一个允许用户创建或更新属性的表单   一个特定的模型对象。

     

该方法可以以几种略微不同的方式使用,具体取决于   你希望多少依赖Rails自动推断出来   模型应该如何构建表单。对于通用模型对象,   可以通过传递form_for字符串或符号来创建表单   代表我们关注的对象:

因此,您可能希望删除在[]方法中插入的额外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 %> 

我建议您尝试这样做,看看它是否可以解决您当前的问题。

答案 1 :(得分:1)

将表单操作更改为此, 的更新

using UnityEngine;
using System.Collections;

public class CharacterControl : MonoBehaviour {

    private Animator animator;
    public bool crouched;
    private string sc;

    // Use this for initialization
    void Start () {

        animator = GetComponent<Animator> ();

    }

    // Update is called once per frame
    void Update () {

        if (crouched == true) {
            sc = "crouch";
        } else {
            sc = "standing";
        }

        animator.Play (sc + "_idle");

        if (Input.GetButton ("Fire3")) {
            if (crouched == false) {
                crouched = true;
            } else {
                crouched = false;
            }
        }

    }
}

答案 2 :(得分:0)

你的第一个参数是数组,试试这个:

.+