0

I'm sure this has been answered before but I've looked everywhere, so I apologize in advance. When I run:

<%= params %>

I get back: {"controller"=>"spree/taxons", "action"=>"show", "id"=>"women/long-sleeve"}

I'm trying to access the :id in the show action of the taxons controller. I have:

def show
  @taxon_id = params[:id]
end

This assigns 'women/long-sleeve' to @taxon_id.

Is there a way to retrieve only 'women' from 'women/long-sleeve'.

I would like to render a partial based on this, something like:

<% if @taxon_id == params[:id] %>
  <%= render 'shared/#{#taxon_id}' %>
<% end %>

But instead of rendering 'shared/women' it's trying to render 'shared/women/long-sleeve', which isn't a partial.

Thank you.

reknirt
  • 2,237
  • 5
  • 29
  • 48

3 Answers3

1

If you just want "women" from your params hash then you can do

@taxon_id = params[:id].split("/").first
rocket scientist
  • 2,427
  • 1
  • 19
  • 28
1

I would just split on / and grab the first element in the resulting array:

params[:id].split('/').first
Kyle
  • 21,978
  • 2
  • 60
  • 61
1

If it's always going to be in that format you can change your route:

match '/spree/taxons/:id/:slug' => 'taxons#show'

And your :id field will match correctly.

Richard Brown
  • 11,346
  • 4
  • 32
  • 43