0

I am creating a muti step form using the wicked gem. The form updates the campaign model that belongs to the user. The error I get is undefined method `update_attributes'

I am having trouble with the update action and I believe it has something to do with my params.

campaigns_controller.rb

class CampaignsController < ApplicationController
  before_action :set_campaign, only: [:show, :edit, :update, :destroy]
  before_action :correct_user, only: [:edit, :update, :destroy]
  before_action :authenticate_user!, except: [:index, :show]

  def index
    @campaigns = Campaign.all
  end

  def show
  end

  def new
    @campaign = current_user.campaigns.build
  end

  def edit
  end

  def create
    @campaign = current_user.campaigns.build(campaign_params)   
      if @campaign.save
        redirect_to campaign_steps_path
      else
        render :new  
    end
  end

  def update  
      if @campaign.update(campaign_params)
        redirect_to @campaign, notice: 'Campaign was successfully updated.' 
      else
        render :edit   
    end
  end

  def destroy
    @campaign.destroy
    redirect_to campaigns_url, notice: 'Campaign was successfully destroyed.'    
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_campaign
      @campaign = Campaign.find(params[:id])
    end

    def correct_user
      @campaign = current_user.campaigns.find_by(id: params[:id])
      redirect_to campaigns_path, notice: "Not authorized to edit this campaign" if @campaign.nil?
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def campaign_params
      params.require(:campaign).permit(:project_title, :video_url, :description, :image, :blurb, :funding_duration, :funding_goal)
    end
end

campaign_steps_controller.rb

class CampaignStepsController < ApplicationController
    include Wicked::Wizard

    before_filter :authenticate_user!

    steps :story, :perks, :team, :funding

    def show
        @campaign = current_user.campaigns.build
        render_wizard
    end

    def update
        @campaign = current_user.campaigns
        @campaign.update_attributes(campaign_steps_params)
        render_wizard @campaign 
    end



    def campaign_steps_params
      params.require(:campaign).permit(:project_title, :video_url, :description, :image, :blurb, :funding_duration, :funding_goal)
    end


end

routes.rb

Rails.application.routes.draw do
  mount Bootsy::Engine => '/bootsy', as: 'bootsy'

  resources :payment_options

  resources :orders

  resources :campaigns

  resources :campaign_steps

  devise_for :users

  root "pages#home"

  get "about" => "pages#about"

end

campaign.rb

class Campaign < ActiveRecord::Base
    belongs_to :user

    has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
    validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]

    validates :image, presence: true
    validates :video_url, presence: true
    validates :description, presence: true
    validates :blurb, presence: true
    validates :project_title, presence: true
    validates :funding_goal, presence: true
    validates :funding_duration, presence: true

    auto_html_for :video_url do
        html_escape
        image
        youtube(:width => 500, :height => 375, :autoplay => true)
        link :target => "_blank", :rel => "nofollow"
        simple_format
    end
end

user.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  has_many :campaigns

  accepts_nested_attributes_for :campaigns


end

1 Answers1

0

The problem is in @campaign = current_user.campaigns

will return a ActiveRecord Collection(more than one record)

You can'nt run update_attributes on collections

you should first find those record on which you want to run update.

 def update
        @campaign = current_user.campaigns.find(id)
        @campaign.update_attributes(campaign_steps_params)
        render_wizard @campaign 
 end
Rahul Singh
  • 3,417
  • 2
  • 25
  • 32