Skip to content

Instantly share code, notes, and snippets.

@geopet
Last active December 18, 2015 16:49
Show Gist options
  • Save geopet/5814499 to your computer and use it in GitHub Desktop.
Save geopet/5814499 to your computer and use it in GitHub Desktop.
Please read the README to understand the purpose of this gist.

This gist shows the current state of a rails app I'm working on. I'm attempting to figure out the routing so that when a user submits a zipcode into the form on app/views/forecast/index.html.erb they get the same experience as if they had done http://localhost:3000/forecast/87120.

There are two main issues that I believe I am misunderstanding.

  1. How can I make the default param with forecast routing be :zip instead of :id? (Is it possible for me to just use resources :forecast in the routes.rb file?)

  2. How do I properly redirect things when I know that the params[:zip].blank? is not true so that the show method/action of the forecast controller is sent the message.

I understand that I can just change my controller to behave with a params[:id] instead of params[:zip], but I feel that there is a piece I'm missing here, and it still doesn't help me with my redirect after form submission on the index.html.erb page.

class ForecastController < ApplicationController
def show
forecast_json = ForecastGenerator.new(params[:zip]).fetch_json
@forecast = Forecast.new(forecast_json)
end
def index
end
end
<!-- app/views/forecast/index.html.erb -->
<p id="notice"><%= notice %></p>
<div class='row'>
<h2>The Weather of Geoff</h2>
</div>
<div class='row'>
<%= form_tag(url: forecast_path(forecast), method: :get) do %>
<%= label_tag(:zip, 'Zip Code') %>
<%= text_field_tag(:zip) %>
<%= submit_tag('Submit') %>
<% end %>
</div>
WogRails2::Application.routes.draw do
match 'forecast/' => 'forecast#index'
get 'forecast/:zip' => 'forecast#show', as: 'forecast'
# how can I use resources :forecast while having the route use params[:zip] instead of params[:id]?
end
@sgrif
Copy link

sgrif commented Jun 19, 2013

Routes:

resources :forecast, only: [:index, :show, :create] # => This will rename zip to id, there's no way around it besides defining your own.

Controller:

def show
  # your code is fine
end

def index
end

def create
  redirect_to forecast_path(params[:zip]) # => Or whatever you named your form input
end

View:

  <%= form_tag(url: forecasts_path, method: :post) do %>
    <%= label_tag(:zip, 'Zip Code') %>
    <%= text_field_tag(:zip) %>
    <%= submit_tag('Submit') %>
  <% end %>

@sgrif
Copy link

sgrif commented Jun 19, 2013

Alternatively:

Routes

get 'forecasts/:zip' => 'forecasts#show'
post 'forecasts' => 'forecasts#show'

@geopet
Copy link
Author

geopet commented Jun 19, 2013

@sgrif Thank you for your help! Very useful.

As follow up, for anyone who would read this after the fact, I needed to pluralize my controller name (ForecastController => ForecastsController) and I used the second routes recommendation. I also added a couple of additional routes to make the whole thing work a little cleaner.

This is my routes file after everything is said and done:

  get 'forecasts/' => 'forecasts#index'

  post '/' => 'forecasts#show'
  post 'forecasts' => 'forecasts#show'

  get '/:id' => 'forecasts#show'
  get 'forecasts/:id' => 'forecasts#show'

  root to: 'forecasts#index'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment