我正在使用邪恶的宝石创建一个多步形式。表单更新属于用户的广告系列模型。我得到的错误是未定义的方法`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
答案 0 :(得分:0)
问题在于 @campaign = current_user.campaigns
将返回ActiveRecord Collection(多个记录)
你不能运行update_attributes on collections
您应首先找到要运行更新的记录。
def update
@campaign = current_user.campaigns.find(id)
@campaign.update_attributes(campaign_steps_params)
render_wizard @campaign
end