Reza Parang
Reza Parang

Reputation: 57

Resource path without specifying ID

I want to render a partial if my path is on a specific resource, but I don't want to indicate the ID of the resource.

I want to load meta data tags held in a partial between my <head> tags if the path is on any video resource:

<head>
  <%= render 'layouts/partial_name' if request.fullpath == video_path(params[:id]) %>
</head>

This only works, though, if I've requested a video resource, e.g., '/videos/92'.

Upvotes: 3

Views: 152

Answers (3)

rthbound
rthbound

Reputation: 1343

I believe this is what you're looking for...

<%= render 'layouts/partial_name' if params[:controller] == "videos"  && params[:action] == "show" %>

Upvotes: 1

nan
nan

Reputation: 4338

Ideally you would want a custom layout for the controller responsible for the videos resource. More about layout can been learned at this railscast http://railscasts.com/episodes/7-all-about-layouts.

For a quick and dirty (very very dirty) solution you can check if request.fullpath contains the "videos" string. eg.

<%= render 'layouts/partial_name' if request.fullpath =~ /videos/ %>

Upvotes: -1

willglynn
willglynn

Reputation: 11510

You could try request.full_path.starts_with? videos_path, or request.full_path =~ /^\/video/.

However, there's a better way to do this. In your layout:

<head>
  <!-- ... -->
  <%= yield :head %>
</head>

In the view where you want to add meta tags:

<% content_for :head do %>
<%= render :partial => 'meta_tags' %>
<% end %>
<!-- ... -->

This usage is the canonical example for the content_for method.

Upvotes: 3

Related Questions