so, trying search through external api (nutrionix) , having trouble figuring out how display results.
i have form searching
<form action = "/foods" method = "get"> <div class = "field"> <input type = "text" name = "searched_value" alt = "search foods" /> </div> <div class = "btn"> <input type = "submit" name ="btn" value = "search foods"> </div> </form>
then have in foodscontroller:
class foodscontroller < applicationcontroller require 'faraday' require 'json' def search @searched_food = params[:searched_value].split(" ").join("%20") @response_body = faraday.get("https://api.nutritionix.com/v1_1/search/#{@searched_value}?format=json?fields=item_name%2citem_id%2cbrand_name%2cnf_calories%2cnf_total_fat&appid=[myappid]&appkey=[myappkey]").body @parsed_response = json.load(@response_body) end end
my relevant routes are:
rails.application.routes.draw 'food_form' => 'foods#food_form' 'foods' => 'foods#foods' 'search' => 'foods#search' end
it goes page links "add foods" page i'm trying stuff show have:
<div class = "description"> <%= link_to "add foods", food_form_path %> <%= @parsed_response%> <%= @response_body%> <%= @searched_food %> </div>
i nothing results link form appears. however, when put controller info on html page can see results , if go nutrionix link myself, results.
help appreciated. :)
the search form isn't hitting action supposed because has action = "/foods"
should action = "/foods/search"
. changing should solve problem.
<form action = "/foods/search" method = "get"> <div class = "field"> <input type = "text" name = "searched_value" alt = "search foods" /> </div> <div class = "btn"> <input type = "submit" name ="btn" value = "search foods"> </div> </form>
Comments
Post a Comment