Skip to main content

Dynamic view based on associations

Rails • Asked by shakycode

I'm trying to add some view logic functionality to one of my apps. I have a "run" view that lists all runs and splits them up in a partial between active and pending. Each run also belongs_to :region. So what I'd like to do is have some sort of tabbed view of all runs but allow switching between regions. I can setup something like this by using a named scope where the run's region equals the string name or id of the region. But considering there could be numerous regions and they may change as time goes on I'd like to avoid iterating/scoping over regions as strings.

What would be the cleanest way in the controller and/or model to allow switching between regions in the run view?


I'm not completely sure I follow, but what about this.

You could have a route that is like /runs/region/:id and then your code could just query for that. It would always hit one controller and that controller can query for Runs.joins(:region).where(region: {permalink: params[:id]}) or something.


Sorry, I probably should have clarified things better.

I think creating a route for /runs/region/:id would work just fine and the query looks pretty solid. I'm a bit confused on the permalink that I'd be passing into the scope. Can you elaborate on that? I'm not using friendly or anything like that.


Permalink being just a pretty ID instead of a number if you wanted. You can just search on ID instead of permalink too. I just tend to always use friendly urls anymore as my default train of thought.


I need to start using friendly urls in my apps, so tired of ugly urls with ids in them. Time to do a bit of refactoring across apps and install friendly_ids.

One thing I had a question on in the query for the controller code is where am I getting the id for region from? I'm wanting to iterate over several regions (Houston, Dallas, Austin, etc) so I'm not sure how to pass that through to the controller from the view. The only thing I can think of is a dropdown that allows you to select region, shoots the region.id to the controller to query for runs matching that id. Maybe I'm making this way too confusing than it needs to be.


I'd say probably using a jQuery AJAX request. If you use a dropdown, set the value to the region's friendly ID and then write a jQuery callback to listen to the dropdown's change event.

Something like:

$("#my_dropdown").on "change", (e) ->
  e.preventDefault()
  $.ajax(
    method: "GET",
    url: "/runs/region/#{$(this).val()}",
  )

Of course you'll need to plug in your success callback to handle the response. You could have the controller return a JSON object that contains an HTML partial if you want to simply render it server side and update the page with that content. It would save you the trouble of using JS to update the page and having the view rendering logic.


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 24,647+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.