图像在update + rails 4上添加了多个

时间:2016-10-19 10:40:18

标签: ruby-on-rails ruby-on-rails-4

编辑设计师页面后,即添加图像,点击保存图像后再显示,我将简要解释这个问题    考虑管理员添加设计师图像&然后管理员想要更改新图像,然后前一个图像文件显示&更新2张图片后显示

class Admin::DesignersController < Admin::AdminController
  before_filter :find_designer, :only => [:edit, :update, :destroy, :download]

 before_filter :verify_user_logged_in
  def index
   @designers = Designer.unscoped.search do |query|
   query.search_columns :name, :company, :description, :email_ids
   query.scope :sort_by, "name"
   query.paginate page_number
   query.filter params[:q]
  end
end

def new
 @designer = Designer.new
end

def create
 @designer = Designer.new(designer_params)
 if @designer.save
   redirect_to admin_designers_path
 else
  render :new
 end
end

def edit
end

def update
 if @designer.update_attributes(designer_params)
  redirect_to admin_designers_path
 else
   render :edit
 end
end

def destroy
 @designer.mark_deleted
  head :ok
end
def download
 respond_to do |format|
   format.xls {
     @products = @designer.products
     render :xls => "#{@designer.permalink}-products"
    }
 end
end

private
def find_designer
@designer = Designer.find_by_id(params[:id])
redirect_to admin_designers_path if @designer.nil?
end

private
def verify_user_logged_in
  redirect_to root_path and return if current_user.nil?
  redirect_to root_path unless current_user.internal?
end

private
 def designer_params
  params.require(:designer).permit(:name, :company, :email_ids, :phone,   :website, :designer_type, :description, :warehouse_address,  :corporate_address, :landmark, :city, :state, :pincode, :handling_shipping,  :info, :keywords, :status, designer_images_attributes: [:image])
  end
end

1 个答案:

答案 0 :(得分:0)

:id中将designer_images_attributes添加到designer_params即可。由于未提供desiner_images的id,因此每次都会创建一条新记录而不更新designer_image_attributes。