邪恶的形式 - 如何更新属于用户的模型 - 未定义的方法"update_attributes"



我正在使用邪恶的宝石创建一个多步骤表单。表单会更新属于用户的活动模型。我得到的错误是未定义的方法`update_attributes'

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

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

问题出现在@campaign=current_user.campagns

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

您无法运行update_attributes on collections

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

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

最新更新