# Editing Files in the Browser

**URL:** <https://www.ruby-forum.com/t/editing-files-in-the-browser/157519>\
**Category:** Rails\
**Created:** [January 12, 2009, 3:38pm UTC](https://www.ruby-forum.com/t/editing-files-in-the-browser/157519 "2009-01-12T15:38:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![daz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@daz](https://www.ruby-forum.com/u/daz)\
**Post date:** [January 12, 2009, 3:38pm UTC](https://www.ruby-forum.com/t/editing-files-in-the-browser/157519/1 "2009-01-12T15:38:40Z")

</div>

Hi,

Is it possible to open a file (in the public directory) in a text  
field, edit it and save it?

Is it also possible to type into a new text field, then save this as a  
new file in the public directory?

One use for this I could see would be to allow users to edit  
stylesheets/themes.

If this is possible, how do you do it?

Cheers,

DAZ

---

<div class="post-metadata">

**Author:** ![daz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@daz](https://www.ruby-forum.com/u/daz)\
**Post date:** [January 12, 2009, 4:27pm UTC](https://www.ruby-forum.com/t/editing-files-in-the-browser/157519/2 "2009-01-12T16:27:25Z")

</div>

On Mon, Jan 12, 2009 at 4:37 PM, DAZ [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> stylesheets/themes.
> 
> If this is possible, how do you do it?
> 
> Cheers,
> 
> DAZ
> 
> >

DAZ

To get the contents, you could do:  
@file\_contents = File.read(File.join(RAILS\_ROOT, “public”,  
“your\_file\_name.css”))

And to save again, do:  
File.open(File.join(RAILS\_ROOT, “public”, “your\_file\_name.css”)) do  
|file|  
file.write params[:file\_contents]  
end

NOTE: I’d be very careful of actually doing this though as there are  
MANY  
security issues.  
Think through things like who will have access to this functionality and  
how  
much they can be trusted.  
One thing to specifically check for is that the user cannot set the file  
path in any way or you could end up with files written to like:  
/home/rails/myproject/public/…/…/…/…/etc/passwd

Have a look at [Securing Rails Applications — Ruby on Rails Guides](http://guides.rubyonrails.org/security.html) for some more  
detailed info on the potential problems.

–  
Andrew T.  
[http://ramblingsonrails.com](http://ramblingsonrails.com)  
[http://www.linkedin.com/in/andrewtimberlake](http://www.linkedin.com/in/andrewtimberlake)

“I have never let my schooling interfere with my education” - Mark Twain

---

<div class="post-metadata">

**Author:** ![daz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@daz](https://www.ruby-forum.com/u/daz)\
**Post date:** [January 12, 2009, 4:58pm UTC](https://www.ruby-forum.com/t/editing-files-in-the-browser/157519/3 "2009-01-12T16:58:11Z")

</div>

Thanks for the reply Andrew, and thanks for the link - very useful and  
informative (as is your blog!).

The idea is as part of a CMS-style app, so people would have to be  
signed in to edit files, and they probably wouldn’t be able to choose  
the path, just the name. I guess I would use a similar whitelist  
approach as recommended in the docs.

Would it be better to do this sort of thing at a database level -  
saving the whole CSS text in a Theme model or something?

cheers,

DAZ

On Jan 12, 3:01 pm, “Andrew T.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![daz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@daz](https://www.ruby-forum.com/u/daz)\
**Post date:** [January 12, 2009, 5:28pm UTC](https://www.ruby-forum.com/t/editing-files-in-the-browser/157519/4 "2009-01-12T17:28:18Z")

</div>

> Would it be better to do this sort of thing at a database level -  
> saving the whole CSS text in a Theme model or something?

I think that’d be the best solution.

---

<div class="post-metadata">

**Author:** ![daz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@daz](https://www.ruby-forum.com/u/daz)\
**Post date:** [January 12, 2009, 5:31pm UTC](https://www.ruby-forum.com/t/editing-files-in-the-browser/157519/5 "2009-01-12T17:31:14Z")

</div>

On Mon, Jan 12, 2009 at 5:57 PM, DAZ [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> saving the whole CSS text in a Theme model or something?
> 
> cheers,
> 
> DAZ

There are pros and cons to everything, I just wanted you to be aware - I  
don’t like providing a solution to someone where they can shoot  
themselves  
in the foot with it 🙂

Even in a CMS based app this can be dangerous.  
If the CMS is for a specific client running on their own hardware, you  
have  
less of a problem than if it is for public consumption.

If you want to allow people to customise the look of an application, I  
would  
provide very specific things they can change.  
Your idea of a Theme model can work but still be careful of what they  
can  
set as values. IE allows javascript to execute within CSS which can open  
you  
up to XSS attacks etc.

–  
Andrew T.  
[http://ramblingsonrails.com](http://ramblingsonrails.com)  
[http://www.linkedin.com/in/andrewtimberlake](http://www.linkedin.com/in/andrewtimberlake)

“I have never let my schooling interfere with my education” - Mark Twain
