开发者

Rails: Tutorials on how to allow users to personnalize their stylesheets

I'm new to Rails (and web development in general). I've been searching for tutorials on Rails that can help me getting through a project. I've found a solution for almost all my question but one still remains.

As the title suggests, I want to enable users of my website to personnalize their css stylesheets for their personnal space. Twitter like.

Can anyone help me ? That's would be worderful.

Thanks !

EDIT:

Okay so here is my code so far (i'm using Devise)

#stylesheets_controller.rb
class StylesheetsController < ApplicationController

  def user_css
  if user_signed_in?
    @user = current_user
  end

  respond_to do |format|
    format.css
  end
end

end

My "dynamic" CSS:

#user_css.css.erb
<%=
  if @user # User log in ?
    background_color = @user.color_index
  else
    background_color = "666"
  end
%>

.cadre {
   background-color : #<%= background_color %>;
   height: 50 px;
   width: 50 px;
}

Can you tell me what I'm doing wrong because I've got this error and can't load my CSS:

Started GET "/stylesheets/user_css.css" for 127.0.0.1 at 2011-04-18 15:09:07 +0200

Processing by StylesheetsController#user_css as HTML

←[1m←[36mUser Load (2.0ms)←[0m ←[1mSELECT "user开发者_Python百科s".* FROM "users" WHERE ("users"."id" = 5) LIMIT 1←[0m

Completed 406 Not Acceptable in 127ms

If you need the code from any of my files, please tell me.


Maybe you could try creating a stylesheets controller:

class StylesheetsController < ...
  def per_user # or whatever name you like
    @user = User.find(params[:id])

    respond_to do |format|
      format.css # I didn't try it, but I guess this should work, please tell me if not
    end
  end
end

And this is your template: /app/views/stylesheets/per_user.css.erb

/* put whatever you want here */
#user-<%= @user.id %> {
  color: #666;
}

<%= @user.user_defined_css %>

And finally the route:

get '/stylesheets/per_user.css' => 'stylesheets#per_user', :as => :per_user_stylesheet

<%= stylesheet_link_tag per_user_styleseet_path %>

I guess stylesheets won't change too frequent. So you would better cache the result.

=== UPDATED ===

I have tried the above code, it was not working also.......

So this is another way I can think of, by making use of partial.

In your layout file:

# views/layouts/application.html.erb
......
<head>
  <%= render :partial => "stylesheets/user_css", :locals => {:user => @user} %>
</head>
......

And your partial contains the customized css for the user.

This may not very beautiful, but I think this should work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜