Reputation: 139
Once the pet is saved, I want to redirect to treats/show controller. I tried doing this but it takes me back to pets/show.
def create
@pets = Pet.new(pet_params)
respond_to do |format|
if @pet.save
format.html { redirect_to @treat}
format.json { template: treats/show, status: :created, location: @pet }
else
format.html { render :new }
format.json { render json: @pet.errors, status: :unprocessable_entity }
end
end
end
Upvotes: 0
Views: 63
Reputation: 76784
@pets
variable should be @pet
...Like this:
def create
@pet = Pet.new pet_params
--
2. You aren't defining your @treat
variable:
@treat = Treat.find params[:treat_id] ?
This is shown up here:
{:action=>"show", :controller=>"treat", :id=>nil}
So the answer is that you need to define @treat
. Because I don't know your associations, or how you want @treat
to be populated, there are two ways to create it:
Either pull it from the association
(IE @pet.treat
) or invoke it explicitly: (@treat -
)...
For simplicity, you're best doing the following:
def create
@pets = Pet.new pet_params
@treat = Treat.find [[[num - where do you get this from?]]]
...
end
--
If you had your models set up as such:
#app/models/pet.rb
class Pet < ActiveRecord::Base
has_many :treats
end
#app/models/treat.rb
class Treat < ActiveRecord::Base
belongs_to :pet
end
You could use the following:
def create
@pet = Pet.new pet_params
@treat = @pet.treat.new treat_params
respond_to do ...
end
private
def treat_params
params.require(:treat).permit(:treat, :params)
end
Upvotes: 1
Reputation: 396
You would do this for your redirect path:
format.html { redirect_to(treat_path(@treat) }
Upvotes: 1