Redirect to another action from another controller in Rails

Asked

Viewed 788 times

2

I am developing a simple action, when saving a register if there is value in a given field the system should redirect to a create from 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 delivery after saving the user is directed to the registration of new animal. And if possible already selected in which pregnancy it was generated (in this way we can track parents and other variables.

1 answer

1


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

The first would be to render the other action, so you can use its variable @animal

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

The second would be to redirect and send via params the information you want to be already filled in the form, then receive and fill it out on the other side.

(..)
else
  redirect_to new_animal_path(reproduction_id: @reproduction.id)
end
(...)
  • For both cases is returning the following message: Completed 500 Internal Server Error in 92ms (Activerecord: 74.9ms) Argumenterror - When assigning Attributes, you must pass a hash as an argument argument.:

  • @Andrégava opa, I saw now that you are passing @reproduction as a parameter for Animal.new, you must pass a hash with the animal attributes. As I don’t know the structure I can’t tell you for sure what they are. What are the attributes of the Animal model?

  • I edited, the second form should not reproduce the error. And the first one needs to be checked which is the right field.

  • I tried both ways, apparently we are having progress, stopped giving error, for example: @animal = Animal.new(reproduction_id: @Reproduction.id) format.html {render 'Animals/new', notice: I18n.t('crud.saved') } No error in terminal. However there is a detail is not leaving the screen, the previous form is being loaded via ajax (partial), and I want to redirect to a new page (detail I’m using turbo links).

  • So for your case it will be the second way, take the parameters of the request in your action and do all the treatments. You can’t help much more than that, because you presented a very limited view of your problem.

  • 1

    http://stackoverflow.com/questions/37492886/after-submitting-a-remote-form-with-rails-how-do-i-redirect-the-user-to-another

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.