1

My application has pages of two basic types: forms and tables.

As such, I have two different CSS files, forms.css and tables.css.

In my application layout file (application.html.erb), I'd like to load different stylesheets depending on some sort of flag set in a given view.

For example, <%= defined?(@tables) : stylesheet_link_tag 'tables' ? stylesheet_link_tag 'forms' %>.

The above snippet doesn't actually work, but that's what I'm trying to accomplish?

Any ideas?

3 Answers 3

2

You should move this to a before_filter in your controller. Keep the view lightweight.

In the view:

<%=stylesheet_link_tag @foo %>

before_filter in Controller:

before_filter :get_css_file

def get_css_file
  @foo = defined?(@tables) ? 'tables' : 'forms'
end

I presume you set @tables in your controller, so you might have to adjust your logic, but you get the idea. In fact you already know if it's a table or form page controller, probably, so you'd basically just be setting @foo directly: @foo = 'tables' etc.

Sign up to request clarification or add additional context in comments.

1 Comment

How'd one go about doing that?
2

I've just tried a similar thing and it works for me. Your code isn't quite right, perhaps you just need to change it to

<%= stylesheet_link_tag(defined?(@tables) ? 'tables' : 'forms') %>

Comments

1

Your ternary operator syntax is wrong, if that's what you're trying to do. I think you mean this:

<%= defined?(@tables) ? stylesheet_link_tag 'tables' : stylesheet_link_tag 'forms' %>

The question mark (?) and colon (:) changed places.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.