ActionController :: UrlGenerationError - Rails 4.0

时间:2014-04-13 01:31:49

标签: ruby-on-rails-4

错误消息

  

没有路线匹配{: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

PicturesController:

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

在视图artist / show中,我调用:&lt;%=渲染文件:&#39; pictures / new&#39; %GT; view pictures / new具有触发错误的代码行,具体如下:

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

我无法确定哪里出了问题。请求的操作应该是“创建”。相反&#39;索引&#39;。请有人帮助我。

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

  

*&lt;%= form_for(:picture,url:[@ artist,@ artist.pictures.new],html:{multpart:true})do | f | %GT *

,显然正在发挥作用。我会做更多的测试。

1 个答案:

答案 0 :(得分:0)

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

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 --- 这应该允许&#39;许可&#39; 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| %>