ruby - Editing file in Rails (using text area) -
i want edit file stored on hdd rails in way: 1. open file , load content 'text_area' (or other field can edit it) 2. edit content in 'text_area' 3. save changes file
i have code here:
1) [controller]
def show @myfile = file.read("/home/pi/www/web-svn/repositories/repo2/hooks/post-commit.tmpl") end
2) [view]
<%= text_area_tag(:message, @myfile, :size => "100x60") %>
3) ???
here problem, how pass edited text controller again , save changes. if have better idea whole procedure can pass usefull code.
in controller's edit() method, determine filename url , read file contents specified file instance variable (@myfile). render view, passed browser, edited user, , edited text passed server post data. rails puts post data params hash , calls controller's update() method. in update() filename determined url, modified contents retrieved params hash , written file. lather, rinse, repeat.
added:
this off top of head , untested, treat pseudo-code, should going in right direction. i'm sure design can improved.
your form_tag
should below. specifies controller, action, , method request server. :file
appears because restful route should specify resource updated (see below).
form_tag({:controller => :hooks, :action => :update, :file => @myfilename}, {:method => :put}) [...] end
so, how pass filename browser can come update request? in 1 restful design, urls /hooks/:action/:file
. value of :file
specify file resource, , if set routes use form, params[:file]
available in controller. have controller do
def edit @myfilename = params[:file] @myfile = file.read(@myfilename) end
and form_tag above should work. i'll leave encoding of filename , setting routes you.
Comments
Post a Comment