From db to css file? - ruby-on-rails

I have a small set of css items in a db table that I need to turn into a css file to be included on a page. How can I do this in Rails3?
Is there a way I can call this directly from a stylesheet_link tag? Do I need to go through some ruby to open a file and output it in some directory first? Do I do this in a controller?
The css is actually its own model associated to the item using the css.
I do not know and looking for a solution found little yet, so asking here.
EDIT:
Simple when looked at, I was looking toward a sass or less solution and may still, but this is a simple start

I would create a Controller that retrieves the data from the database that uses a View to render the data to a Cascading Style Sheet.
You could then reference the given URL whenever you need to use the resulting stylesheet.

This is the easiest way that I have found http://blog.hasmanythrough.com/2007/10/18/simpler-than-dirt-restful-dynamic-css

Related

incorporating all views into one view

Just wondering what the shorthand would be in Rails to do this (if any):
I have views/pages/ containing 5 html.erb files and they all use the same default layout.html.erb, with one yield statement in the middle of it (the standard setup).
Now I want one view that incorporates all 5 of those erb files above contiguously, one after the other, in place of the one existing yield statement in that same layout.html.erb.
What minimal changes would I make to the layout.html.erb to accomplish this.
(Rails Newbie - like it more than Django now).
Ah,
I see what you're saying. Try this. Have your file structure such that all the views for said controller are in one folder...
#controllers_views = Dir.glob("your/controllers/views/*.erb")
#controllers_views.each { |cv| puts cv }
Seems like that would work, I'm away from my dev box or I'd test it for you.
Hope that helps.
Good luck!
You could always have a javascript that requests the sequential yields at a time interval as an ajax request. Then just your target element change to reflect the updated information.
Alternatively load all 5 into different divisions, and have them revolve visibility, like a picture gallery. CSS3 could pull this off.
http://speckyboy.com/2010/06/09/10-pure-css3-image-galleries-and-sliders/

Rails 3 - How to create custom html components that are treated with CRUD operations?

I'm using Rails 3 to create a project that will need a model called Sketch. I've already created a model, controller, and migration to handle Sketch - so far it just creates a 'sketch' object with a name for each sketch.
My problem is that I need to be able to attach an html5 canvas to each sketch object when it is created (or remove it when it is destroyed).
Since 'canvas' is not a datatype that will be stored in the database (like 'string', 'integer', or 'datetime'), how do I go about creating custom html components such as this that need to be treated like any other datatype in a Rails app?
I'm assuming that you would need to add the html components to a Model method and use a callback - like after_save - to initiate the component. But I'm not sure at all how to do this.
Not sure if I'm describing this well enough, so here is a very simple mockup:
I have the Raphael Javascript library in mind for the component that will do the sketching - if that helps.
If you can point me to any tutorials on this subject that would be great.
HTML5 canvases are rendered in the browser, not on the server where your ruby code is actually executed. Therefore I think it's safe to say that what you're asking isn't possible (at least in the way the question is phrased).
Instead you'll need to work with HTML, CSS and Javascript in your view to get the canvas working.
Canvas Tutorial / Reference
Hope this helps.
(On a related note, it's also considered a bad practice to mix view-related concepts in with your models.)

Rails: Embed metadata in templates – YAML in my HAML?

I would like to be able to set things like the page title and <meta> description from within HAML “pages” served up by my static page controller.
Is there a good way to do this? Ideally, I see it working something like:
Name files like about_us.html.haml.yaml
Use the normal render method
But now there is a hash of metadata available to my controller and layout templates, which set various headers and elements, respectively.
Thoughts?
(Since no one contributed a full answer)
If you want to set up title, description, noindex or similar tags in the head, then github.com/kpumuk/meta-tags is the best way to do it! I've used in a various projects, and think it's best gem ever for manipulating with title, description and other stuff that sits in the head tag.
— Dmitry Polushkin
It seems to work well for me, though it is a touch less powerful than what my question was looking for. Further answers welcome.

Fill a rails form with a hashmap

I have a difficult situation.
I let the the user create a form through a Rich Text Editor and then I save this.
So for example, I save this literally into my DB:
http://pastebin.com/DNdeetJp (how can you post HTML here? It gets interpreted, so now I use pastebin...)
On another page I wrap this in a form_tag and it gets presented as it should be.
What I want to do is save this as a template and save the answers as a hashmap to my DB.
This works well, but the problem is I want to recreate what checkbox/radiobutton/... is selected when the user goes back to the page. So I want to fill the form with the answers from the hashmap.
Is there a way to use a 'dummy' model or something else to accomplish this?
Thanks!
Since you're pasting in raw HTML which is not properly configured as a template, it is more difficult to enable the proper options based on whatever might be stored in your DB.
The reliable approach to making this work is to use Hpricot or Nokogiri to manipulate the bit of HTML you have and substitute values accordingly. This isn't too hard so long as you can define the elements in that form using a proper selector. For example, create a div with a unique id and operate on all input elements within it, comparing the name attribute with your properties. There may even be a library for this somewhere.
The second approach is to use JavaScript to enable the options in much the same fashion. This seems like a bit of a hack since the form itself will not have a proper default state.

Changing the order of params in pagination links

I am using Cakephp to perform a search through sphinx. I wanted to do modify the default structre of pagination links generated by cakephp
For example
From:
localhost/search/page:1/key1:google/key2:code
To:
localhost/search/key1:google/key2:code/page:1
I want the page number to appear at the end. Is there a way this can be done?
Any help appreciated
I would suggest that you modify the PaginatorHelper. I would recommend that you extend it and load it when Cake launches so that you dont modify the Cake file in cake/libs...
Then, wherever you want the output URL to be printed w/ the page number at the end, you'll need to modify the PaginatorHelper... I'd suggest you search for the page/# key/value pair. If you find it, remove it, then append it to the end of the string. Then return that value.
Edit: link to PaginatorHelper in the API - http://api.cakephp.org/class/paginator-helper

Resources