user1341808
user1341808

Reputation: 259

Rails 3: No Method Error for Attributes

I'm getting the following error message:

NoMethodError in UploadStepsController#update

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


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

I'm currently building a wizard that allows users to upload files, with the Wicked Wizard gem. What am I missing here?

upload_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

upload.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

new error

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

app/controllers/upload_steps_controller.rb:13:in `update'

Upvotes: 0

Views: 1317

Answers (3)

Yuri  Barbashov
Yuri Barbashov

Reputation: 5437

current_user.uploads is AREL object. So u have to specify what upload do u want to update. For example first user upload.

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

or maybe

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

or all records

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

Upvotes: 1

Sandip Mondal
Sandip Mondal

Reputation: 921

@upload = current_user.uploads, it is given you the array of uploads object, but attributes methods is apply for single object. So you have to apply each methods

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

Upvotes: 0

Ben Miller
Ben Miller

Reputation: 1484

try this instead:

@upload.update_attributes(params[:upload])

Upvotes: 0

Related Questions