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.
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
@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?– Luiz Carvalho
I edited, the second form should not reproduce the error. And the first one needs to be checked which is the right field.
– Luiz Carvalho
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).
– André Gava
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.
– Luiz Carvalho
http://stackoverflow.com/questions/37492886/after-submitting-a-remote-form-with-rails-how-do-i-redirect-the-user-to-another
– André Gava