newt tompkins
newt tompkins

Reputation: 13

Ruby on Rails - Show method for a parsed JSON result

I am building a seemingly simple website for a visitor to get a status update on a selected data piece. The data comes from an external API. The gist of it is this: the visitor sees a list of the data names, clicks one, and is redirected to a partial with status update, either True or False.

My index method works very well, and iterates through the data names perfectly. I believe my routing (using the friendly_id gem) should work fine. However, I cannot figure out how to properly set up the show method.

Here is my code thus far:

Controller:

class DataController < ApplicationController
  include HTTParty
  attr_accessor :name, :status

  def index
    @response = HTTParty.get("api_url").parsed_response
     respond_to do |format|
      format.json { render :json => JSON.parse(@result, :include => { :data => { :only => [:name, :status]}}) }
      format.html { render "index.html.erb" }
     end
   end


  def show
    @response = HTTParty.get('api_url').parsed_response
    respond_to do |format|
      format.json { render :json => JSON.parse(@result, :include => { :data => { :only => [:name, :status]}}) }
      format.html { render "show.html.erb" }
    end
    @name = @response.find(params[:name])
  end
end

View:

<% @response.each do |data| %>
  <ul>
    <li>
      <%= link_to data['name'].upcase, name_path(@name) %>
    </li>
  </ul>
<% end %>

Routes:

Rails.application.routes.draw do
  root 'data#index'
  get '/:name' => 'data#show', as: "name"
end

All of this together brings up the following error:

ActionController::UrlGenerationError in Data#index
Showing /app/views/layouts/_header.html.erb where line #11 raised:

No route matches {:action=>"show", :controller=>"data", :name=>nil} missing required keys: [:name]

What I am trying to accomplish is to be able to route each iterated link as 'root/{data name}', pulling {data name} from the JSON result as a parameter, and then rendering a show page with the status update information. Clearly, I have no idea how to actually capture the "name" key from the parsed JSON result as a param for the show method. I have tried creating a data_params method to contain it, but it did not work. I have tried implicitly calling the param on the find method in show (as above), to no avail. I have even tried calling a new api scrape in a params method and trying to parse the results into that, and nothing.

I'm guessing it is either some very simple mistake I'm not seeing (is my link_to actually pointing the right direction? are my api calls and JSON parsing done correctly [yes, I know I should create a separate method or helper for the api call, but that hasn't been working out for me so far--I keep breaking the app]? is this link actually supposed to call from the index method still?), or else something a bit more out of my depth.

Any ideas on where to go from here?

Upvotes: 0

Views: 800

Answers (1)

Carson Crane
Carson Crane

Reputation: 1217

In this view:

<% @response.each do |data| %>
  <ul>
    <li>
      <%= link_to data['name'].upcase, name_path(@name) %>
    </li>
  </ul>
<% end %>

You're using @name but you never actually assign a value to it in your index controller method. As a result - it gets passed to the name_path function with a value of nil. That throws an error because your route definition requires a name.

I think you want something like name_path(data['name']) or something along those lines.

Upvotes: 2

Related Questions