-1

Well im going to clarify im doing it

class DuplicatesController < ApplicationController
  before_action :set_venue, only: [:new]

  def new
  end

  def create
    if @venue.duplicate(venue_params)
      flash[:success] = t('controller.create.success',
                          resource: Venue.model_name.human)
      redirect_to admin_venue_url @venue
    else
      flash[:warning] = @venue.errors.full_messages.to_sentence
      render :new
    end
  end

  private
  def set_venue
    @venue = Venue.friendly.find params[:venue_id]
  end
end

def venue_params
  params.require(:venue).permit(:name,
                                :address,
                                :address_complement,
                                :city,
                                :phone)
end

end

def duplicate

(name, address, address_complement, city, phone)

new_venue = self.dup
new_venue.update_attributes(description: self.description,
                            logo: self.logo,
                            opening_time: self.opening_time,
                            closing_time: self.closing_time,
                            ally_id: self.ally_id)
new_venue.save

end

How can I call those params in my duplicates controller, thanks I need to set the attributes, after create a dup because I want to save a new record with new information, but i dont know to do it in my method, someone could explain me Thanks.

  • could you please clarify your question or add more details to it? Are you trying to create a separate controller which has exact same actions as VenueController? You can't pass params from one controller to another, params are extracted from HTTP request. – Vbp Feb 25 '19 at 13:28
  • Thanks, yes now im doing it in a model venue def duplicate(params) new_venue = self.dup new_venue.attribute = params new_venue.save end but in controller im calling the method.duplicate from one variable – Javier Andrés Burgos Roldán Feb 25 '19 at 15:41

1 Answers1

0

Probably the best way to do it is to pass only id/slug of original model.

Then your duplicates_controller.rb can look similar to this:

class DuplicatesController < ApplicationController
  def create
    old_venue = Venue.friendly.find(params[:id])
    venue = old_venue.dup
    venue.attributes = venue_params
    if venue.save
      # success render
    else
      # error render
    end
  end

  private 

  def venue_params
    params.require(:venue).permit(:permitted_attributes) # id should not be there
  end
end

Of course you can refactor it, but I do not think it is needed in this situation.

Or my favourite is to change VenueController#create to something like this to allow creating from another instance:

if copy_from_id = params[:copy_from_id]
  @copy_source = Venue.find_by(id: copy_from_id)
  @venue = @copy_source.dup
  @venue.attributes = venue_params
else
  @venue = Venue.new
end

if @resource.save
  ...
else
  ...
end
DonPaulie
  • 2,004
  • 17
  • 26
  • Thanks, but I just want to pass some params from venue and I have a validation from my Venue.model that name is uniqueness then after I made the .dup and store in the new variable I need to set new attributes, how could I do that. – Javier Andrés Burgos Roldán Feb 25 '19 at 16:07
  • Thanks, @DonPaulie now when im triyng to assign venue.attributes = venue_params , I always have the same error undefined local variable or method `venue_params' for # Did you mean? venue_path – Javier Andrés Burgos Roldán Feb 25 '19 at 17:02
  • It means that you did not defined the method venue_params. make sure, that the last lines of first code block are there. – DonPaulie Feb 25 '19 at 21:02