user1051849
user1051849

Reputation: 2337

Rails 3.1 multiple controllers for one path

We have two models, areas and stores, which we want to run off the same path: www.mysite.com/the_name_of_the_thing_here

What we would like to do is go through the areas table for a match to show the area page and, if there is no match, to go through the stores table and show the store page instead. We're not quite sure where to put this logic (in the areas controller?) and how to switch controllers. Any ideas?

Thanks

Upvotes: 0

Views: 168

Answers (1)

alex
alex

Reputation: 3742

I think you can use controller action for that, something like

@area = Area.find_by_name(params[:name])
@store = Store.find_by_name(params[:name])
if @area
  redirect_to area_path(@area)
elsif @store
  redirect_to store_path(@store)
else
  redirect_to help_url
end

If you want to change content only make other controller method in which you define variable:

@thing = Area.find_by_name(params[:name]) || Store.find_by_name(params[:name])

and pass it to view

<%= thing.name %>

Upvotes: 1

Related Questions