0

我正在使用 wicked gem 创建一个多步骤表单。该表单会更新属于该用户的活动模型。我得到的错误是未定义的方法“update_attributes”

我在更新操作时遇到问题,我相信这与我的参数有关。

campaign_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

路线.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

活动.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

用户.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
4

1 回答 1

0

问题出在@campaign = current_user.campaigns

将返回一个 ActiveRecord 集合(多条记录)

你不能跑update_attributes on collections

您应该首先找到要运行更新的那些记录。

 def update
        @campaign = current_user.campaigns.find(id)
        @campaign.update_attributes(campaign_steps_params)
        render_wizard @campaign 
 end
于 2014-08-30T16:04:36.947 回答