6

A user should be able to design a room (room is a model) in my Rails app. When the user visits myapp.com/room/1 the room with its content and specific design is shown.

The design or CSS of the room is based on room parameters (color1, color2, ...) and some random generated design characteristics (font type, image border, ...). These characteristics are stored in the room model when the room is saved.

I don't see how I can generate a specific CSS for each room. When the user visits myapp.com/room/1 my app should build specific CSS (or SCSS) for room1. Where (what controller) should I build that CSS?

Thanks

2
  • how is store design definition? What kind of information is defined? Commented Dec 12, 2014 at 16:25
  • 3
    This is a clear question to those who have programmed in Rails. Commented Dec 15, 2014 at 17:06

2 Answers 2

16

You can make your RoomsController respond to the CSS format as well in order to get this to work:

# app/controllers/rooms_controller.rb
class RoomsController
  def show
    @room = Room.find(params[:id])

    respond_to do |format|
      format.html
      format.css
    end
  end
end

Then you need to implement a template to be rendered for the CSS format:

/* app/views/rooms/show.css.erb */
.room {
  background-color: <%= @room.color1 %>;
  color: <%= @room.color2 %>;
}

Note how this is very similar to a regular template. You need to make sure that this results in valid CSS.

You need to make sure the stylesheet is included when the user visits the page. Let's say a user can browse their room design when they visit /rooms/1. This will render a HTML template, which we could define as follows:

<!-- app/views/rooms/show.html.erb -->
<% content_for :stylesheet_includes do %>
  <%= stylesheet_link_tag(room_path(@room, format: :css)) %>
<% end %>

<div class="room">
  Room Contents Here
</div>

Notice that I've used content_for around the stylesheet link tag. We can use this to make sure the stylesheet link tag is rendered nicely in the head of the layout:

<!-- app/views/layouts/application.html.erb -->
<head>
  <%= yield :stylesheet_includes %>
</head>

Of course you'll need to fill in the details yourself, but this would be the most logical approach for the problem.

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

1 Comment

my css rule is quite coplex(render chart). I have to write to scss. Is there a way to render to template like 'app/views/rooms/show.css.scss.erb' and then compile scss to css?
-1

If you design your controller to respond to the show action with json requests, you'll be able to retrieve the attributes of the Room object in json format

http://localhost.com:3000/rooms/1.json

{"id":1,"color1":"black","color2":"green","created_at":"2014-12-15T19:52:21.235Z","updated_at":"2014-12-15T19:52:21.235Z"}

In that case, you can use javascript to make the get request, retrieve the data in JSON, and subsequently use that data to manipulate the dom.

$.get( "rooms/1.json", function( data ) { alert(data); });

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.