【问题标题】:undefined method `model_name'未定义的方法`model_name'
【发布时间】:2011-12-26 00:41:11
【问题描述】:

我正在为专辑控制器获取这个未定义的方法并为任何解决方案建模?...

ActiveRecord::Relation:Class 的未定义方法 `model_name'

提取的源代码(第 6 行附近):

3:>>

4:新专辑

5:

6: = form_for @albums do |f|

7: = f.error_messages

8: %p

9: = f.label :name


   =link_to 'albums', albums_path
      >>
      new album
    = form_for @album do |f|
      = f.error_messages
  %p
    = f.label :name
    = f.text_field :name

  %p
    = f.label :description
    = f.text_field :description, :rows => 3
  %p
    = f.submit

%p= link_to "Back to List", albums_path

   class AlbumsController < ApplicationController
  before_filter :authenticate_user!

  respond_to :html
  respond_to :json

  def index
    @albums = current_user.albums.paginate :page => params[:page], :per_page => 9, :order => 'created_at DESC'
    respond_with @albums, :aspect => @aspect
  end

  def create
    aspect = params[:album][:to]

    @album = current_user.post(:album, params[:album])
    if @album.persisted?
      redirect_to :action => :show, :id => @album.id, :aspect => aspect
    else
      redirect_to albums_path(:aspect => aspect)
    end
  end

  def new
    @album = Album.new
  end

  def destroy
    @album = current_user.find_visible_post_by_id params[:id]
    @album.destroy
    respond_with :location => albums_url
  end

  def show
    @person = current_user.visible_people.find_by_person_id(params[:person_id]) if params[:person_id]
    @person ||= current_user.person

    @album = :uploads if params[:id] == "uploads"
    @album ||= current_user.find_visible_post_by_id(params[:id])

    unless @album
      render :file => "#{Rails.root}/public/404.html", :layout => false, :status => 404
    else

      if @album == :uploads
        @album_id = nil
        @album_name = "Uploads"
        @album_photos = current_user.visible_posts(:_type => "Photo", :album_id => nil, :person_id => @person.id)

      else
        @album_id = @album.id
        @album_name = @album.name
        @album_photos = @album.photos
      end

      respond_with @album
    end
  end

  def edit
    @album = current_user.find_visible_post_by_id params[:id]
    redirect_to @album unless current_user.owns? @album
  end

  def update
    @album = current_user.find_visible_post_by_id params[:id]

    if current_user.update_post( @album, params[:album] )
      respond_with @album
    else
      render :action => :edit
    end
  end

end

class Album < ActiveRecord::Base
  include ROXML
  xml_attr :name
  xml_attr :description

  belongs_to :user
  belongs_to :person
  validates :person, :presence => true

  has_many :photos, :class_name => 'Photo', :foreign_key => :album_id

  validates_presence_of :name
  validates :person, :presence => true



     before_destroy :destroy_photos

      attr_accessible :name
end

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1 ruby-on-rails-plugins


    【解决方案1】:
    =form_for @albums do |f|
    

    应该是

    =form_for @album do |f|
    

    @album vs @albums,只是根据你的错误信息在这里猜测,在你的代码示例中它看起来是正确的,不知道它们会有什么不同?错误来自新操作?

    【讨论】:

      【解决方案2】:

      从您的问题中不清楚何时发生此错误 - 您已将代码发布到六种方法,但您还没有说明哪个方法导致了此错误。错误回溯的来源也与您粘贴在其下方的来源不匹配。

      错误的广义上说是您试图在看起来不够像ActiveModel 对象的东西上调用类似form_for 的东西(相反,您似乎将范围传递给form_for,这并没有没有意义)。做form_for @albums 肯定会触发这个。

      【讨论】:

        猜你喜欢
        • 2021-09-23
        • 1970-01-01
        • 2011-12-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多