Vote count:
0
I've been struggling a little getting the content of a form post request to render on a results.erb template. here is my petscontroller:
class PetsController < ApplicationController
# before_action :set_pet, only: [:show, :edit, :update, :destroy]
PETFINDER = Petfinder::Client.new('xxxxxxxxxxxxxxxx', 'xxxxxxxxxxxxxxx')
# GET /pets
# GET /pets.json
def results
@animal_list = Pet.all
def search # new
@pet = Pet.new
end
def create
Pet.destroy_all
@pets = PETFINDER.find_pets(pet_finder_type, pet_finder_zip, count: 500)
if params['pets']['breed'].empty?
@selected_animals = @pets.select do |pet|
pet.age == params['pets']['age'] &&
pet.size == params['pets']['size'] &&
pet.sex == params['pets']['sex']
end
else
@selected_animals = @pets.select do |pet|
pet.age == params['pets']['age'] &&
pet.size == params['pets']['size'] &&
pet.sex == params['pets']['sex'] &&
pet.breeds.include?(params['pets']['breeds'])
end
end
@selected_animals.each do |selected_animal|
@desired_pet = Pet.create(name: selected_animal.name)
@desired_pet.age = selected_animal.age
@desired_pet.size = selected_animal.size
@desired_pet.sex = selected_animal.sex
@desired_pet.breed = selected_animal.breeds
# @desired_pet.picture = selected_animal.photos.first.medium
@desired_pet.description = selected_animal.description
@desired_pet.shelter_id = selected_animal.shelter_id
@desired_pet.last_update = selected_animal.last_update
@desired_pet.save
end
# binding.pry
end
private
# Never trust parameters from the scary internet, only allow the white list through.
def pet_finder_type
params[:pets][:type]
end
def pet_finder_zip
params[:pets][:zip]
end
end
routes.rb
root 'welcome#index'
get '/pets' => 'pets#search'
post '/pets' => 'pets#create'
get '/pets' => 'pets#results'
I'd like to be able to call all my Pet instances and render them in pets/results.erb iterate over it and get its values. something like this:
pets/results.erb
<% @animal_pet.each do |pet| %>
<ul>
<li><%= pet.age %></li>
</ul>
<%end%>
I get a Missing template pets/create, application/create with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in: * "/Users/cyrusghazanfar/Desktop/Purrfect-Match/app/views" after I submit the a form in search.erb Could someone help me set up a correct route and method here.
asked 1 min ago
Render post request in template Ruby on Rails
Aucun commentaire:
Enregistrer un commentaire