[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

getting human-readable results

Thufir Hawat

2/10/2008 11:22:00 AM

How do I get human readable results instead of:

Login: 0123

#<Call:0xb714cc94> #<Call:0xb714cc58> #<Call:0xb714cc30>
Edit | Back

Is there some ruby magic which I can put in the model? I'm hoping that
the solution isn't terribly complex. Is there something which I can do
from the console to figure out what's going wrong?


thufir@arrakis ~/goodfellow-tool $
thufir@arrakis ~/goodfellow-tool $
thufir@arrakis ~/goodfellow-tool $ cat app/models/login.rb
class Login < ActiveRecord::Base
belongs_to :employee
has_many :calls

def report
reports = []
calls.each do |call|
puts login + "\t" +
call.created_at.to_s + "\t"
call.comment
end
end

end
thufir@arrakis ~/goodfellow-tool $
thufir@arrakis ~/goodfellow-tool $ cat app/views/logins/show.rhtml
<% for column in Login.content_columns %>
<p>
<b><%= column.human_name %>:</b> <%=h @login.send(column.name) %>
</p>
<% end %>


<p>
<% @login.report.each do |report| %>
<%= h report %>
<% end %>
</p>


<%= link_to 'Edit', :action => 'edit', :id => @login %> |
<%= link_to 'Back', :action => 'list' %>
thufir@arrakis ~/goodfellow-tool $



thanks,

Thufir


12 Answers

Phlip

2/10/2008 5:21:00 PM

0

Thufir wrote:

> def report
> reports = []
> calls.each do |call|
> puts login + "\t" +
> call.created_at.to_s + "\t"
> call.comment
> end
> end

After fixing this, you need to just sit and read a Ruby tutorial. Don't
just read to answer your current question; you need soak-time with this
stuff to get the hang of it.

In Rails (which this is not the best forum for), 'puts' never goes into a
web page. It always goes into the console, which is somewhere inside your
web server.

Next, Ruby uses a "magic return" system, where the return value of a
function is the return of its last block. 'calls.each' returns 'calls',
for method-chaining (look that up). You need to make a 'map' of report
contents, then 'join' them together, like this:

def report
return calls.map{ |call|
CGI::escapeHTML(login) + "\t" +
call.created_at.to_s + "\t"
CGI::escapeHTML(call.comment)
}.join('<br/>')
end

Note that I added a clear 'return' (I always do), and that I used 2-space
tabs. Your editor picks 8 because it is stupid.

Use that like this:

> <% @login.report.each do |report| %>
> <%= report %>
> <% end %>

Notice I took your h out, because your system needed to escape your data
in between outputting your <br/>.

You also need to read up on basic HTML. _All_ the Rails documentation
will assume you know you can't use "\t" in a web page!

--
Phlip

Thufir Hawat

2/10/2008 8:03:00 PM

0

On Feb 10, 9:20 am, Phlip <phlip2...@gmail.com> wrote:
[...]
> After fixing this, you need to just sit and read a Ruby tutorial. Don't
> just read to answer your current question; you need soak-time with this
> stuff to get the hang of it.

Yes, I keep going to the library looking for books to answer this sort
of question -- no luck yet. Honestly, I've never seen ruby tutorial
address this sort of thing, perhaps I'm reading the wrong tutorials.

> In Rails (which this is not the best forum for), 'puts' never goes into a
> web page. It always goes into the console, which is somewhere inside your
> web server.

Right, it was just to illustrate the desired output.

> Next, Ruby uses a "magic return" system, where the return value of a
> function is the return of its last block. 'calls.each' returns 'calls',
> for method-chaining (look that up). You need to make a 'map' of report
> contents, then 'join' them together, like this:
>
> def report
> return calls.map{ |call|
> CGI::escapeHTML(login) + "\t" +
> call.created_at.to_s + "\t"
> CGI::escapeHTML(call.comment)
> }.join('<br/>')
> end
>
> Note that I added a clear 'return' (I always do), and that I used 2-space
> tabs. Your editor picks 8 because it is stupid.

Guess I'll have to "nano --tabsize=2 foo.rb" or switch to emacs;
another thing to learn (emacs).

Ah, yes I've heard about this "map" but couldn't figure out how to use
it. Now I'll be able to try some more focused queries in google about
it.

The "map" and "join" work together?

What I find most confusing about the above is the CGI part, I've never
seen that in any rails book I've looked at, but maybe I've not looked
hard enough!

> Use that like this:
>
> > <% @login.report.each do |report| %>
> > <%= report %>
> > <% end %>
>
> Notice I took your h out, because your system needed to escape your data
> in between outputting your <br/>.

I would've never known that in a million years. How do you know that
the system needs to escape the data between outputting <br/>? Anytime
data is output it must be escaped, then un-escaped, or something?

> You also need to read up on basic HTML. _All_ the Rails documentation
> will assume you know you can't use "\t" in a web page!

Of course :)

> --
> Phlip


Is there a more ruby-ish or rails way? Somehow, I can't think that
_why would do it like that, it seems kludgy.

Let me rephrase the question:

What's the optimal technique (idiom?) for getting output from the
model?



thanks,

Thufir

ps: Absolutely, I'll be going more in the ruby direction than rails,
thank you for the pointers :)

Phlip

2/10/2008 8:20:00 PM

0

> Yes, I keep going to the library looking for books to answer this sort
> of question -- no luck yet. Honestly, I've never seen ruby tutorial
> address this sort of thing, perhaps I'm reading the wrong tutorials.

Have you ever sat down with a Ruby language tutorial and just read it
straight thru - at least the first half?

I know it's rough, but it can't be worse than the average Stephen King
novel. (I wouldn't know!)

> Guess I'll have to "nano --tabsize=2 foo.rb" or switch to emacs;
> another thing to learn (emacs).

Get a real editor! Just set the tab width in the f---ing Options menu and be
done with it!!

> The "map" and "join" work together?

This is what the tutorial was for. (-:

> What I find most confusing about the above is the CGI part, I've never
> seen that in any rails book I've looked at, but maybe I've not looked
> hard enough!

That's because a Rails book will advertise that HTML code only belongs in
the View. This is not exactly true, but it's a good metric. Your Models and
Controllers should assemble data, and your View should iterate these data to
emit strings. So all h() calls "belong in" the View.

> I would've never known that in a million years. How do you know that
> the system needs to escape the data between outputting <br/>? Anytime
> data is output it must be escaped, then un-escaped, or something?

All Rails tutorial cover that - it prevents malicious HTML insertion
attacks.

--
Phlip


Phlip

2/10/2008 8:38:00 PM

0

>> Guess I'll have to "nano --tabsize=2 foo.rb" or switch to emacs;
>> another thing to learn (emacs).

Please don't tell us you are working in Linux with only some kind of console
mode available...


Andrei Maxim

2/10/2008 8:39:00 PM

0

On 2/10/08, Thufir <hawat.thufir@gmail.com> wrote:
> On Feb 10, 9:20 am, Phlip <phlip2...@gmail.com> wrote:
> Is there a more ruby-ish or rails way? Somehow, I can't think that
> _why would do it like that, it seems kludgy.
>
> Let me rephrase the question:
>
> What's the optimal technique (idiom?) for getting output from the
> model?

If you want to store the output to a log file or something similar,
use the logger object (for example, logger.info).

Otherwise, there isn't a simple way of getting HTML from the model and
that's because the model never speaks directly with the view. It's the
controller's job to do that. If you look closely, you'll see that you
completely skipped the controller.

As a hint, try using scaffolds and modify one to suit your needs. It
might give you a good head-start, but remember that sometimes you
might have to seriously tweak them.

--
Andrei Maxim
http://andr...

7stud --

2/10/2008 9:54:00 PM

0

Thufir wrote:
> Is there some ruby magic which I can put in the model?

Try posting your questions on a forum entitled: C++ or C#.
--
Posted via http://www.ruby-....

Phlip

2/10/2008 10:10:00 PM

0

>> Is there some ruby magic which I can put in the model?
>
> Try posting your questions on a forum entitled: C++ or C#.

If that's an attempt at humor...


Howard Roberts

2/10/2008 10:41:00 PM

0

Thufir wrote:

> Is there some ruby magic which I can put in the model? I'm hoping that
> the solution isn't terribly complex. Is there something which I can do
> from the console to figure out what's going wrong?

You'd probably get much better results posting this to the Ruby on Rails
mailing list. But in case you aren't familiar with it .script/console
may be exactly what you are looking for to try out your models and such
from a command line, IRB style.

HTH,

Howard
--
Posted via http://www.ruby-....

Thufir Hawat

2/10/2008 11:40:00 PM

0

On Feb 10, 2:40=A0pm, Howard Roberts <howardrobe...@comcast.net> wrote:
[...]
> You'd probably get much better results posting this to the Ruby on Rails
> mailing list.
[...]

Parts of this thread are showing up in ruby-talk-google which is a two-
way portal to the Ruby on Rails mailing list...I think.

Yeh, I must've posted to comp.lang.ruby initially, or at least that's
where the first reply was. Typically I use gmane. aaarghh.
Certainly I intended to post to the rails list via gmane, but I guess
I didn't.


-Thufir

Thufir Hawat

2/10/2008 11:47:00 PM

0



On Feb 10, 12:39=A0pm, "Andrei Maxim" <and...@andreimaxim.ro> wrote:
> On 2/10/08, Thufir <hawat.thu...@gmail.com> wrote:
>
> > On Feb 10, 9:20 am, Phlip <phlip2...@gmail.com> wrote:
> > Is there a more ruby-ish or rails way? =A0Somehow, I can't think that
> > _why would do it like that, it seems kludgy.
>
> > Let me rephrase the question:
>
> > What's the optimal technique (idiom?) for getting output from the
> > model?
>
> If you want to store the output to a log file or something similar,
> use the logger object (for example, logger.info).

Oh, I should've written HTML output for a user.

> Otherwise, there isn't a simple way of getting HTML from the model and
> that's because the model never speaks directly with the view. It's the
> controller's job to do that. If you look closely, you'll see that you
> completely skipped the controller.

I know :(

> As a hint, try using scaffolds and modify one to suit your needs. It
> might give you a good head-start, but remember that sometimes you
> might have to seriously tweak them.

I have a scaffold for this model, I'm using rails 1.5 IIRC.

Ok, I started to do that before going this direction. I had the
desired output through the console, but didn't know how to translate
that to MVC, which is the bigger question.

Basically, the model has a report method, then, in the controller
there's a "run_report" method? Then the view "calls" run_report?


Thanks,

Thufir

http://code.google.com/p/goodfellow-tool/sou...