This is my first post. I just started playing with Rails a couple of days ago after seeing the ONLamp tutorial via Slashdot. My company has a MS SQL Server only rule, so I spent some time figuring out how to go through the tutorial with everything set for SQL Server instead of MySQL. That all went fine until I get to my own customizations. Could someone please explain how I would call a stored procedure using a button or link on the html pages? I haven''t been able to find any documentation at all on calling stored procedures via Ruby/Rails. Any guidance would be greatly appreciated. Thanks, Seth
Seth Fleishman wrote:>This is my first post. I just started playing with Rails a couple of >days ago after seeing the ONLamp tutorial via Slashdot. > >My company has a MS SQL Server only rule, so I spent some time >figuring out how to go through the tutorial with everything set for >SQL Server instead of MySQL. That all went fine until I get to my own >customizations. > >Could someone please explain how I would call a stored procedure using >a button or link on the html pages? I haven''t been able to find any >documentation at all on calling stored procedures via Ruby/Rails. Any >guidance would be greatly appreciated. > >Seth, Rails/ActiveRecord relies on db schema inspection for infering most of the boring SQL code you won''t have to write anymore (less code, less bugs, more productivity). Coming from a .NET and SQL Server background myself, I know it''s a good practice to encapsulate all your SQL code in stored procs, for various reasons, performance and security being the most cited. But not in the Rails world, no my friend, you don''t have to that. As I said, ActiveRecord itself will write most of the SQL code for you, but, if you must, you can always use the following syntax to execute SPs: exec[ute] your_sp_name argument1, argument2, ... I hope that helps. Rgds, Demetrius
Demetrius Nunes wrote:> Seth Fleishman wrote: > >> This is my first post. I just started playing with Rails a couple of >> days ago after seeing the ONLamp tutorial via Slashdot. >> >> My company has a MS SQL Server only rule, so I spent some time >> figuring out how to go through the tutorial with everything set for >> SQL Server instead of MySQL. That all went fine until I get to my own >> customizations. >> >> Could someone please explain how I would call a stored procedure using >> a button or link on the html pages? I haven''t been able to find any >> documentation at all on calling stored procedures via Ruby/Rails. Any >> guidance would be greatly appreciated. >> >> > Seth, > > Rails/ActiveRecord relies on db schema inspection for infering most of > the boring SQL code you won''t have to write anymore (less code, less > bugs, more productivity). > > Coming from a .NET and SQL Server background myself, I know it''s a > good practice to encapsulate all your SQL code in stored procs, for > various reasons, performance and security being the most cited. But > not in the Rails world, no my friend, you don''t have to that. > > As I said, ActiveRecord itself will write most of the SQL code for > you, but, if you must, you can always use the following syntax to > execute SPs: > > exec[ute] your_sp_name argument1, argument2, ...After reading my post, I noticed that maybe this got a little too loose... Here''s a more concrete example using AR to clarify: class MyClass < ActiveRecord::Base def self.select_some_records connection.select_all "exec my_stored_proc ''arg1'', ''arg2''" end end
Demetrius Nunes wrote:> Coming from a .NET and SQL Server background myself, I know > it''s a good practice to encapsulate all your SQL code in > stored procs, for various reasons, performance and security > being the most cited. But not in the Rails world, no my > friend, you don''t have to that.Perhaps it would be a worthy addition to ActiveRecord, if there was a way of specifying stored procedures as methods of classes. For each procedure you would need to specify which parameter corresponds to "self", so that when calling it, only the other parameters need specifying. While new users should be encouraged to discover the beauty/wisdom/coolness of "the Ruby Way", I think that one of Ruby''s strengths is it''s ability to integrate pretty tightly into "the Microsft Way" (ASP, COM, etc). I''ve only just started with Ruby myself, so I don''t know enough about how ActiveRecord works to extend it the way I want to. Yet. Adelle.
Thanks, Demetrius. How would I reference to it? I know I''m being old-fashioned, but say I want clicking on a link to run the stored procedure spDeleteTemp, would I put it in the :action section of <%= link_to rs, :action =>>? If so how? Now, let me take your example. If the sp is: DELETE FROM tblRecipe_temp WHERE id=@recipe. How would I go about using your ActiveRecord method? Much obliged... Seth On Mon, 24 Jan 2005 20:25:44 -0300, Demetrius Nunes <demetrius-fDpYTK8McCzCdMRJFJuMdgh0onu2mTI+@public.gmane.org> wrote:> Seth Fleishman wrote: > > >This is my first post. I just started playing with Rails a couple of > >days ago after seeing the ONLamp tutorial via Slashdot. > > > >My company has a MS SQL Server only rule, so I spent some time > >figuring out how to go through the tutorial with everything set for > >SQL Server instead of MySQL. That all went fine until I get to my own > >customizations. > > > >Could someone please explain how I would call a stored procedure using > >a button or link on the html pages? I haven''t been able to find any > >documentation at all on calling stored procedures via Ruby/Rails. Any > >guidance would be greatly appreciated. > > > > > Seth, > > Rails/ActiveRecord relies on db schema inspection for infering most of > the boring SQL code you won''t have to write anymore (less code, less > bugs, more productivity). > > Coming from a .NET and SQL Server background myself, I know it''s a good > practice to encapsulate all your SQL code in stored procs, for various > reasons, performance and security being the most cited. But not in the > Rails world, no my friend, you don''t have to that. > > As I said, ActiveRecord itself will write most of the SQL code for you, > but, if you must, you can always use the following syntax to execute SPs: > > exec[ute] your_sp_name argument1, argument2, ... > > I hope that helps. > > Rgds, > Demetrius > >
Now I got it. You were right about my MVC absorption. I appreciate the help. On Tue, 25 Jan 2005 01:00:00 -0300, Demetrius Nunes <demetrius-fDpYTK8McCzCdMRJFJuMdgh0onu2mTI+@public.gmane.org> wrote:> Seth Fleishman wrote: > > >How would I reference to it? I know I''m being old-fashioned, but say I > >want clicking on a link to run the stored procedure spDeleteTemp, > >would I put it in the :action section of <%= link_to rs, :action =>>? > >If so how? > > > >Now, let me take your example. If the sp is: > >DELETE FROM tblRecipe_temp WHERE id=@recipe. > >How would I go about using your ActiveRecord method? > > > Seth, I am sensing that your grasp of the MVC pattern hasn''t yet been > absorbed completely. I am saying this because you should not try to > reference a link to a stored procedure directly. Instead, you should map > a link to a action in the controller and make the controller call the > appropriate method in the model class that would finally call the stored > procedure. > > Maybe something like this: > > In the view you would have: > <%= link_to "Delete Temporary Recipe", :controller => "recipe", :action > => "delete_temp" %> > > In the recipe_controller.rb you would have: > def delete_temp > Recipe.delete_temporary @params["recipe"]["id"] #or something like this > end > > In the model recipe.rb you would have: > def self.delete_temporary(id) > connection.select_one "exec spDeleteTemp #{id}" > end > > Adjust as needed. > > Rgds > >
James G. Stallings II
2005-Jan-25 17:28 UTC
Multiple apps on shared server with vhost/dedicated port
Greetings All. I''m a ruby convert from the world of perl. I''m especially charged up with ruby on rails, a wonderful bit of technology that addresses perfectly the niche for which it is intended. Kudos. We seem a bit short on some of the details though. I suspect that this is largely because it is relatively new. In any case, here''s my issue: I got ruby 1.9, gem, rails, mysql all up and cooking on my panther mac system. I worked the onlamp tutorial, modifying the project on the fly from a recipe book to a contact list interface, That I was able to do this thoroughly and successfully on my first encounter with the framework is a testament to the brilliance of the framework. This is not to say I didn''t have problems all along the way; I did. But they were all without exception issues of system adminsitration concerning the installation of the new ruby/gem/mysql software on OS X. My problem is this: In moving from the simple ruby webserver provided with rails to apache 1.3.3, the apache configuration required is less than trivial and not especially well documented; I can''t for the life of me get it to cook. Perhaps the documentation I need is there and just not easy to find. The server configuration philosophy is to accomodate several more traditional website developement projects on port 80 and various rails experiments on dedicated ports via vhosts. Currently only the contact app is present, as I am attempting to prototype a developement environment and associated policies for working with ruby/rails before putting it to work ;) The rails app is parked at /Library/WebServer/Documents/contact/ Following are the httpd.conf and .htaccess files; when accessing the server at http://192.168.1.37:3000/ I get a forbidden from the server; this is because there is no index capability turned on in the app ''public'' directory. My suspicion is that my rewrite ruleset in .htaccess is broken, but I am too weak there to know heads from tails. Thanks for any assistance; sorry about the text file dumps. Cheers! Twitch Here''s my Apache config: ## ## httpd.conf -- Apache HTTP server configuration file ## ServerType standalone ServerRoot "/usr" LockFile "/private/var/run/httpd.lock" PidFile "/private/var/run/httpd.pid" ScoreBoardFile "/private/var/run/httpd.scoreboard" Timeout 300 KeepAlive On MaxKeepAliveRequests 100 KeepAliveTimeout 15 MinSpareServers 10 MaxSpareServers 55 StartServers 10 MaxClients 150 MaxRequestsPerChild 100000 Listen 80 Listen 3000 BindAddress * # # Note: The order in which modules are loaded is important. Don''t change # the order below without expert advice. # # Example: # LoadModule foo_module libexec/mod_foo.so LoadModule vhost_alias_module libexec/httpd/mod_vhost_alias.so LoadModule config_log_module libexec/httpd/mod_log_config.so LoadModule mime_module libexec/httpd/mod_mime.so LoadModule negotiation_module libexec/httpd/mod_negotiation.so LoadModule includes_module libexec/httpd/mod_include.so LoadModule autoindex_module libexec/httpd/mod_autoindex.so LoadModule dir_module libexec/httpd/mod_dir.so LoadModule fastcgi_module libexec/httpd/mod_fastcgi.so LoadModule cgi_module libexec/httpd/mod_cgi.so LoadModule asis_module libexec/httpd/mod_asis.so LoadModule imap_module libexec/httpd/mod_imap.so LoadModule action_module libexec/httpd/mod_actions.so LoadModule userdir_module libexec/httpd/mod_userdir.so LoadModule alias_module libexec/httpd/mod_alias.so LoadModule rewrite_module libexec/httpd/mod_rewrite.so LoadModule access_module libexec/httpd/mod_access.so LoadModule auth_module libexec/httpd/mod_auth.so LoadModule setenvif_module libexec/httpd/mod_setenvif.so LoadModule hfs_apple_module libexec/httpd/mod_hfs_apple.so # Reconstruction of the complete module list from all available modules # (static and shared ones) to achieve correct module execution order. # [WHENEVER YOU CHANGE THE LOADMODULE SECTION ABOVE UPDATE THIS, TOO] ClearModuleList AddModule mod_vhost_alias.c AddModule mod_log_config.c AddModule mod_mime.c AddModule mod_negotiation.c AddModule mod_include.c AddModule mod_autoindex.c AddModule mod_dir.c AddModule mod_fastcgi.c AddModule mod_cgi.c AddModule mod_asis.c AddModule mod_imap.c AddModule mod_actions.c AddModule mod_userdir.c AddModule mod_alias.c AddModule mod_rewrite.c AddModule mod_access.c AddModule mod_auth.c AddModule mod_so.c AddModule mod_setenvif.c AddModule mod_hfs_apple.c # Port: The port to which the standalone server listens. For # ports < 1023, you will need httpd to be run as root initially. # Port 80 User www Group www ServerAdmin twitch-kuaf+BvAnvdypLqBFPtG/w@public.gmane.org ServerName Opticon.local DocumentRoot "/Library/WebServer/Documents" <Directory /> Options FollowSymLinks AllowOverride None </Directory> <Directory "/Library/WebServer/Documents"> # # This may also be "None", "All", or any combination of "Indexes", # "Includes", "FollowSymLinks", "ExecCGI", or "MultiViews". # # Note that "MultiViews" must be named *explicitly* --- "Options All" # doesn''t give it to you. # Options Indexes FollowSymLinks ExecCGI MultiViews # This controls which options the .htaccess files in directories can # override. Can also be "All", or any combination of "Options", "FileInfo", # "AuthConfig", and "Limit" # AllowOverride None Order allow,deny Allow from all </Directory> <IfModule mod_dir.c> DirectoryIndex index.html index.fcgi index.cgi index.rb </IfModule> AccessFileName .htaccess <Files ~ "^\.ht"> Order allow,deny Deny from all Satisfy All </Files> # UseCanonicalName: (new for 1.3) With this setting turned on, whenever # Apache needs to construct a self-referencing URL (a URL that refers back # to the server the response is coming from) it will use ServerName and # Port to form a "canonical" name. With this setting off, Apache will # use the hostname:port that the client supplied, when possible. This # also affects SERVER_NAME and SERVER_PORT in CGI scripts. UseCanonicalName On <IfModule mod_mime.c> TypesConfig /private/etc/httpd/mime.types </IfModule> DefaultType text/plain <IfModule mod_mime_magic.c> MIMEMagicFile /private/etc/httpd/magic </IfModule> HostnameLookups On ErrorLog "/private/var/log/httpd/error_log" # LogLevel: Control the number of messages logged to the error_log. # Possible values include: debug, info, notice, warn, error, crit, # alert, emerg. LogLevel info LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined LogFormat "%h %l %u %t \"%r\" %>s %b" common LogFormat "%{Referer}i -> %U" referer LogFormat "%{User-agent}i" agent CustomLog "/private/var/log/httpd/access_log" common # Set to one of: On | Off | EMail ServerSignature EMail <IfModule mod_alias.c> Alias /icons/ "/usr/share/httpd/icons/" <Directory "/usr/share/httpd/icons"> Options Indexes MultiViews AllowOverride None Order allow,deny Allow from all </Directory> Alias /manual/ "/Library/WebServer/Documents/manual/" <Directory "/Library/WebServer/Documents/manual"> Options Indexes FollowSymlinks MultiViews AllowOverride None Order allow,deny Allow from all </Directory> ScriptAlias /cgi-bin/ "/Library/WebServer/CGI-Executables/" <Directory "/Library/WebServer/CGI-Executables"> AllowOverride None Options None Order allow,deny Allow from all </Directory> </IfModule> <IfModule mod_autoindex.c> IndexOptions FancyIndexing AddIconByEncoding (CMP,/icons/compressed.gif) x-compress x-gzip AddIconByType (TXT,/icons/text.gif) text/* AddIconByType (IMG,/icons/image2.gif) image/* AddIconByType (SND,/icons/sound2.gif) audio/* AddIconByType (VID,/icons/movie.gif) video/* AddIcon /icons/binary.gif .bin .exe AddIcon /icons/binhex.gif .hqx AddIcon /icons/tar.gif .tar AddIcon /icons/world2.gif .wrl .wrl.gz .vrml .vrm .iv AddIcon /icons/compressed.gif .Z .z .tgz .gz .zip AddIcon /icons/a.gif .ps .ai .eps AddIcon /icons/layout.gif .html .shtml .htm .pdf AddIcon /icons/text.gif .txt AddIcon /icons/c.gif .c AddIcon /icons/p.gif .pl .py AddIcon /icons/f.gif .for AddIcon /icons/dvi.gif .dvi AddIcon /icons/uuencoded.gif .uu AddIcon /icons/script.gif .conf .sh .shar .csh .ksh .tcl AddIcon /icons/tex.gif .tex AddIcon /icons/bomb.gif core AddIcon /icons/back.gif .. AddIcon /icons/hand.right.gif README AddIcon /icons/folder.gif ^^DIRECTORY^^ AddIcon /icons/blank.gif ^^BLANKICON^^ DefaultIcon /icons/unknown.gif AddDescription "GZIP compressed document" .gz AddDescription "tar archive" .tar AddDescription "GZIP compressed tar archive" .tgz ReadmeName README HeaderName HEADER IndexIgnore .??* *~ *# HEADER* README* RCS CVS *,v *,t </IfModule> <IfModule mod_mime.c> AddLanguage da .dk AddLanguage nl .nl AddLanguage en .en AddLanguage et .ee AddLanguage fr .fr AddLanguage de .de AddLanguage el .el AddLanguage he .he AddCharset ISO-8859-8 .iso8859-8 AddLanguage it .it AddLanguage ja .ja AddCharset ISO-2022-JP .jis AddLanguage kr .kr AddCharset ISO-2022-KR .iso-kr AddLanguage nn .nn AddLanguage no .no AddLanguage pl .po AddCharset ISO-8859-2 .iso-pl AddLanguage pt .pt AddLanguage pt-br .pt-br AddLanguage ltz .lu AddLanguage ca .ca AddLanguage es .es AddLanguage sv .sv AddLanguage cs .cz .cs AddLanguage ru .ru AddLanguage zh-TW .zh-tw AddCharset Big5 .Big5 .big5 AddCharset WINDOWS-1251 .cp-1251 AddCharset CP866 .cp866 AddCharset ISO-8859-5 .iso-ru AddCharset KOI8-R .koi8-r AddCharset UCS-2 .ucs2 AddCharset UCS-4 .ucs4 AddCharset UTF-8 .utf8 <IfModule mod_negotiation.c> LanguagePriority en da nl et fr de el it ja kr no pl pt pt-br ru ltz ca es sv tw </IfModule> AddType application/x-tar .tgz AddEncoding x-compress .Z AddEncoding x-gzip .gz .tgz AddHandler cgi-script .cgi AddHandler cgi-script .rb </IfModule> <Location /server-status> SetHandler server-status Order deny,allow Deny from all Allow from 127.0.0.1 </Location> <IfModule mod_fastcgi.c> # URIs that begin with /fcgi-bin/, are found in /var/www/fcgi-bin/ Alias /fcgi-bin/ /Library/WebServer/Documents/fcgi-bin/ Alias /logs/ /var/log/httpd/ # Anything in here is handled as a "dynamic" server if not defined as "static" or "external" <Directory /Library/WebServer/Documents/fcgi-bin/> SetHandler fastcgi-script Options +ExecCGI </Directory> FastCgiIpcDir /Library/Webserver/Documents/.fcgi-namedpipes # Anything with one of these extensions is handled as a "dynamic" server if not defined as # "static" or "external". Note: "dynamic" servers require ExecCGI to be on in their directory. AddHandler fastcgi-script .fcgi .fpl </IfModule> # You may use the command line option ''-S'' to verify your virtual host # configuration. NameVirtualHost *:80 <VirtualHost *:80> DocumentRoot /Library/WebServer/Documents ServerName Opticon.local <Directory "/Library/WebServer/Documents" > Options ExecCGI FollowSymLinks AddHandler cgi-script .cgi AddHandler cgi-script .rb Order allow,deny Allow from all </Directory> </VirtualHost> <VirtualHost *:3000> DocumentRoot /Library/WebServer/Documents/contact ServerName Opticon.local <Directory "/Library/WebServer/Documents/contact" > Options ExecCGI FollowSymLinks AddHandler cgi-script .cgi AddHandler cgi-script .rb AllowOverride all Order allow,deny Allow from all </Directory> </VirtualHost> <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_METHOD} ^TRACE RewriteRule .* - [F] </IfModule> # End httpd.conf--------------------------------------------------------------- Here''s .htaccess: # General Apache options AddHandler fastcgi-script .fcgi AddHandler cgi-script .cgi Options +FollowSymLinks +ExecCGI # Make sure that mod_ruby.c has been added and loaded as a module with Apache RewriteEngine On # Change extension from .cgi to .fcgi to switch to FCGI and to .rb to switch to mod_ruby RewriteBase /dispatch.fcgi # Enable this rewrite rule to point to the controller/action that should serve root. RewriteRule ^$ /contact [R] # <caching> # no query string? RewriteCond %{QUERY_STRING} ^$ # no POST method? RewriteCond %{REQUEST_METHOD} !^POST$ [NC] # Request filename is a directory? RewriteCond %{REQUEST_FILENAME} -d # Request filename + ''/index'' is a file? RewriteCond %{REQUEST_FILENAME}/index -f # Rewrite to request filename + ''/index'' and finish RewriteRule ^(.*)/?$ $1/index [QSA,L] # no query string? RewriteCond %{QUERY_STRING} ^$ # no POST method? RewriteCond %{REQUEST_METHOD} !^POST$ [NC] # Request filename is a file? RewriteCond %{REQUEST_FILENAME} -f # Finish rewriting RewriteRule .* - [L] # Set default type of cached files to text/html DefaultType text/html # </caching> # Add missing slash RewriteRule ^([-_a-zA-Z0-9]+)$ /$1/ [R] # Default rewriting rules. RewriteRule ^([-_a-zA-Z0-9]+)/([-_a-zA-Z0-9]+)/([0-9]+)$ ?controller=$1&action=$2&id=$3 [QSA,L] RewriteRule ^([-_a-zA-Z0-9]+)/([-_a-zA-Z0-9]+)$ ?controller=$1&action=$2 [QSA,L] RewriteRule ^([-_a-zA-Z0-9]+)/$ ?controller=$1&action=index [QSA,L] RewriteRule ^([-_a-zA-Z0-9]+)/([-_a-zA-Z0-9]+)/([-_a-zA-Z0-9]+)/([0-9]+)$ ?module=$1&controller=$2&action=$3&id=$4 [QSA,L] RewriteRule ^([-_a-zA-Z0-9]+)/([-_a-zA-Z0-9]+)/([-_a-zA-Z0-9]+)$ ?module=$1&controller=$2&action=$3 [QSA,L] RewriteRule ^([-_a-zA-Z0-9]+)/([-_a-zA-Z0-9]+)/$ ?module=$1&controller=$2&action=index [QSA,L] # You can also point these error messages to a controller/action ErrorDocument 500 /500.html ErrorDocument 404 /404.html
Dick Davies
2005-Jan-26 12:03 UTC
Re: Multiple apps on shared server with vhost/dedicated port
* James G. Stallings II <twitch-kuaf+BvAnvdypLqBFPtG/w@public.gmane.org> [0128 17:28]:> > Following are the httpd.conf and .htaccess files; when accessing the > server at http://192.168.1.37:3000/ I get a forbidden from the server; > this is because there is no index capability turned on in the app > ''public'' directory. My suspicion is that my rewrite ruleset in .htaccess > is broken, but I am too weak there to know heads from tails.It''s an apache thing I think - try adding an ''Options Indexes'' for that directory, either in the vhost or the .htaccess.> Thanks for any assistance; sorry about the text file dumps.Not your fault - I wish apache would ship with a smaller httpd.conf....> <VirtualHost *:3000> > DocumentRoot /Library/WebServer/Documents/contact > ServerName Opticon.local > <Directory "/Library/WebServer/Documents/contact" > > Options ExecCGI FollowSymLinks > AddHandler cgi-script .cgi > AddHandler cgi-script .rb > AllowOverride all > Order allow,deny > Allow from all > </Directory> > </VirtualHost> > > <IfModule mod_rewrite.c> > RewriteEngine On > RewriteCond %{REQUEST_METHOD} ^TRACE > RewriteRule .* - [F] > </IfModule>> Here''s .htaccess: > > # General Apache options > AddHandler fastcgi-script .fcgi > AddHandler cgi-script .cgi > Options +FollowSymLinks +ExecCGI > > # Make sure that mod_ruby.c has been added and loaded as a module with > Apache > RewriteEngine On > > # Change extension from .cgi to .fcgi to switch to FCGI and to .rb to > switch to mod_ruby > RewriteBase /dispatch.fcgi > > # Enable this rewrite rule to point to the controller/action that should > serve root. > RewriteRule ^$ /contact [R]-- ''Aww, you know what always cheers me up? Laughing at other people''s misfortunes.'' -- Bender Rasputin :: Jack of All Trades - Master of Nuns