System function using variables from fieldset

B

Bill Mccarthy

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:

<fieldset>
<ol>
<li>
<%= f.label :name %>
<%= f.text_field :name, :class => 'text' %>
</li>
<li>
<%= f.label :credit_balance %>
<%= f.text_field :credit_balance, :class => 'text' %>
</li>
</ol>
</fieldset>
<fieldset class="submit">
<%= f.submit 'Submit', :class => 'submit' %>
</fieldset>
<% 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!
 
R

Rick DeNatale

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:

<fieldset>
<ol>
<li>
<%=3D f.label :name %>
<%=3D f.text_field :name, :class =3D> 'text' %>
</li>
<li>
<%=3D f.label :credit_balance %>
<%=3D f.text_field :credit_balance, :class =3D> 'text' %>
</li>
</ol>
</fieldset>
<fieldset class=3D"submit">
<%=3D f.submit 'Submit', :class =3D> 'submit' %>
</fieldset>
<% 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. =A0In 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. =A0I 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.
--=20
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
 
B

Bill Mccarthy

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
 
G

Gregory Brown

Rick:

Thank you for the reply. =A0I 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
 
G

Gary Wright

=20
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 =3D "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 Wright
 
R

Rick DeNatale

Rick:

Thank you for the reply. =A0I 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)


--=20
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
 
B

Bill Mccarthy

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!
 
G

Gregory Brown

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top