English 中文(简体)
铁路3: 属性无方法错误
原标题:Rails 3: No Method Error for Attributes

我收到以下错误信息:

NoMethodError in UploadStepsController#update

undefined method `attributes=  for #<ActiveRecord::Relation:0x00000104775c40>


app/controllers/upload_steps_controller.rb:12:in `update 

我正在造一个巫师,允许用户上传文件, 使用邪恶巫师宝石。 我错过了什么?

< 强上加载_ steps_ controller.rb

class UploadStepsController < ApplicationController
include Wicked::Wizard
steps :audience, :rewards, :review

def show
    @upload = current_user.uploads
    render_wizard
end

def update
    @upload = current_user.uploads
    @upload.attributes = params[:upload]
    render_wizard @upload
end


end

< 强 > 上载.rb

class Upload < ActiveRecord::Base
attr_accessible :title, :tagline, :category, :genre, :length, :description

belongs_to :user

validates :title, presence: true
validates :tagline, presence: true
validates :category, presence: true
validates :genre, presence: true
validates :length, presence: true
validates :description, presence: true
validates :user_id, presence: true

default_scope order:  uploads.created_at DESC 
end

<强 > 新错误

NoMethodError in UploadStepsController#update
undefined method `save  for #<ActiveRecord::Relation:0x0000010159c098>

app/controllers/upload_steps_controller.rb:13:in `update 
问题回答

当前 _user. uploads is AREL 对象。 所以, u 必须指定上传要更新的内容。 例如, 第一次用户上传 。

current_user.uploads.first.update_attributes(params[:upload])

或也许

@upload = current_user.uploads.find(params[:upload].delete(:id))
@upload.update_attributes(params[:upload])

或所有记录

@upload = current_user.uploads
@upload.update_all(params[:upload])

以此代替:

@upload.update_attributes(params[:upload])

@ upload = current_user.uploads, 它给了您上传对象的数组, 但属性方法适用于单个对象。 所以, 您必须应用每种方法 。

@uploads = current_user.uploads
@uploads.each do | upload|
  upload.update_attributes(params[:upload])
end




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签