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:
<%= 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.
Second, creating a directory isn’t something you really want to do in
a view because:
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.
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.
Second, creating a directory isn’t something you really want to do in
a view because:
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.
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.
On Thu, Dec 3, 2009 at 4:18 PM, Bill Mccarthy [email protected] 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.
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 [email protected] 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:
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.