Question

I'm working on a little stock market app where the users can lookup company stock info based on the ticker symbol. After the user has posted the :symbol param in the search field, they should be redirected to the appropriate "company" page (like Wall Street Journal, Yahoo Finance, Google Finance, etc). I'm currently able to manually type in the route with the symbol and everything works good. For example, localhost:9292/company/GOOG. I'm a total noob, so any help would be greatly appreciated. Thanks!

I currently have this in my view:

<%== search_field_tag(:symbol, "Enter symbol") %>
<%== submit_tag ("Search") %>

This is in my routes:

get  "/company/:symbol"  => "main#company"
post "/company/:symbol"  => "main#company_post"

EDIT: I'm using the MarketBeat gem to pull in the data, but I also have a Company table where I have columns symbol and name. Here is my controller:

class MainController < ApplicationController
def index
    render :index and return
end

def company
    @name = MarketBeat.company params["symbol"]
    @symbol = MarketBeat.symbol params["symbol"]
    @price  = MarketBeat.last_trade_real_time params["symbol"]
    @change = MarketBeat.change_and_percent_change params["symbol"]
    @volume = MarketBeat.volume params["symbol"]
    @days_range = MarketBeat.days_range params["symbol"]
    @eps = MarketBeat.earnings_to_share params["symbol"]
    @pe = MarketBeat.pe_ratio params["symbol"]
    @stock_exchange = MarketBeat.stock_exchange params["symbol"]


    market_cap = MarketBeat.market_capitalization params["symbol"]
    # @market_cap is rounded to billions
    @market_cap = market_cap.to_i / 1000

    render :company and return
end
Was it helpful?

Solution

In your main#company_post method, put the following:

redirect_to "/company/#{params[:symbol]}"

So the routes should be:

get  "/company/:symbol"  => "main#company"
post "/company"  => "main#company_post"

The controller:

def company_post
  redirect_to "/company/#{params[:symbol]}"
end

The view:

<%= form_tag("/company", method: :post) do %>
  <%= search_field_tag(:symbol, "Enter symbol") %>
  <%= submit_tag ("Search") %>
<% end %>

OTHER TIPS

At the end of your #company controller method you probably will do something like this

render "#{params[:symbol]}"

or

render partial: "#{params[:symbol]}"

along with have a template file with the same name of the company, like google.html.erb

Give it a try!

I make simple search system that looks almost like your task

Full example

routes.rb

  post 'search'          => 'vids#prepare_search', as: :prepare_search_vids
  get  'search(/*query)' => 'vids#search',         as: :search_vids

vids_controller.rb

  # GET /search(/*query)
  def search
    @results = Title.search params[:query] if search_query?
    if @results.count == 1
      flash[:notice] = I18n.t 'vids.search.signle_result'
      redirect_to @results[0].vid
    end
    @query = params[:query]
  end

  # POST /search
  def prepare_search
    query = params[:q] ? params[:q] : ''
    redirect_to search_vids_path(query)
  end

  private

    def search_query?
      params[:query] and !params[:query].blank?
    end

Also in your situation I recommend use asteriks instead of colon in routes http://guides.rubyonrails.org/routing.html#route-globbing-and-wildcard-segments

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top