How to select data from API call result in rails? - html

I’m completely new to ruby on rails, I’m creating a simple article search application that would use the Guardian API and just display the news titles. It just needs to work like this: a user enters the page, fills in the search form and views the news titles.
I want to simply select the request result’s ‘webTitle’ keys and display their values as list items, but I get a big chunk of data and I’m not sure how can I do that.
Here is the request result:
{"response"=>{"status"=>"ok", "userTier"=>"developer", "total"=>2153270, "startIndex"=>1, "pageSize"=>10, "currentPage"=>1, "pages"=>215327, "orderBy"=>"relevance", "results"=>[{"id"=>"books/2017/jul/16/fall-down-7-times-get-up-8-naoki-higashida-review-autism", "type"=>"article", "sectionId"=>"books", "sectionName"=>"Books", "webPublicationDate"=>"2017-07-16T06:00:13Z", "webTitle"=>"Fall Down 7 Times Get Up 8 review – a window on the world of autism", "webUrl"=>"https://www.theguardian.com/books/2017/jul/16/fall-down-7-times-get-up-8-naoki-higashida-review-autism", "apiUrl"=>"https://content.guardianapis.com/books/2017/jul/16/fall-down-7-times-get-up-8-naoki-higashida-review-autism", "isHosted"=>false, "pillarId"=>"pillar/arts", "pillarName"=>"Arts"}, {"id"=>"football/2017/jul/07/gold-cup-2017-predictions-usa-mexico-costa-rica-football", "type"=>"article", "sectionId"=>"football", "sectionName"=>"Football", "webPublicationDate"=>"2017-07-07T09:00:08Z", "webTitle"=>"Gold Cup picks: USA to tip under-strength Mexico and in-form Costa Rica", "webUrl"=>"https://www.theguardian.com/football/2017/jul/07/gold-cup-2017-predictions-usa-mexico-costa-rica-football", "apiUrl"=>"https://content.guardianapis.com/football/2017/jul/07/gold-cup-2017-predictions-usa-mexico-costa-rica-football", "isHosted"=>false, "pillarId"=>"pillar/sport", "pillarName"=>"Sport"}, {"id"=>"world/2017/jul/15/stream-of-floating-bodies-near-mosul-raises-fears-of-reprisals-by-iraqi-militias", "type"=>"article", "sectionId"=>"world", "sectionName"=>"World news", "webPublicationDate"=>"2017-07-15T08:00:01Z", "webTitle"=>"Stream of floating bodies near Mosul raises fears of reprisals by Iraqi militias", "webUrl"=>"https://www.theguardian.com/world/2017/jul/15/stream-of-floating-bodies-near-mosul-raises-fears-of-reprisals-by-iraqi-militias", "apiUrl"=>"https://content.guardianapis.com/world/2017/jul/15/stream-of-floating-bodies-near-mosul-raises-fears-of-reprisals-by-iraqi-militias", "isHosted"=>false, "pillarId"=>"pillar/news", "pillarName"=>"News"}]}}
API consumer class:
#app/clients/guardian_api_client.rb
class GuardianApiClient
include HTTParty
API_KEY = ENV['GUARDIAN_CONTENT_API_KEY']
BASE_URL ="https://content.guardianapis.com/search?"
API_PARTIAL_URL = "api-key=#{API_KEY}"
def query(q)
request = HTTParty.get(BASE_URL+"q=#{q}&""api-key=#{API_KEY}")
puts request
request
end
end
Controller:
class SearchController < ApplicationController
def search
#app = GuardianApiClient.new
#results = #app.query(params[:q])
end
end
View:
<%= form_with(url: '/search') do |f| %>
<%= f.text_field :q %>
<%= f.submit 'search' %>
<% end %>
<% if #results != nil %>
<ul>
<%= #results.each do |r| %>
<li><%= r["webTitle"] %></li>
<% end %>
</ul>
<% else %>
<p>No results yet</p>
<% end %>
Routes:
Rails.application.routes.draw do
get '/search' => 'search#search'
post '/search' => 'search#search'
end

The response is some JSON, so you need to learn how to map through it and get the results that you want.
To see the data more clearly try printing it with:
puts JSON.pretty_generate(#results)
in your controller, then see the output in your rails console.
Anyway, you have a few options:
Option 1: Likely you just need to drill down further into #results in your view. In the JSON that is returned, the webTitles are nested, so changing the third line below should work. Also note on that line that I removed the = sign to prevent the return value from being displayed.
<% if #results != nil %>
<ul>
<% #results["response"]["results"].each do |r| %>
<li><%= r["webTitle"] %></li>
<% end %>
</ul>
<% else %>
<p>No results yet</p>
<% end %>
Option 2: You may consider getting the list of articles in your controller, which I think was your original intent and also is probably more "rails" like:
class SearchController < ApplicationController
def search
#app = GuardianApiClient.new
#results = #app.query(params[:q])
#articles = #results["response"]["results"].map do |article|
article
end
end
end
In your view, then call render to a partial:
<%= render 'articles' %>
Then create a partial view called _articles.html.erb in whatever directory your other view is in, and then add some code to display each article:
<ul>
<% #articles.each do |article| %>
<li><%= article["webTitle"] %> <% link_to 'Link', article["webUrl"] %></li>
<% end %>
<ul>
By separating out each article that was returned in the #articles array, it will probably be easier for you to get other attributes as well in a more readable way. As you can see, above I included a link to the actual article.

Related

Trying to load a view in application layout and calling its action on every page

I am trying to load a view with a list of Product Type. I need the view to be displayed on every page. But the action is not getting called in other pages, please tell me where I'm going wrong and suggest any alternatives.
index.html.erb
<h1>Listing the types of products</h1>
<% content_for :sidebar do %>
<% #types.each do |type| %>
<li><%= link_to type.name, product_type_path(type)%> <!-- go to product_type controller show action-->
<%= link_to "Edit", edit_product_type_path(type) %> <!-- go to product_type controller edit action -->
<%= link_to "Delete", product_type_path(type) , method: :delete, data: { confirm: "Are U Sure?" }%> <!-- go to product_type controller delete action and a pop to confirm the action -->
</li>
<% end %>
<h3><%= link_to "New Type Of Product", new_product_type_path %></h3>
<h3><%= link_to "All Types of Products", products_path %></h3> <!-- All types of products listed for admin's ease -->
<% end %>
This is the Application layout I'm using.
Application.html.erb
<!DOCTYPE html>
<html>
<head>
<%= render 'layouts/title' %>
<%= render 'layouts/rails_defaults'%>
<%= render 'layouts/shim' %>
</head>
<body>
<aside><%= yield: sidebar %></aside>
<%= render 'layouts/header' %>
<%= render 'layouts/flash' %>
<div class="container">
<%= yield %>
</div>
<%= render 'layouts/footer' %>
</body>
</html>
As you can see I'm using yield: sidebar , but its not working properly, in the sense the action index is not getting called if I go to different page.
Product_types Controller
class ProductTypesController < ApplicationController
def index
#types = ProductType.all #to get all the records to an instance variable(array in the case) lasts only until the scope lasts. Since the is defined in index, it lasts only till you are using the index view
end
def new
#type = ProductType.new #to create a new record into the respective model
end
def show
#type = ProductType.find(params[:id]) #Finding the type of product click on
#products = Product.where(value: #type.value) #finding all the products whose value field is same as the type of product value(primary key)
end
def create
#type = ProductType.new(type_params) #type params defined below in private class
if #type.save #save the product created
redirect_to root_url #and redirect to root
else #if doesnt save and error occurs
render 'new' #error occurs and render 'new' view
end
end
def edit
#type = ProductType.find(params[:id])
end
def update
#type = ProductType.find(params[:id])
if #type.update(type_params) #update the params
redirect_to root_url #if updated redirect to root
else
render 'edit' #else if error occurs render 'edit' view again
end
end
def destroy
ProductType.find(params[:id]).destroy #destroy the record
redirect_to root_url #redirect to root
end
private
def type_params
params.require(:product_type).permit(:name,:value) #used to only permit type and value thorugh request(to prevent hacking) used in create and update action above
end
end
The action is not getting called whenever i jump to another page.
please suggest alternatives.
content_for / yield are for the current view only invoked by just the current action. So if the other pages dont have a content_for :sidebar in their views, then they wont have a sidebar. And if you just include in the content_for directly, it still wont execute any extra controller logic required.
Use a helper or partial like your layouts/header if you want resuable content, rather than a reusable "space".
For things you dont want to do only in a partial, but also not only in direct Ruby (with content_tag, etc.), you might combine a partial and a helper.
class ApplicationHelper # Or any other helper
def products_sidebar
products = Product.where(show_on_sidebar: true).order(:popularity) # Or whatever you like
render partial: "shared/products_sidebar", locals: {products: products}
end
end
shared/_products_sidebar.html.erb (Id definitely give consideration to other template engines)
<div id="products_sidebar">
<% products.each do |product| %>
<div><%=product.name%></div> <!--whatever you want it to look like-->
<% end %>
</div>
Then in your index, you can just call it, and it wont depend on the action/controller currently being processed.
<body>
<aside><%= products_sidebar %></aside>

Rails: Get form to only render when certain conditions are met

Im trying to get this loop to only render the reviews form for services which doesnt already have a review. I can't get it to function properly. Any ideas?
<% #services.each do |service| %>
<% if service == #booked && !#hasReview %>
<%= form_for(service, service.reviews.new) do |f| %>
<label>Create review for</label> <%= label_tag service.title %>
<div id="user_stars"></div>
<div class form-group>
<%= f.text_area :comment, rows: 3, class: "form-control" %>
</div>
<%= f.hidden_field :service_id, value: service.id %>
<div class="actions">
<%= f.submit "Create", class: "btn btn-primary" %>
</div>
<% end %>
<% end %>
<% end %>
The #booked and #hasReview actions are working correctly by themselves. So I guess Im setting it up wrongly with the IF
EDIT:
services_controller.rb
def show
#user = User.find(params[:id])
#services = #user.services
#booked = Booking.where("service_id = ? AND user_id = ?", #service.id, current_user_id).present? if current_user
#reviews = #service.reviews
#hasReview = #reviews.find_by(user_id: current_user_id) if current_user
end
reviews_controller.rb
def create
#review = current_user.reviews.create(review_params)
redirect_to request.referer
end
Your logic looks good, so I'm guessing your problem is with this part of your if statement:
if service == #booked
Judging from this line in your services_controller:
#booked = Booking.where("service_id = ? AND user_id = ?", #service.id, current_user_id).present? if current_user
it looks like the #booked instance variable is a boolean. So you're comparing a boolean to service, but it doesn't look like service is a boolean.
So you'll want to change the if statement so that you're comparing two equivalent types, and in your case you probably want to compare two boolean values.

Search box in ruby on rails not displaying results

I'm working with Ruby on Rails, trying to get my search bar to display the results. I've tried searching for similar issues, but most already had search working and weren't getting it to work right.
in my html I have:
<%= form_tag users_path, :method => 'get' do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
<div class="scrollBox">
<%= will_paginate%>
<ul>
<% #users.each do |user| %>
<li>
<%= link_to user.name, user, {:class=>"signout-style"} %>
</li>
<% end %>
</ul>
<%= will_paginate %>
</div>
In my users controller I have:
def index
#users = User.paginate(page: params[:page])
#searches = User.search(params[:search])
end
In my users model I have:
def self.search(search)
if search
find(:all, :conditions => ['name LIKE ?', "%#{search}%"])
else
find(:all)
end
end
I was using the railscast episode #37
Any help is appreciated!
Update the index action as below:
def index
#users = User.search(params[:search]).paginate(page: params[:page])
end
Currently, you are storing the search results in instance variable #searches with this line:
#searches = User.search(params[:search])
BUT in your view you are accessing #users which is set as #users = User.paginate(page: params[:page]). So, you always see all the users in the view and not the searched users.
UPDATE
As you are using Rails 4.0.2, I would suggest you to refactor your search method as below:
def self.search(search)
if search
where('name LIKE ?', "%#{search}%")
else
all
end
end

How can I populate a div with Rails code when a link is clicked?

I'm trying to populate some li tags with this code:
<% #events.each do |event| %>
<li>
<span class="eventname"><%= event.name %></span>
<span class="eventlocation"><%= event.location %></span>
</li>
<% end %>
And I have some links that say "today", "tomorrow" etc and when I hit today I want the events from today to show up. If I hit tomorrow I want tomorrow's to show up. I've looked everywhere through AJAX tutorials and javascript tutorials and I'm completely lost on how to do this in rails.
One way I considered doing is having this block of code be a partial, and change it slightly for each partial so that the specific events that I want to show come up. But then I'm still stuck with the same problem, I'm not sure how to show a specific partial based on the click of a link.
We will have one partial that would render a set of given events (any type) let's call it _events.html.erb
_events.html.erb:
<% events.each do |event| %>
<li>
<span class="eventname"><%= event.name %></span>
<span class="eventlocation"><%= event.location %></span>
</li>
<% end %>
Then we would create an action named fetch_events that returns a js response.
Class EventsController < ApplicationController
def fetch_events
#events = Event.where(type: params[:type])
respond_to do |format|
format.js
end
end
end
ofcourse in routes.rb
match 'fetch_events/:type' => 'events#fetch_events', as: :fetch_events
now we build out fetch_events.js.erb, this file will set the contents of the div with id 'events' to the rendered partial.
fetch_events.js.erb:
$('#events').html(<%= escape_javascript(render :events, events: #events) %>)
Now in your page view add a have div with id 'events' that will contain the response, while
the ajax could be called using link_to
<%= link_to 'today', fetch_events_path(type: :today), :remote => true %>
<%= link_to 'tomorrow', fetch_events_path(type: :tomorrow), :remote => true %>

Placing link at top

How do I place a link at the top of my page when the URL that it is pointing to is not determined until later down the page. In this example, I want to move Create and Edit Scenario links to the top of the page, but as you can see Edit Scenario depends on knowing the #scenario_id first.
<%= will_paginate #scens, :next_label => 'Older', :prev_label => 'Newer' %>
<div class="box">
<% for scenario in #scens %>
<% #created = scenario.created_at %>
<% #updated = scenario.updated_at %>
<% #scenario_id = scenario.id %>
<% if scenario.scenario_image.exists? %>
<%= scenario_image_tag(scenario) %>
<% end %>
<%= simple_format(scenario.description) %>
<% end %>
</div>
<% if session[:role_kind] == "controller" %>
<p>
<%= button_to "Create new scenario", :action => "create" %>
<% if #scens.size > 0 %>
<%= button_to "Edit scenario", :action => "edit", :id => #scenario_id %>
<% end %>
</p>
You can add the link at the top but you will need to programmatically access it later and then assign the URL to it. That needs some kind of reference or look-up capability, I'm thinking client-side javascript but that's as I don't know Ruby.
Alternatively you could create the link later when you have the URL and place the link at the top using CSS positioning. The actual position of all the DOM elements on the page need not match the order in which they are rendered.
One way to do this is to use a helper:
In your helper.rb file:
def stack_example(scens, &block)
html = 'Scenario Details'
edit_link = 'Edit Link'
yield html, edit_link
end
Then in your partial you could have something like:
<% stack_example(#scens) do |html, edit_link| %>
<%= edit_link %><br>
<%= html %>
<% end %>
Should output the following:
Edit Link
Scenario Details
I don't get it. Why do you create model in the view layer? Why wouldn't you create the model variables in the controller? Sth like:
class your_controller
def your_method
#scenario_id = ...
end
end
I think that your problem lays in the invalid MVC usage. Don't you think that all the #member #variables should be initialized before the view starts to render?