Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
BrokenDynasty
Dec 25, 2003

Operation Atlas posted:

Sorry for the snark, but it's the right answer:

Write javascript.

No problem, and no apology necessary. I'm relatively new to rails and am still constantly double-checking to make sure I'm doing things the 'right' way. So wasn't sure if there was a better way using RJS, my google searches didn't turn up anything obvious. Thanks again.

Adbot
ADBOT LOVES YOU

NotShadowStar
Sep 20, 2000
My advice is avoid RJS. I think they went a little to far saying 'ruby for EVERYTHING'. RJS is really confusing and really really badly documented (probably goes along with Prototype is badly documented), wheras putting JS in the view template (or better, a partial that contains the script) is much easier.

Rails 3 is much nicer since it doesn't dump Javascript all over the page but instead creates specific named HTML ids and classes that you can use any JS framework you want to target those classes.

Hammertime
May 21, 2003
stop

NotShadowStar posted:

My advice is avoid RJS. I think they went a little to far saying 'ruby for EVERYTHING'. RJS is really confusing and really really badly documented (probably goes along with Prototype is badly documented), wheras putting JS in the view template (or better, a partial that contains the script) is much easier.

Rails 3 is much nicer since it doesn't dump Javascript all over the page but instead creates specific named HTML ids and classes that you can use any JS framework you want to target those classes.

Echoing this. I never really saw the point to writing Javascript hooks in ruby, when it takes around the same amount of code as it does in Javascript. It's just adding an awkward layer of abstraction. Plus out of the box it doesn't support jQuery, which I'd heartily recommend over Prototype.

Operation Atlas
Dec 17, 2003

Bliss Can Be Bought

NotShadowStar posted:

My advice is avoid RJS. I think they went a little to far saying 'ruby for EVERYTHING'. RJS is really confusing and really really badly documented (probably goes along with Prototype is badly documented), wheras putting JS in the view template (or better, a partial that contains the script) is much easier.

Rails 3 is much nicer since it doesn't dump Javascript all over the page but instead creates specific named HTML ids and classes that you can use any JS framework you want to target those classes.

There is one exception: when I have a JS partial that I need to throw out on an AJAX request, I do:

code:
respond.to :js do
  render :update do |page|
    page << render_to_string(:partial => 'layouts/pagination.js')
  end
end
But here it's just spitting out the JS. It's just a nice way to keep the controllers simple and MIME proper.

smug forum asshole
Jan 15, 2005
Anyone want to recommend some resources for learning and utilizing Javascript libraries like Prototype and JQuery?

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.
The jQuery and JavaScript megathreads in this forum?

smug forum asshole
Jan 15, 2005

pokeyman posted:

The jQuery and JavaScript megathreads in this forum?

:cool: didn't even see those

:cool:

NotShadowStar
Sep 20, 2000

Operation Atlas posted:

There is one exception: when I have a JS partial that I need to throw out on an AJAX request, I do:

code:
respond.to :js do
  render :update do |page|
    page << render_to_string(:partial => 'layouts/pagination.js')
  end
end
But here it's just spitting out the JS. It's just a nice way to keep the controllers simple and MIME proper.

Sorry that's still too :psyduck: for me.

code:
respond_to do |r|
  r.html
  r.js { render :text => 'script' }
  r.json { render :text => @model.to_json }
end
And if it's not set up already (don't know if it's default)
code:
Mime::Type.register "application/x-javascript", :js

McLarenF1
Jan 9, 2004

Looking to Buy a McLaren, Anyone Selling One .... Cheap?
Figured I'd post here before I bothered with it's own thread. I'm a recruiter and I'm looking for some Ruby on Rail developers. Normally I just stick to the normal methods, my company database, job boards and networking sites, but this position has proven more difficult so I though I'd check if anyone here is interested. Anyway, cutting to the chase:

code:
Position: Applications Developer
Location: White Plains, NY
Type: Contract to hire
Relocation Package: Yes
Industry: Pharmaceutical
Openings: 3
Salary: Based on previous salary history, but very competitive
Other: Full benefits package

Primary skills: 
- Ruby on Rails
- PERL
- Python
This is an extremely fast going growing company with 300+ open positions (mostly research scientists) and HR is so bogged down, so an IT Director my company has a good relationship has reached out to us for help filling these positions. Needless to say there are a lot of other details I can provide. Please message me with your contact info if you'd like to talk.

Last note, I'll be straight forward that I'd like to stick with candidates in the north east US if possible unless you're extremely well qualified. Thanks.

plasticbugs
Dec 13, 2006

Special Batman and Robin
I've got another very rudimentary Ruby question. I'm attempting to access data inside an XML file with Nokogiri. The data is coming back as a Hash with a nested Array. Then, inside that Array is a Hash with another Array nested inside it. Inside THAT Array is a Hash with the data I want to access.

Given:
code:
{a => [{b =>[{c => "value"}]}]}
What's the best programmatic way to access "value" in one line of code? The way I've been doing it can't possibly be right.

Flobbster
Feb 17, 2005

"Cadet Kirk, after the way you cheated on the Kobayashi Maru test I oughta punch you in tha face!"

plasticbugs posted:

Given:
code:
{a => [{b =>[{c => "value"}]}]}
What's the best programmatic way to access "value" in one line of code? The way I've been doing it can't possibly be right.

Does this work?
code:
thing['a'].first['b'].first['c'] # assuming a, b, c are strings and not symbols, in which case:
thing[:a].first[:b].first[:c]
I'm assuming you will know a priori that those are single-element arrays that you're dealing with. I used first instead of [0] so the whole thing didn't become a huge square bracket mess, even though it's a little longer.

I don't think any of that needs to be parenthesized to work, but I'm not in front of my interpreter right now.

plasticbugs
Dec 13, 2006

Special Batman and Robin
I should clarify, I wasn't using Nokogiri. I was using a gem for a specific API, which I'm guessing has its quirks.

Flobbster posted:

Does this work?
code:
thing['a'].first['b'].first['c'] # assuming a, b, c are strings and not symbols, in which case:
thing[:a].first[:b].first[:c]
I'm assuming you will know a priori that those are single-element arrays that you're dealing with. I used first instead of [0] so the whole thing didn't become a huge square bracket mess, even though it's a little longer.

I don't think any of that needs to be parenthesized to work, but I'm not in front of my interpreter right now.

Yes, sorry. a, b, and c are strings. EDIT: That's the answer I was looking for. Thanks!

EDIT: Deleted stupid question. Learning all this on my own with nothing but some books and Google is challenging, but this thread has been such a huge help. Thanks.

plasticbugs fucked around with this message at 09:15 on Mar 12, 2010

plasticbugs
Dec 13, 2006

Special Batman and Robin
I'm confused again. Another dumb question:

I'm working with a Nokogiri object and am attempting to populate a page with TV episode titles and links to those titles with data from an XML file.

This gets all the links:
code:
 
doc.css('episode link').each do |link|
     link.text
end
This gets all the episode titles
code:
doc.css('episode title').each do |title| %>
     title.text
end
It seems the best solution was to create an array and populate it with hashes like so:
code:
[{:link=>"/Buffy_The_Vampire_Slayer/episodes/329033", :title=>"Unaired Pilot"},
{:link=>"/Buffy_The_Vampire_Slayer/episodes/28077", :title=>"Welcome to the Hellmouth (1)"}, 
{:link=>"/Buffy_The_Vampire_Slayer/episodes/28078", :title=>"The Harvest (2)"}, 
{:link=>"/Buffy_The_Vampire_Slayer/episodes/28079", :title=>"The Witch"},
etc...

This was my approach to putting the results of both operations into the array above, and it worked, but it seems VERY sloppy:
code:
@myarray = []
@doc.css('episode link').each do |link|
@myarray << Hash[:link, link.text]
end

i = 0
@doc.css('episode title').each do |title|
@myarray[i][:title] = title.text
i = i + 1
end
1. I'm not sure where to put that code, now. It's too much logic to stick in my view, but when I copy/paste it into my Controller's "show" action, it takes 12 seconds to render the view. Which is NOT right at all.

2. Is there a better way to put those results into a structure that I can iterate over in my view to create a list of Episode titles and links? I feel like I'm missing the obvious, easy solution to displaying this data in my view.

plasticbugs fucked around with this message at 09:28 on Mar 12, 2010

jetviper21
Jan 29, 2005

plasticbugs posted:


2. Is there a better way to put those results into a structure that I can iterate over in my view to create a list of Episode titles and links? I feel like I'm missing the obvious, easy solution to displaying this data in my view.

Try using something like Rack::Cache if your having lag problems and the data doesn't change much http://rtomayko.github.com/rack-cache/

Valrik
Apr 16, 2003

Bald!
After failing spectacularly to grasp the innards of C#, I've run to the glittery arms of Ruby. I'm amazed at how sensible it is, and am looking forward to learning more. Thanks to this thread for reminding me about Ruby! I've learned more Ruby in 3 days than I learned after 3 weeks of C#.

HasidicNeckbeard
Jul 14, 2006
I'm livin' a life that's far from affordable, standin' on the corner callin' cuties on the portable
Does anyone know of a download mirror for the Ruby on Rails windows installer? The official site (http://rubyforge.org/frs/?group_id=167) says its downloading painfully slow, but its not downloading at all. I also can't seem to find any other place that offers the file.

jetviper21
Jan 29, 2005

HasidicNeckbeard posted:

Does anyone know of a download mirror for the Ruby on Rails windows installer? The official site (http://rubyforge.org/frs/?group_id=167) says its downloading painfully slow, but its not downloading at all. I also can't seem to find any other place that offers the file.

I don't mean to derail your question but if your machine can support vm's i would suggest developing in a Ubuntu vm with virtual box if you are on windows some gems just don't play nicely in windows.

nbv4
Aug 21, 2002

by Duchess Gummybuns
OK, I'm new to the whole rubygems thing...

code:
$ sudo gem install compass --pre
Successfully installed compass-0.10.0.rc1
1 gem installed
Installing ri documentation for compass-0.10.0.rc1...
Installing RDoc documentation for compass-0.10.0.rc1...
$ compass
compass: command not found
Any ideas as to what I'm missing? I'm on Ubuntu 9.10 using ruby 1.8

Pardot
Jul 25, 2001




Compass is fantastic.

For your problem though, the only thing I can think of is to check your $PATH

code:
~% sudo gem install compass --pre
Successfully installed compass-0.10.0.rc1
1 gem installed
~% compass
Nothing to compile. If you're trying to start a new project, you have left off the directory argument.
Run "compass -h" to get help.
~% which compass
/usr/bin/compass
That's on os x though. The other thing is that the ruby from ubuntu's apt really sucks. If getting this to work becomes much more of a headache, I'd recommend grabbing the rvm gem and using that to build a proper ruby 1.8.7.

Also be aware that there have been some important changes between 1.8.6 and 1.8.7. The biggest issue soon is that rails3 will only run on 187. There's a lot of stuff on 6 though, and it probably has nothing at all to do with your gem problem.

nbv4
Aug 21, 2002

by Duchess Gummybuns
code:
$find / -name compass
$
:rolleyes:

welp i just removed the apt version of rubygems and re-installed it from source and it works fine

NotShadowStar
Sep 20, 2000

nbv4 posted:

code:
$find / -name compass
$
:rolleyes:

welp i just removed the apt version of rubygems and re-installed it from source and it works fine

rvm, for god's sake, the answer is always lupus rvm

plasticbugs
Dec 13, 2006

Special Batman and Robin
I'm checking in to say that I've solved my own problem from a few posts up with regards to creating a hash in one step from an XML file using Nokogiri. For anyone getting started with Ruby or Nokogiri, this may be of some help.

Here's my refactored code:
code:
   @episodes = []
    doc.css('episode').each do |episode|
      @episodes << Hash[:title, episode.css('title').text, :link, episode.css('link').text]
    end
This saves me the extra steps of adding the titles and links to my array using separate operations. It also lets me add some logic in there to check whether a certain title even HAS a link.

Pardot
Jul 25, 2001




plasticbugs posted:

code:
   @episodes = []
    doc.css('episode').each do |episode|
      @episodes << Hash[:title, episode.css('title').text, :link, episode.css('link').text]
    end
You should use Enumerable#map
code:
   @episodes = doc.css('episode').map |episode|
      { :title => episode.css('title').text, 
        :link => episode.css('link').text }
    end

plasticbugs
Dec 13, 2006

Special Batman and Robin

Pardot posted:

You should use Enumerable#map
code:
   @episodes = doc.css('episode').map |episode|
      { :title => episode.css('title').text, 
        :link => episode.css('link').text }
    end

Thanks for this! I'm reading up on it right now.

Sharrow
Aug 20, 2007

So... mediocre.

Pardot posted:

Compass is fantastic.

What is compass anyway? I can see hundreds of blog posts telling me it's awesome, but every post is just 90% "this is how you use sass" and 10% "also it includes a bundled copy of blueprint and 960gs".

(Not that sass isn't amazing though.)

Pardot
Jul 25, 2001




Sharrow posted:

What is compass anyway? I can see hundreds of blog posts telling me it's awesome, but every post is just 90% "this is how you use sass" and 10% "also it includes a bundled copy of blueprint and 960gs".

(Not that sass isn't amazing though.)

Compass is built on sass. It gives you http://www.blueprintcss.org/ already sassified, and a bunch of general helpers and css3 helpers built in.

There are also some libraries built ontop of compass, such as the wonderful fancy-buttons

Only registered members can see post attachments!

roop
May 10, 2002

I am become Roberto, the destroyer of scoring chances
Say I have the following models:

Person
Job

With data:

Person:
- John
- Tony
- Bob

And Jobs:
- Driver
- Shipper
- Receiver
- Cook

When I'm creating the actual class for the model, I would have for Person:

code:
fields do
   name                 :string
   description       :text
   timestamps
end

has_many :Jobs
To show that each person can have many jobs and vice-versa in Job to show that each Job can be done by many Persons. You know, a many-to-many relationship.

Now my question is - what if each person has multiple primary Jobs and multiple secondary Jobs? How would that be expressed?

Would I need to create a third model called, for example, PrimaryJob that has a Person and Job? Then another one called SecondaryJob with Person and Job indexes?

I figure that would work but seems that there should be a way to do that within the Person/Job model without creating the extra model.

hmm yes
Dec 2, 2000
College Slice
@job.is_primary as a boolean could work

You could also use something like @job.priority, accepting an integer of 1/2/3 for primary/secondary/teartiary. It could also accept a string such as "Primary" or "Secondary"

You could also create a third model called Priority (made up of id, name) and assign a @job.priority_id

edit: or you could do this! VVVVVVVVV

hmm yes fucked around with this message at 13:07 on Mar 18, 2010

trinary
Jul 21, 2003

College Slice
has_many :x, :through => :y is how to specify extra attributes on the relationship between two other models. Y is its own model with whatever attributes you want for each individual association between a person and a job. I think it works like this:
code:
class Person
  has_many :people_jobs
  has_many :jobs, :through => :people_jobs
end

class PersonJob
  belongs_to :person
  belongs_to :job
  # add whatever properties you want to the relationship
  # int priority field like atastypie suggested?
end

class Job
  has_many :people_jobs
  has_many :people, :through => :people_jobs
end

skidooer
Aug 6, 2001
If jobs are unique there is no point in the added complexity of a join model.

code:
class Person < ActiveRecord::Base
  has_many :jobs
end

class Job < ActiveRecord::Base
  belongs_to :person

  scope :primary, where(:primary => true)
  scope :other, where(:primary => false)
end

# >> @person.jobs.primary
# >> @person.jobs.other
If jobs are shared, that is another matter.

skidooer fucked around with this message at 19:14 on Mar 18, 2010

roop
May 10, 2002

I am become Roberto, the destroyer of scoring chances

skidooer posted:

If jobs are unique there is no point in the added complexity of a join model.
That's the whole thing, jobs are not unique and each person has primary jobs and secondary jobs.

For example:
John, primary jobs: driver, cook, secondary jobs: shipper
Tony, primary jobs: shipper, secondary jobs: cook, receiver
Bob, primary jobs: driver secondary jobs: receiver

Need to the simplest way to express a many-to-many relationship multiple times that reference the same model.

Valrik
Apr 16, 2003

Bald!
I'm getting a NoMethodError: "undefined method 'connect' for Mysql:Class" when trying to connect to a MySQL database:

code:
require 'rubgems'
require 'mysql'
db = Mysql.connect('localhost', 'root', '', 'test')
Checked gem list and verified "mysql (2.8.1)" is present.

using Ruby 1.8.7 on OSX SnowLeopard

ideas?

Operation Atlas
Dec 17, 2003

Bliss Can Be Bought

Valrik posted:

I'm getting a NoMethodError: "undefined method 'connect' for Mysql:Class" when trying to connect to a MySQL database:

code:
require 'rubgems'
require 'mysql'
db = Mysql.connect('localhost', 'root', '', 'test')
Checked gem list and verified "mysql (2.8.1)" is present.

using Ruby 1.8.7 on OSX SnowLeopard

ideas?

Connect is an instance method. Instantiate an instance of the Mysql class and then call connect on that. That instance will be your "connection", instead of what you thought would be returned by connect().

trinary
Jul 21, 2003

College Slice

roop posted:

That's the whole thing, jobs are not unique and each person has primary jobs and secondary jobs.

For example:
John, primary jobs: driver, cook, secondary jobs: shipper
Tony, primary jobs: shipper, secondary jobs: cook, receiver
Bob, primary jobs: driver secondary jobs: receiver

Need to the simplest way to express a many-to-many relationship multiple times that reference the same model.

Read up on has_many :through and do something like what I suggested. :)

roop
May 10, 2002

I am become Roberto, the destroyer of scoring chances

trinary posted:

Read up on has_many :through and do something like what I suggested. :)
Aahh, a light bulb just went on in my head and it all makes sense now, thanks. This helped as well:

http://blog.hasmanythrough.com/2006/4/20/many-to-many-dance-off

Insurrectum
Nov 1, 2005

I'm new to web programming and have some general programming experience but I wouldn't say I'm extraordinarily adept. Mostly Numerical Recipes C++-type programming dealing with just numerical analysis.

I want to make a simple webapp that just does the following:

Take a query.
Analyze it.
Graph the output.

All I'm wondering is the relative difficulty and time it will take to implement this, and maybe some suggestions of any graphing packages to use.

The March Hare
Oct 15, 2006

Je rêve d'un
Wayne's World 3
Buglord
Alrighty then, I just spent hours bashing my head against my keyboard trying to figure out what was going on and I think I did. However, despite knowing what is wrong, my mind is still being blown. I'm new to ruby so be gentle.

I used scaffolding to, well, scaffold. I did this because a book told me to. In the process of this I generated a routes.rb in /config. I kept making links to other pages off of my main page, like /about.html.erb and wondering why it was telling me that it wasn't defined and that it didn't exist etc.

Then it dawned on me, it was trying to find words/about as though about was an entry in my database, and not a website. So, I deleted the map.resources :words thing in my routes file and went to /about - there it is! Wahoo! Problem solved, onto bigger and better things. Not. Because doing that seems to break all of my other pages (doh) and I just can't take it anymore. Is there some way for me to escape the routes thing with link_to so that it doesn't try to go to about as an db entry? Better yet, is there a way for me to just remove the /say/ option entirely while keeping the other ones intact? That would really be ideal, since I don't particularly need a /say/ option and would really like to be able to make this thing link to other pages at the same time.

Pardot
Jul 25, 2001




Insurrectum posted:

maybe some suggestions of any graphing packages to use.

Do the graphing client side and use flot. The server side option would be something built on rmajgick, but unless things have changed in the last year or so, everyone hates rmagick.

The March Hare posted:

routes

Post your routes and version of ruby, read Rails Routing from the Outside In, and run rake routes to see what all your link helper methods are and where they go.

The March Hare
Oct 15, 2006

Je rêve d'un
Wayne's World 3
Buglord
Pardot, thanks for the answer! I actually solved this last night after some dude in the official rails irc spoke at me in third person while distributing tons of links to me. That said, while I now have a better grasp of routing, I'm still going to read what you linked because I'd really like to understand this stuff 100%

Adbot
ADBOT LOVES YOU

ChadyG
Aug 2, 2004
egga what?

Insurrectum posted:

I'm new to web programming and have some general programming experience but I wouldn't say I'm extraordinarily adept. Mostly Numerical Recipes C++-type programming dealing with just numerical analysis.

I want to make a simple webapp that just does the following:

Take a query.
Analyze it.
Graph the output.

All I'm wondering is the relative difficulty and time it will take to implement this, and maybe some suggestions of any graphing packages to use.

There's also Google's charts API
static http://code.google.com/apis/chart/image_charts.html
dynamic http://code.google.com/apis/visualization/interactive_charts.html

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply