Trying to do a simple thing


#1

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.


#2

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


#3

Le Mercredi 04 Janvier 2006 00:09, Peter D. 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 :slight_smile:


#4

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) %>

<%= text_field_tag(‘second_number’, @second) if @first %>

<% if (@first and @second) then -%>
Total of the two numbers: <%= @first.to_i + @second.to_i %>

<%= link_to “Click to play again”, { :action => “replay” } %>
<% else -%>
Enter two numbers to see the total.
<% end -%>
<%= submit_tag “Guess” %>
<%= end_form_tag %>


#5

On Jan 3, 2006, at 3:03 PM, Nicolas C. wrote:

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 :wink:

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 Z.
Yakima Herald-Republic
WebMaster
http://yakimaherald.com
509-577-7732
removed_email_address@domain.invalid


#6

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 S.
Technology Visionary - Technology Assessment - Documentation
“Looking at technology from every angle”
http://www.eclecticity.com


#7

Le Mercredi 04 Janvier 2006 02:15, Wilson B. a écrit :

@second = session[:second_number]
end
<% 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 :-).


#8

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 S. tried to type something
like:

missing.


Rails mailing list
removed_email_address@domain.invalid
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!


#9

Le Mercredi 04 Janvier 2006 01:18, Ezra Z. 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 :wink:

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 :slight_smile:

Bye.


#10

Nicolas C. wrote:

def index
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 :slight_smile:


#11

Le Mercredi 04 Janvier 2006 12:39, Nicolas C. 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


#12

On Tue, Jan 03, 2006 at 07:23:22PM -0800, Dan S. 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 S.
–Greg


#13

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


#14

Gregory S. 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 removed_email_address@domain.invalid
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

<Directory /var/local/projects/mysite/public>
Options +FollowSymLinks
Order allow,deny
allow from all

LogLevel warn

CustomLog /var/local/projects/mysite/log/apache.log combined

#15

I have not used this method before but maybe do this:

redirect_back_or_default :controller => “foo”, :action => “welcome”


#16

Worked without a hitch.
Thanks!

– Frank H. removed_email_address@domain.invalid wrote:

written in my controller, though instead of


Rails mailing list
removed_email_address@domain.invalid

http://lists.rubyonrails.org/mailman/listinfo/rails


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


Yahoo! DSL ? Something to write home about.
Just $16.99/mo. or less.
dsl.yahoo.com


#17

On 1/4/06, Alex Y. removed_email_address@domain.invalid wrote:

class NumberController < ApplicationController

attr_accessor :target

def thanks
@result = calculate_result
end
end

The rest is left as an exercise for the reader :slight_smile:

Using attr_accessor on a controller had never occurred to me. That’s
really cool.
Thanks for sharing it.


#18

I don’t see redirect_back_or_default in the methods box of http://
api.rubyonrails.com, where is it documented?

– fxn


#19

On 1/4/06, Xavier N. removed_email_address@domain.invalid 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