Rails 3 上未定义的方法搭建“新”行动

发布于 2024-12-23 17:46:56 字数 1787 浏览 0 评论 0原文

我正在 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

它的模型正在使用载波 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

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

I was creating a scaffolded class on rails and then I tried to access it's "new" action but i got this error:

 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'

the weird part is that it have the resource configured:

 resources :medias

And it's model is using the carrierwave gem to manage uploads

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

It's controller method (new) is just the same as any scaffolded method:

  # 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

I don't really know what's going on here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感受沵的脚步 2024-12-30 17:46:56

请参阅此答案了解您的解决方案。

总而言之,“媒体”是复数形式的“媒体”,而不是您所说的“媒体”。

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

您需要更改您的routes.rb(可能还需要更改一些其他代码,例如控制器类名称)才能纠正问题。

See this answer for your solution.

To summarize, 'media' is pluralized 'media', not 'medias' as you have put.

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

You'll need to change your routes.rb (and maybe some other code such as controller class names) in order to correct the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文