From: Rick DeNatale Date: 2009-12-04T05:54:30+09:00 Subject: Re: System function using variables from fieldset On Thu, Dec 3, 2009 at 2:34 PM, Bill Mccarthy wrote: > Hey everyone...trying to get a handle on Ruby syntax but having a bit of > an issue. > > I am trying to create a form and then call a linux system call that will > create a directory using the value of the variable within the fieldset. > Here is a short example: > >
>
    >
  1. > <%= f.label :name %> > <%= f.text_field :name, :class => 'text' %> >
  2. >
  3. > <%= f.label :credit_balance %> > <%= f.text_field :credit_balance, :class => 'text' %> >
  4. >
>
>
> <%= f.submit 'Submit', :class => 'submit' %> >
> <% system("mkdir /var/www/html/WHAT DO I PUT HERE") %> > > Basically, I just need to know the syntax of the system line so that the > directory that will be created will be the value of the :name variable > within the fieldset but cannot figure out the syntax.  In other words, > if, on the form someone puts in WHATEVER for the :name field and 10 for > the :credit_balance, I want to create a directory called > /var/www/html/WHATEVER > > Easy for me in PHP, but I have not been able to find a way to do it in > Ruby.  I am sure that it is easy for one of you. > > If someone would be nice enough to get me started in the right > direction, I would really appreciate it. First, I'm guessing that you are trying to write a Rails app, in which case you should ask on the rails list. http://groups.google.com/group/rubyonrails-talk Second, creating a directory isn't something you really want to do in a view because: 1) The code in the view gets executed before the form is sent to the user's browser, it's actually generating the html for the form. so the user hasn't entered anything into the name field yet. 2) view's should have very little logic. You would create the directory as a response to the user posting the form, typically in a create or update action in the controller, and usually indirectly as a result of calling a method on a model object. While it's common practice to mix all the view controller and model stuff up in PHP, it's not good practice, and good PHP code like mediawiki doesn't do this but I've seen too much PHP code which does. One of the good things about web frameworks like Rails is that it encourages a structure which is maintainable. Also just where you create the directory is going to depend on how you deploy the application, in Rails applications you want to typically do that under RAILS_ROOT probably in the public/files subdirectory, but again the rails group is the right place to find folks who can talk you through things like that. -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Twitter: http://twitter.com/RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale