getting human-readable results

T

Thufir

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
 
T

Thufir

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:


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


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 :)
 
A

Andrei Maxim

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.
 
H

Howard Roberts

Thufir said:
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
 
T

Thufir

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
 
T

Thufir

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/source/browse
 
T

Thufir

[...]> 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.

Woops, I posted to google-talk-ruby (comp.lang.ruby), instead of
google-talk-rails. Regardless, I got some good pointers :)


thanks,

Thufir
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top