# System function using variables from fieldset

**URL:** <https://www.ruby-forum.com/t/system-function-using-variables-from-fieldset/179788>\
**Category:** Ruby\
**Created:** [December 3, 2009, 8:34pm UTC](https://www.ruby-forum.com/t/system-function-using-variables-from-fieldset/179788 "2009-12-03T20:34:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![musty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/77aa72/32.png) [@musty](https://www.ruby-forum.com/u/musty)\
**Post date:** [December 3, 2009, 8:34pm UTC](https://www.ruby-forum.com/t/system-function-using-variables-from-fieldset/179788/1 "2009-12-03T20:34:31Z")

</div>

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. \<%= f.label :credit\_balance %\> \<%= f.text\_field :credit\_balance, :class =\> 'text' %\>

\<%= 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.

Thank so much!

---

<div class="post-metadata">

**Author:** ![musty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/77aa72/32.png) [@musty](https://www.ruby-forum.com/u/musty)\
**Post date:** [December 3, 2009, 9:55pm UTC](https://www.ruby-forum.com/t/system-function-using-variables-from-fieldset/179788/2 "2009-12-03T21:55:19Z")

</div>

On Thu, Dec 3, 2009 at 2:34 PM, Bill Mccarthy  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> \<%= f.label :name %\>
> 
> 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](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/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![musty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/77aa72/32.png) [@musty](https://www.ruby-forum.com/u/musty)\
**Post date:** [December 3, 2009, 10:18pm UTC](https://www.ruby-forum.com/t/system-function-using-variables-from-fieldset/179788/3 "2009-12-03T22:18:16Z")

</div>

Rick:

Thank you for the reply. I really appreciate your input.

If you were FORCED to do it this way, what would the syntax be in this  
case for this system statement to create the directory.

Thank you so much again,

Bill

Rick Denatale wrote:

> On Thu, Dec 3, 2009 at 2:34 PM, Bill Mccarthy  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > \<%= f.label :name %\>
> > 
> > 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](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/](http://talklikeaduck.denhaven2.com/)  
> Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
> WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
> LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![musty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/77aa72/32.png) [@musty](https://www.ruby-forum.com/u/musty)\
**Post date:** [December 4, 2009, 12:07am UTC](https://www.ruby-forum.com/t/system-function-using-variables-from-fieldset/179788/4 "2009-12-04T00:07:36Z")

</div>

On Thu, Dec 3, 2009 at 4:18 PM, Bill Mccarthy  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Rick:
> 
> Thank you for the reply. I really appreciate your input.
> 
> If you were FORCED to do it this way, what would the syntax be in this  
> case for this system statement to create the directory.

You basically can’t. See Rick’s first point.

-greg

---

<div class="post-metadata">

**Author:** ![musty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/77aa72/32.png) [@musty](https://www.ruby-forum.com/u/musty)\
**Post date:** [December 4, 2009, 1:14am UTC](https://www.ruby-forum.com/t/system-function-using-variables-from-fieldset/179788/5 "2009-12-04T01:14:21Z")

</div>

On Thu, Dec 3, 2009 at 4:18 PM, Bill Mccarthy  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Rick:
> 
> Thank you for the reply. I really appreciate your input.
> 
> If you were FORCED to do it this way, what would the syntax be in this  
> case for this system statement to create the directory.

If someone tried to FORCE me the syntax I would use would be something  
like:

find(someone\_else)

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![musty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/77aa72/32.png) [@musty](https://www.ruby-forum.com/u/musty)\
**Post date:** [December 4, 2009, 12:20am UTC](https://www.ruby-forum.com/t/system-function-using-variables-from-fieldset/179788/6 "2009-12-04T00:20:28Z")

</div>

On Dec 3, 2009, at 6:07 PM, Gregory B. wrote:

> On Thu, Dec 3, 2009 at 4:18 PM, Bill Mccarthy  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Rick:
> > 
> > Thank you for the reply. I really appreciate your input.
> > 
> > If you were FORCED to do it this way, what would the syntax be in this  
> > case for this system statement to create the directory.
> 
> You basically can’t. See Rick’s first point.

If you are insisting that it be done in the view, then Greg is right  
because you won’t have the necessary data (the directory name from the  
form input field) at the time the view code is executed.

If you are just asking how you dynamically construct an argument to  
system,  
use string interpolation:

dirname = “foobar”  
system(“mkdir #{dirname}”)

alternatively you can pass separate arguments to system:

system(“mkdir”, dirname)

When you pass multiple arguments, the command is not subject to shell  
expansion.

Finally, be very, very careful about constructing commands from user  
input.  
This is a great way to create a code injection security nightmare. You  
really  
have to sanitize the data before you use it to construct commands.

Gary W.

---

<div class="post-metadata">

**Author:** ![musty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/77aa72/32.png) [@musty](https://www.ruby-forum.com/u/musty)\
**Post date:** [December 4, 2009, 6:15am UTC](https://www.ruby-forum.com/t/system-function-using-variables-from-fieldset/179788/7 "2009-12-04T06:15:41Z")

</div>

Rick:

I have to admit, you write the most elegant code I have ever seen 🙂

To be honest, that is a truly GREAT ANSWER. I have been laughing since  
I read it…

Thanks for lightening my day. If you ever need anything in the VoIP  
world, let me know. There, we are fantastic. With Ruby, I am feeling  
around in the dark like the night of my Junior Prom.

Cheers!

Rick Denatale wrote:

> On Thu, Dec 3, 2009 at 4:18 PM, Bill Mccarthy  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Rick:
> > 
> > Thank you for the reply. ï¿½I really appreciate your input.
> > 
> > If you were FORCED to do it this way, what would the syntax be in this  
> > case for this system statement to create the directory.
> 
> If someone tried to FORCE me the syntax I would use would be something  
> like:
> 
> find(someone\_else)
> 
> –  
> Rick DeNatale
> 
> Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
> Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
> WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
> LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![musty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/77aa72/32.png) [@musty](https://www.ruby-forum.com/u/musty)\
**Post date:** [December 4, 2009, 8:26pm UTC](https://www.ruby-forum.com/t/system-function-using-variables-from-fieldset/179788/8 "2009-12-04T20:26:51Z")

</div>

On Thu, Dec 3, 2009 at 6:20 PM, Gary W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > case for this system statement to create the directory.
> > 
> > You basically can’t. See Rick’s first point.
> 
> If you are insisting that it be done in the view, then Greg is right  
> because you won’t have the necessary data (the directory name from the  
> form input field) at the time the view code is executed.

Unless of course you make your view multi-purpose and do your  
conditional parameter checking in there, and have the form action go  
back to the same view.  
But ugh, let’s not think about such things. 🙂

-greg
