Search code examples
ruby-on-railsrubyimgur

Render results on #show without storing data in ActiveStorage


I'm learning RoR by building my first app (yay!). I gotta a question thought as rails guides do not cover this topic:

How to render unique results on #show to a user without storing any data in a model?

Steps I want to take:

  1. Create a basic index view with a form_tag that will allow user to submit a link (string) and click submit button
  2. Write Service Objects that will allow me to parse that link and create a response I want user to see
  3. I want to write a #show method in a separate controller that will allow me to display all the data. (I also want to parse my params[:link] in that method using Service Objects.
  4. I want to finally display this data in a table in #show view (probably I need to create a unique #show/[:id] for each user?

Here's what my app looks like at the moment (more or less):

Static Controller (just to render index.html.erb with a form)

class StaticController < ApplicationController
  def index
  end
end

Static Index view (yup, parsing imgur link here)

<h1>Hello Rails!</h1>

<%= form_tag("/images", method: "post") do %>
  <p>
    <%= label_tag(:imgur_link) %><br>
    <%= text_field_tag(:imgur) %>
  </p>
  <p>
    <%= submit_tag("Get my cards") %>
  </p>
    <% end %>

Images Controller (where all the magic SHOULD happen)

class ImagesController < ApplicationController

  def show
    @collection = params[:imgur_link]
    @service1 = service1.new(*args).call
    @service2 = service2.new(*args).call
    ...
  end
end

Images Show view

Empty as I'm stuck with the Images controller at the moment.

Any help would be more than appreciated. Thanks!


Solution

  • There is no reason you should put something into storage just in order to display it. If you get to a point when you have the results in your controller, you could just pass them to view in some @variable

    As I see, you have set up the form for step 1. If you also have routes.rb call 'images#show' for POST /images, then you will have params[:imgur_link] available in your show action. This should do:

    # config/routes.rb
    YourApplication.routes.draw do 
      # ...
      post '/images' => 'images#show'
    end
    

    Now you have to somehow process that link. Since I don't know what your results should be, I'm going to assume that you have two classes, Service1 and Service2, both of which accept an URL and return collection of results, and both collections hold the elements of the same class. Then you can leave only unique results for your show view, like this:

    # app/controllers/images_controller.rb
    class ImagesController < ApplicationController
      def show
        link = params[:imgur_link]
        results1 = Service1.new(link).results
        results2 = Service2.new(link).results
    
        @results = (results1 + results2).uniq
      end
    end
    

    Then you can do something with @results in your show view. E.g.

    # app/views/images/show.html.erb
    <% @results.each do |result| %>
      <%= result.inspect %>
    <% end %>