Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

John Stegman
John Stegman
12,182 Points

Creating drop down of customers with link to show action

I am transferring an MS Access db to Rails and I need to be able to have a drop down list of customers (last_name, first_name, id) that will allow me to select a customer and generate the customer show page. I've been able to make the drop down but I can't figure out how to create the link to the show page

<select name="cust_id">
  <%= @customers.each do |customer| %>
    <option value="<%= customer.id %>"><%= customer.last_name %>, <%= customer.first_name %> </option>
  <% end %>
</select>

1 Answer

Jay McGavren
STAFF
Jay McGavren
Treehouse Teacher

In your other question you mention this app is using jQuery, which is good, because unless you want users to also have to click a Submit button, you'll need to use JavaScript. This StackOverflow answer has some code that you can probably adapt:

<select id="dynamic_select">
  <option value="" selected>Pick a Website</option>
  <option value="http://www.google.com">Google</option>
  <option value="http://www.youtube.com">YouTube</option>
  <option value="https://www.gurustop.net">GuruStop.NET</option>
</select>

<script>
    $(function(){
      // bind change event to select
      $('#dynamic_select').on('change', function () {
          var url = $(this).val(); // get selected value
          if (url) { // require a URL
              window.location = url; // redirect
          }
          return false;
      });
    });
</script>