Skip to content

Instantly share code, notes, and snippets.

@sapient
Created November 20, 2011 22:39
Show Gist options
  • Save sapient/1381078 to your computer and use it in GitHub Desktop.
Save sapient/1381078 to your computer and use it in GitHub Desktop.
NEVER DO THIS
def show
if params["product_search"].present?
@product = Product.find_by_name(params["product_search"][:name])
if @product.blank?
redirect_to(products_path, :notice => "Not Found")
return
end
else
@product = Product.find(params[:id])
end
respond_to do |format|
format.html # show.html.erb
format.json { render json: @product }
end
end
post "products/show" => "products#show", :as => :product_search
resources :products
<h1>Listing products</h1>
<h2><%= flash[:notice] if flash[:notice].present? %></h2>
<%= form_for :product_search, :url => product_search_path do |f| %>
Search
<%= f.text_field :name %>
<% end %>
<table>
<tr>
<th>Name</th>
<th>Description</th>
<th>Price</th>
<th></th>
<th></th>
<th></th>
</tr>
<% @products.each do |product| %>
<tr>
<td><%= product.name %></td>
<td><%= product.description %></td>
<td><%= product.price %></td>
<td><%= link_to 'Show', product %></td>
<td><%= link_to 'Edit', edit_product_path(product) %></td>
<td><%= link_to 'Destroy', product, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Product', new_product_path %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment