Redirect to another action from another controller in Rails

2

I am developing a simple action, when saving a register if there is value in a certain field the system must redirect to a create of another model / controller. I'm having trouble making this happen.

   if @reproduction.update(reproduction_params)
    if reproduction_params[:parturition].nil?
      format.html { redirect_to reproductions_path, notice: I18n.t('crud.saved') }
      format.json { render :show, status: :ok, location: @reproduction }
      format.js { render 'reproduction', animal: @animal, reproductions: @reproductions }
    else
      @animal = Animal.new(@reproduction)
      redirect_to new_animal_path
    end
  else
    format.html { render :edit }
    format.json { render json: @reproduction.errors, status: :unprocessable_entity }
    format.js { render 'edit' }
  end
end

The idea is that when saving the state of pregnancy, if there is childbirth after saving the user is directed to the registration of new animal. And if possible already selected in which gestation it was generated (this way we can trace the parents and other variables.

    
asked by anonymous 05.03.2017 / 22:16

1 answer

1

I see two ways to solve your problem, it will depend on the structure of your project.

The first one would render a render of the other action, so you can use its @animal

(...)
else
  @animal = Animal.new(reproduction_id: @reproduction.id)
  render 'animals/new'
end
(...)

The second would be to redirect and params the information you want to be filled in the form, then receive and fill in the other side.

(..)
else
  redirect_to new_animal_path(reproduction_id: @reproduction.id)
end
(...)
    
06.03.2017 / 12:45