【问题标题】:ActionController::UrlGenerationError - Rails 4.0ActionController::UrlGenerationError - Rails 4.0
【发布时间】:2014-04-13 01:31:49
【问题描述】:

错误信息

没有路线匹配 {:action=>"index", :controller=>"pictures", :artist_id=>#, :format=>nil} 缺少必需 键:[:artist_id]

我的模特:

class Artist < ActiveRecord::Base
  has_many :pictures, as: :imageable, dependent: :destroy
end

class Picture < ActiveRecord::Base
  belongs_to :imageable, polymorphic: true
end

图片控制器:

class PicturesController < ApplicationController
  before_action :load_artist
  respond_to :js, :html

  def create
    @picture = @artist.pictures.new(picture_params)
    if @picture.save
      redirect_to @artist, notice: 'Thanks for your comment'
    else
   end
  end

  def destroy
    @picture = @artist.pictures.find(params[:id])
    @picture.destroy
  end

  private
  def load_artist
    @artist = Artist.find(params[:artist_id])
  end

  def picture_params
    params.require(:picture).permit(:image, :info, :imageable_id, :imageable_type)
  end
end

Routes.rb

resources :artists do
  resources :pictures
end

在我调用的视图艺术家/节目中: 视图图片/新有触发错误的代码行,即:

<%= form_for([@artist, @artist.pictures.new], html: {multpart: true}) do |f| %>
continue...

我无法确定哪里出了问题。请求的操作应该是“创建”而不是“索引”。有人可以帮帮我吗?

新闻 - 我不明白为什么,但是当我改变这个时:

**

,显然正在工作。我会做更多的测试。

【问题讨论】:

  • 您是否为图片生成了一个脚手架,如果有,请尝试通过访问 localhos:3000/pictures 来查看是否有错误,而不是添加/编辑/删除图片。如果一切正常,现在您可以为艺术家表演制作嵌套表单
  • 图片最依赖于一个艺术家,他们不需要像索引或显示这样的动作。所以这是一个嵌套资源。无论如何,我尝试了你的建议,但没有奏效。无论如何,谢谢。

标签: ruby-on-rails-4


【解决方案1】:

有很多错误。 对于您的错误输出。它说你的控制器中没有动作索引(图片)

1--

No route matches {:action=>"index", :controller=>"pictures", :artist_id=>#, :format=>nil} missing required keys: [:artist_id]

class PicturesController < ApplicationController
   #def index

   #end
end

2--- 这应该在'permit'artist_id中有

def picture_params
    params.require(:picture).permit(:image, :info, :imageable_id, :imageable_type)
  end

3--

<%= form_for @picture, html: {multpart: true} do |f| %>

【讨论】:

  • 图片最依赖于一个艺术家,他们不需要像索引或显示这样的动作。所以这是一个嵌套资源。无论如何,我尝试了你的建议,但没有奏效。
猜你喜欢
  • 2015-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-04
  • 2017-03-29
  • 2014-10-17
  • 2015-12-30
  • 2015-10-26
相关资源
最近更新 更多