Hi ! I was talking to a seasider and he asked me if it was easy to do the following thing using rails : 1) ask a number to the user 2) ask a second number 3) give the addition of the two number and a link to be able to replay All these things have to be done in one controller and one action, there is no need for verification and other stuff. I tried but I''ve got some problems (looping). Can anyone give me an elegant solution to this problem ? (the controller file and the view) Thank you for your help. -- Nicolas Cavigneaux | GPG keyID : CFE76D24 nico@bounga.org | http://www.bounga.org -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: not available Url : http://wrath.rubyonrails.org/pipermail/rails/attachments/20060103/96872069/attachment.bin
This sounds like a perfect task for session variables. Now if only I wasn''t such a newbie, I''d tell you how to do it... Peter
Le Mercredi 04 Janvier 2006 00:09, Peter Dekkers a ?crit?:> This sounds like a perfect task for session variables.Yeah, you have to use session variables ...> Now if only I wasn''t such a newbie, I''d tell you how to do it...I''m a newbie too ''cause I can''t find an elegant solution ... Bye :-) -- Nicolas Cavigneaux | GPG keyID : CFE76D24 nico@bounga.org | http://www.bounga.org -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: not available Url : http://wrath.rubyonrails.org/pipermail/rails/attachments/20060103/26f7cf60/attachment.bin
On Jan 3, 2006, at 3:03 PM, Nicolas Cavigneaux wrote:> Hi ! > > I was talking to a seasider and he asked me if it was easy to do > the following > thing using rails : > > 1) ask a number to the user > 2) ask a second number > 3) give the addition of the two number and a link to be able to replay > > All these things have to be done in one controller and one action, > there is no > need for verification and other stuff. > > I tried but I''ve got some problems (looping). > > Can anyone give me an elegant solution to this problem ? (the > controller file > and the view) > > Thank you for your help.Your friend is trying to trip you up because that kind of thing is really easy in a continuation based framework like seaside where you can effectively maintain state between stateless http requests. Ask him if he can do the same thing in seaside without a big ugly query string hash in the url ;-) But seriously, here''s a way to do it with rails. I will let you figure out the view part and how to make the form but here is a controller action and session interaction that could work: class FooController < AR... def summer if session[:first_num] @result = params[:num] + session[:firstnum] session[:first_num] = nil return elsif params[:num] @session[:firstnum] = params[:num] end end end So on the first request to this action you should have a num variable in the params hash. The first time you call it it just renders the form. The second time you call it with the first number to be added, session[:first_num] gets set to params[:num] and you get put back to the form to enter the second num. The third time you call it, session[:firstnum] is already set so @result gets set to params [:num] + session[:first_num] and session[:firstnum] gets set to nil so the whole thing will start over again the next time you call it. Now in the view you have a conditional statement that checks for session[:first_num] and if that is there then it displays text that says enter second number. Also you have a if statement that checks for @result and if that is there it displays the answer and a link to /foo/summer to do the whole thing over again. Does that work for you? Cheers- -Ezra Zygmuntowicz Yakima Herald-Republic WebMaster http://yakimaherald.com 509-577-7732 ezra@yakima-herald.com
Here''s one way to do it.. totally untested, written in email client, use at own risk, etc, etc. The idea, though, is that when displaying the view, use the values for the two numbers that are in the session. When receiving the "Submit" action from the form, store any numbers we received as parameters. If we have both numbers, display the total. --controller-- def guess @first = session[:first_number] @second = session[:second_number] if request.post? session[:first_number] = params[:first_number] || session[:first_number] session[:second_number] = params[:second_number] || session[:second_number] end end def replay session[:first_number] = nil session[:second_number] = nil redirect_to(:action => ''guess'') end --view-- guess.rhtml <%= start_form_tag :action => ''guess'' %> <%= text_field_tag(''first_number'', @first, :disabled => !@first) %> <br /> <%= text_field_tag(''second_number'', @second) if @first %> <br /> <% if (@first and @second) then -%> Total of the two numbers: <%= @first.to_i + @second.to_i %> <br /> <%= link_to "Click to play again", { :action => "replay" } %> <% else -%> Enter two numbers to see the total. <% end -%> <%= submit_tag "Guess" %> <%= end_form_tag %> On 1/3/06, Nicolas Cavigneaux <nico@bounga.org> wrote:> Hi ! > > I was talking to a seasider and he asked me if it was easy to do the following > thing using rails : > > 1) ask a number to the user > 2) ask a second number > 3) give the addition of the two number and a link to be able to replay > > All these things have to be done in one controller and one action, there is no > need for verification and other stuff. > > I tried but I''ve got some problems (looping). > > Can anyone give me an elegant solution to this problem ? (the controller file > and the view) > > Thank you for your help. > -- > Nicolas Cavigneaux | GPG keyID : CFE76D24 > nico@bounga.org | http://www.bounga.org > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > >
I have a colleague who got very enthusiastic about Rails a few days ago. Today, he called me and said he was no longer thinking Rails was quite as cool as he''d first thought because it was too hard to get working on Apache on a port other than 3000 and that this kept Rails applications from being quite as easy for non-technical users to use and maintain. I don''t know squat about Apache but what he said sounded to me like horse puckey, so I figured I''d ask the question here, get a definitive answer, and then go talk to him about what he must be missing. So, what''s he missing? -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. -.-.-.-.-.-.-.-.-.-.-.-.-.-.- Dan Shafer Technology Visionary - Technology Assessment - Documentation "Looking at technology from every angle" http://www.eclecticity.com
Dan, You can run it straigth under apache, there is a catch however, performance might drop severely with heavy usage, due to it''s CGI like nature. That''s why all these light weight http daemons are available. Then you can have apache function as a proxy and let it''s little brother handle the rails app. Here''s a link: http://wiki.rubyonrails.com/rails/pages/HowToSetupDebianApache2Rails And this is a config snippet of apache2 <Directory /home/.../railsapp/public> Options +FollowSymLinks +ExecCGI AddHandler cgi-script .cgi AddHandler fcgid-script .fcgi #AddHandler fastcgi-script .fcgi Order allow,deny Allow from all </Directory> I must say I''m fond of apache, but the proxy thingy is on my todo list. Regards, Gerard. On Wednesday 04 January 2006 04:23, Dan Shafer tried to type something like:> I have a colleague who got very enthusiastic about Rails a few days > ago. Today, he called me and said he was no longer thinking Rails was > quite as cool as he''d first thought because it was too hard to get > working on Apache on a port other than 3000 and that this kept Rails > applications from being quite as easy for non-technical users to use > and maintain. > > I don''t know squat about Apache but what he said sounded to me like > horse puckey, so I figured I''d ask the question here, get a > definitive answer, and then go talk to him about what he must be > missing. > > So, what''s he missing? > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. > -.-.-.-.-.-.-.-.-.-.-.-.-.-.- > Dan Shafer > Technology Visionary - Technology Assessment - Documentation > "Looking at technology from every angle" > http://www.eclecticity.com > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails-- "Who cares if it doesn''t do anything? It was made with our new Triple-Iso-Bifurcated-Krypton-Gate-MOS process ..." My $Grtz =~ Gerard; ~ :wq!
Le Mercredi 04 Janvier 2006 02:15, Wilson Bilkovich a ?crit?:> Here''s one way to do it.. totally untested, written in email client, > use at own risk, etc, etc. > The idea, though, is that when displaying the view, use the values for > the two numbers that are in the session. When receiving the "Submit" > action from the form, store any numbers we received as parameters. > If we have both numbers, display the total. > > --controller-- > def guess > @first = session[:first_number] > @second = session[:second_number] > if request.post? > session[:first_number] = params[:first_number] || > session[:first_number] session[:second_number] = params[:second_number] || > session[:second_number] end > end > > def replay > session[:first_number] = nil > session[:second_number] = nil > redirect_to(:action => ''guess'') > end > > --view-- > guess.rhtml > > <%= start_form_tag :action => ''guess'' %> > <%= text_field_tag(''first_number'', @first, :disabled => !@first) %> <br /> > <%= text_field_tag(''second_number'', @second) if @first %> <br /> > <% if (@first and @second) then -%> > Total of the two numbers: <%= @first.to_i + @second.to_i %> <br /> > <%= link_to "Click to play again", { :action => "replay" } %> > <% else -%> > Enter two numbers to see the total. > <% end -%> > <%= submit_tag "Guess" %> > <%= end_form_tag %>Each number is asked two times before calculating the total ... I''m trying to see what happens Thank you for your help :-). -- Nicolas Cavigneaux | GPG keyID : CFE76D24 nico@bounga.org | http://www.bounga.org -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: not available Url : http://wrath.rubyonrails.org/pipermail/rails/attachments/20060104/e3aada7a/attachment.bin
Le Mercredi 04 Janvier 2006 12:39, Nicolas Cavigneaux a ?crit?:> Each number is asked two times before calculating the total ... > > I''m trying to see what happensThis way, it works well : class NumberController < ApplicationController def index if request.post? session[:first_number] = params[:first_number] || session[:first_number] session[:second_number] = params[:second_number] || session[:second_number] end @first = session[:first_number] @second = session[:second_number] end def replay session[:first_number] = nil session[:second_number] = nil redirect_to(:action => ''index'') end end -- Nicolas Cavigneaux | GPG keyID : CFE76D24 nico@bounga.org | http://www.bounga.org -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: not available Url : http://wrath.rubyonrails.org/pipermail/rails/attachments/20060104/2827bcc1/attachment.bin
Le Mercredi 04 Janvier 2006 01:18, Ezra Zygmuntowicz a ?crit?:> Your friend is trying to trip you up because that kind of thing is > really easy in a continuation based framework like seaside where you > can effectively maintain state between stateless http requests. Ask > him if he can do the same thing in seaside without a big ugly query > string hash in the url ;-)Oh I see :-). So I''m gonna try to trip him up with a thing that is not "natural" to do using seaside compared to RoR. Any idea ?> Does that work for you?Yes, I have a working solution, thank you :-) Bye. -- Nicolas Cavigneaux | GPG keyID : CFE76D24 nico@bounga.org | http://www.bounga.org -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: not available Url : http://wrath.rubyonrails.org/pipermail/rails/attachments/20060104/4ebe429d/attachment.bin
Nicolas Cavigneaux wrote:> Le Mercredi 04 Janvier 2006 12:39, Nicolas Cavigneaux a ?crit : > >>Each number is asked two times before calculating the total ... >> >>I''m trying to see what happens > > > This way, it works well : > > class NumberController < ApplicationController > def index > if request.post? > session[:first_number] = params[:first_number] || > session[:first_number] > session[:second_number] = params[:second_number] || > session[:second_number] > end > @first = session[:first_number] > @second = session[:second_number] > end > > def replay > session[:first_number] = nil > session[:second_number] = nil > redirect_to(:action => ''index'') > end > endBetter: class NumberController < ApplicationController attr_accessor :target def index reset ask "First number?", :firstnumber target = :q2 end def q2 ask "Second number?", :secondnumber target = :thanks end def thanks @result = calculate_result end end The rest is left as an exercise for the reader :-) -- Alex
On Tue, Jan 03, 2006 at 07:23:22PM -0800, Dan Shafer wrote: } I have a colleague who got very enthusiastic about Rails a few days } ago. Today, he called me and said he was no longer thinking Rails was } quite as cool as he''d first thought because it was too hard to get } working on Apache on a port other than 3000 and that this kept Rails } applications from being quite as easy for non-technical users to use } and maintain. It is, indeed, harder than it should be to get working under Apache, particularly Apache2. Despite my only vague understanding of Apache config files and modules and such, however, I was able to go from using WEBrick for development to Apache2 for deployment in roughly a day with the help of an excellent package management system (yay, Debian!) and some very good, if imperfect, instructions found on the web with Google. } I don''t know squat about Apache but what he said sounded to me like } horse puckey, so I figured I''d ask the question here, get a } definitive answer, and then go talk to him about what he must be } missing. Nope, not horse puckey. It also very much depends on one''s deployment platform. I am confident that I would have had much more trouble with a platform other than Debian, particularly Windows, and not just because of less familiarity with administering them. } So, what''s he missing? More to the point, what is Rails missing? I looked in the tutorials and howto sections on the rubyonrails.org site and was unable to find anything about deploying on Apache. Google found a few pages that discussed whether to use mod_ruby, scgi, fastcgi, or fcgid, but none of them were particularly thorough on pros and cons of each approach. Since I have never attempted a real deployment (what I''ve done so far is just a toy), and I''ve only done it on Debian and Apache2 and fcgid, I am unqualified to produce the documentation that is needed. I would if I could. If there is someone (or a group of people) out there with sufficient expertise/breadth of knowledge, please explain the following: 1) What methods exist for RoR deployment (WEBrick, mod_ruby/scgi/fastcgi/fcgid w/ Apache/Apache2/lighttpd) 2) What makes one deployment choice better or worse than another (only fcgid seems to work well with Apache2? scgi is recommended on Windows?) 3) What steps must be taken to deploy with each of these methods ( software installation, messing with apache.conf and .htaccess, etc.) Part 1 is easy since it''s just a laundry list and I may have actually covered it right there. Part 2 is the complicated part, and may be somewhat controversial. Part 3 has a platform-specific component (software installation), but each individual deployment method howto can be written by a separate person. I think I could write the Apache2 and fcgid part, with a Debian software installation process. } Dan Shafer --Greg
I have redirect_back_or_default :action => "welcome" written in my controller, though instead of linking to a "welcome" action, I would like to link outside to another action in another directory.... Anyone have any ideas? __________________________________________ Yahoo! DSL ? Something to write home about. Just $16.99/mo. or less. dsl.yahoo.com
Gregory Seidman a ?crit :> It is, indeed, harder than it should be to get working under Apache, > particularly Apache2. Despite my only vague understanding of Apache config > files and modules and such, however, I was able to go from using WEBrick > for development to Apache2 for deployment in roughly a day with the help of > an excellent package management system (yay, Debian!) and some very good, > if imperfect, instructions found on the web with Google.Try scgi. Install libapache2-mod-scgi, then gem install scgi, configure your vhost with something like this, and go ! (scgi_start) Even in dev mode, it''s the fastest way I''ve found to run Rails. <VirtualHost *> DocumentRoot /var/local/projects/mysite/public ServerName mysite.com AddDefaultCharset utf-8 ServerAdmin webmaster@mysite.com ErrorDocument 500 /500.html ErrorDocument 404 /404.html # handle all requests throug SCGI # Tune the same number in config/scgi.yaml ! SCGIMount / 127.0.0.1:10000 # matches locations with a dot following at least one more characters, that is, things like *,html, *.css, *.js, which should be delivered directly from the filesystem <LocationMatch "\.(jpg|gif|png|ico|js|css|pdf|zip|tgz|bz2|html|mp3|gif)$"> # don''t handle those with SCGI SCGIHandler Off </LocationMatch> <Directory /var/local/projects/mysite/public> Options +FollowSymLinks Order allow,deny allow from all </Directory> LogLevel warn CustomLog /var/local/projects/mysite/log/apache.log combined </VirtualHost> -- Jean-Christophe Michel
I have not used this method before but maybe do this: redirect_back_or_default :controller => "foo", :action => "welcome" On 1/4/06, jonathan Mcintire <hatejonny@yahoo.com> wrote:> I have > redirect_back_or_default :action => "welcome" > written in my controller, though instead of linking to > a "welcome" action, I would like to link outside to > another action in another directory.... > Anyone have any ideas? > > > > > > > > > > > > __________________________________________ > Yahoo! DSL ? Something to write home about. > Just $16.99/mo. or less. > dsl.yahoo.com > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Worked without a hitch. Thanks! -- Frank Hale <frankhale1@gmail.com> wrote:> I have not used this method before but maybe do > this: > > redirect_back_or_default :controller => "foo", > :action => "welcome" > > On 1/4/06, jonathan Mcintire <hatejonny@yahoo.com> > wrote: > > I have > > redirect_back_or_default :action => "welcome" > > written in my controller, though instead of > linking to > > a "welcome" action, I would like to link outside > to > > another action in another directory.... > > Anyone have any ideas? > > > > > > > > > > > > > > > > > > > > > > > > __________________________________________ > > Yahoo! DSL ? Something to write home about. > > Just $16.99/mo. or less. > > dsl.yahoo.com > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >__________________________________________ Yahoo! DSL ? Something to write home about. Just $16.99/mo. or less. dsl.yahoo.com
I don''t see redirect_back_or_default in the methods box of http:// api.rubyonrails.com, where is it documented? -- fxn
On 1/4/06, Alex Young <alex@blackkettle.org> wrote:> Nicolas Cavigneaux wrote: > > Le Mercredi 04 Janvier 2006 12:39, Nicolas Cavigneaux a ?crit : > > > >>Each number is asked two times before calculating the total ... > >> > >>I''m trying to see what happens > > > > > > This way, it works well : > > > > class NumberController < ApplicationController > > def index > > if request.post? > > session[:first_number] = params[:first_number] || > > session[:first_number] > > session[:second_number] = params[:second_number] || > > session[:second_number] > > end > > @first = session[:first_number] > > @second = session[:second_number] > > end > > > > def replay > > session[:first_number] = nil > > session[:second_number] = nil > > redirect_to(:action => ''index'') > > end > > end > > Better: > > class NumberController < ApplicationController > attr_accessor :target > def index > reset > ask "First number?", :firstnumber > target = :q2 > end > > def q2 > ask "Second number?", :secondnumber > target = :thanks > end > > def thanks > @result = calculate_result > end > end > > The rest is left as an exercise for the reader :-) >Using attr_accessor on a controller had never occurred to me. That''s really cool. Thanks for sharing it.
On 1/4/06, Xavier Noria <fxn@hashref.com> wrote:> I don''t see redirect_back_or_default in the methods box of http:// > api.rubyonrails.com, where is it documented? >It''s part of the Salted Hash Login Generator, and/or the Login Engine based on it. It''s pretty simple, though.. here''s the code: # store current uri in the session. # we can return to this location by calling return_location def store_location session[:return_to] = request.request_uri end # move to the last store_location call or to the passed default one def redirect_back_or_default(default) if session[:return_to].nil? redirect_to default else redirect_to_url session[:return_to] session[:return_to] = nil end end