1

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
Kyle Reierson
  • 55
  • 1
  • 5
  • 2
    I need to see your main controller and the company actions to help you, in your actions `params[:symbol]`'s value should be available as the search entered – godzilla3000 Sep 05 '13 at 17:18

3 Answers3

2

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 %>
hiattp
  • 2,326
  • 1
  • 20
  • 23
  • I replaced "render :company and return" with your suggestion but I get a redirect loop – Kyle Reierson Sep 05 '13 at 18:15
  • Yeah you wouldn't want to put that in the `main#company` method or it would redirect to itself. You need to put it into whatever action is handling your `POST /company/:symbol` request, though I don't see it in your controller code. You need a `main#company_post` method somewhere. Also, I would take `:symbol` out of the `post "/company/:symbol"` route, since the `:symbol` will be in the body of that request. I'll add the controller code in the answer. – hiattp Sep 05 '13 at 18:34
  • Thanks a bunch for the help. I'm still getting the following error: ActionController::InvalidAuthenticityToken in MainController#company_post ActionController::InvalidAuthenticityToken – Kyle Reierson Sep 05 '13 at 19:06
  • Yeah the view code above won't work verbatim because it doesn't let Rails put in the CSFR (authenticity) token. You'll either need to use the rails `form_tag` view helper or skip the verification, with the former being preferred. I'll try to add the form tag code but it might need a bit of adjusting, I haven't used it that often. – hiattp Sep 05 '13 at 19:20
0

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!

Miguelgraz
  • 4,376
  • 1
  • 21
  • 16
0

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

aTei
  • 1,844
  • 4
  • 15
  • 17