rails上的undefined方法3搭建“新”动作

时间:2011-12-31 18:29:54

标签: ruby-on-rails-3.1 routes nomethoderror

我在rails上创建了一个脚手架类,然后我尝试访问它的“新”动作,但是我收到了这个错误:

 NoMethodError in Medias#new

 Showing /Users/webcloud/Documents/Trabalhos/Reinaldo/Residencial/gerenciador/rails_estate/app/views/medias/_form.html.erb where line #1 raised:

 undefined method `media_index_path' for #<#<Class:0x007fbfdc371bf0>:0x007fbfdc36b6d8>
 Extracted source (around line #1):

 1: <%= form_for @media, :html => {:multipart => true} do |f| %>
 2:   <% if @media.errors.any? %>
 3:     <div id="error_explanation">
 4:       <h2><%= pluralize(@media.errors.count, "error") %> prohibited this media from being  saved:</h2>
 Trace of template inclusion: app/views/medias/new.html.erb

 Rails.root: /Users/webcloud/Documents/Trabalhos/Reinaldo/Residencial/gerenciador/rails_estate

 Application Trace | Framework Trace | Full Trace
 app/views/medias/_form.html.erb:1:in `_app_views_medias__form_html_erb___3954857503624418674_70230991238020'
 app/views/medias/new.html.erb:3:in `_app_views_medias_new_html_erb__3813674121976191732_70231004981260'
 app/controllers/medias_controller.rb:34:in `new'

奇怪的是它配置了资源:

 resources :medias

它的模型是使用carrierwave gem来管理上传

class Media < ActiveRecord::Base
  has_many :gallery_data
  has_many :galleries, :through => :gallery_data
  belongs_to :user
  belongs_to :category
  attr_accessible :file_name, :remote_image_url
  mount_uploader :file_name, ImageUploader
end

它的控制器方法(新)与任何脚手架方法相同:

  # GET /media/new
  # GET /media/new.json
  def new
    @media = Media.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @media }
    end
  end

我真的不知道这里发生了什么。

1 个答案:

答案 0 :(得分:0)

请参阅this answer了解您的解决方案。

总而言之,'媒体'是多元化的'媒体',而不是你所提出的'媒体'。

$ rails console
> 'media'.pluralize
 => "media"

您需要更改routes.rb(以及其他一些代码,例如控制器类名称)才能解决问题。