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
Novo
May 13, 2003

Stercorem pro cerebro habes
Soiled Meat

RonaldMcDonald posted:

how would the following PHP code look in RoR?

If I wanted a page that listed all people ordered by lastname, I could do this:

In app/controllers/person_controller.rb:

code:
class PersonController < ApplicationController
   def list
      @people = Person.find(:all, :order => 'lastname ASC')
   end
end
Here I am saying, "there is an action called list in the person controller, it finds all people ordered by lastname (ascending) and puts the collection in @people". In a real application you would put other actions for managing people here.

In app/models/person.rb:
code:
class Person < ActiveRecord::Base
   def age
      Time.now.year - birthdate.year
   end
end
Defining this data model assumes a table called "persons" (you can override this easily) and is what allows you to find people using Person.find in your controller (above). To make your example more interesting I've defined a simple method which will return the approximate age of the person. This assumes a persons.birthdate datetime column.

In app/views/person/list.rhtml:
code:
<table>
<% @people.each do |person| %>
 <tr>
   <td><%= person.lastname -%></td>
   <td><%= person.firstname -%></td>
   <td><%= person.age -%></td>
 </tr>
<% end %>
</table>
This view simply steps through each person in @people and renders the appropriate HTML. Notice that we are using the age method in our view. We are done. Going to http://myapp.com/person/list would render list.rhtml above. In a .rhtml template (or "view"), statements inside <% %> are evaluated, <%= %> is evaluated and echoed, and ending with -%> suppresses the final newline.

To highlight another convenience of Rails, if I have a site template, I simply put that in app/views/layouts/application.rhtml and it will wrap other views:
code:
<html>
 <head>
  <title>My Site</title>
 </head>
 <body>
  <!-- content gets inserted where you yield -->
  <%= yield -%>
 </body>
</html>
Simple.

Novo fucked around with this message at 00:18 on Aug 9, 2007

Adbot
ADBOT LOVES YOU

Novo
May 13, 2003

Stercorem pro cerebro habes
Soiled Meat

SpaceNinja posted:

can anybody recommend a better MySQL browser under Windows?
I like SQLyog

Novo
May 13, 2003

Stercorem pro cerebro habes
Soiled Meat

Deus Rex posted:

Is ActiveResource really as goddamn slick as it seems? Automatically map REST resources from an external API to models in my application? :aaa:

I spend more time fighting with ActiveResource than any other part of Rails. Of course, I'm trying to make it do crazy things like use non-numeric IDs. I have enough trouble getting it to work between two Rails apps, I wouldn't even bother trying it against a third party API.

Novo
May 13, 2003

Stercorem pro cerebro habes
Soiled Meat
Sometimes you want to control the order in which things show up. I have gotten into the habit of defining my navigation in lib/appname/navigation.rb using an Array instead of a Hash.

code:
module Porthole::Navigation
  TREE = [
    [ 'accounts',    :accounts    ],
    [ 'invitations', :invitations ],
    [ 'groups',
      [
        [ 'authorization', :groups_auth_groups ],
        [ 'distribution',  :groups_dist_groups ]
      ]
    ],
    [ 'reports',
      [
        [ 'entitlement flags', :entitlements_report     ],
        [ 'expiring soon',     :expiring_report         ],
        [ 'expired',           :expired_report          ],
        [ 'status mismatches', :status_mismatch_report  ],
        [ 'email forwarding',  :email_forwarding_report ]
      ]
    ],
    [ 'logs', :logs ]
  ]
end
It's pretty easy to traverse TREE and generate nested navigation markup for Bootstrap.

Also, you should never be writing literal URLs in a Rails app, use the route names or _url helpers, they will save you a lot of hassle.

Novo
May 13, 2003

Stercorem pro cerebro habes
Soiled Meat

Sparta posted:

My company is offering a nice finders-fee to any of us who recruit a ruby/rails developer. I've been posting ads on cl, responding to resumes, etc, but I have yet to even get a response back.

How do I find rails developers looking for a job?

Have you tried GitHub / StackOverflow's jobs page? I check those when I'm bored and thinking about moving on. Just don't do what Rumblefish did: contacted me out of the blue, refused to say how they found me, complained about how hard it was to find "good developers" then stopped talking to me entirely without so much as a "we dont' think you'd be a good match because _______" after I agreed to talk with them. That type of poo poo is not going to make finding good developers any easier.

Novo
May 13, 2003

Stercorem pro cerebro habes
Soiled Meat

prom candy posted:

It's times like this that I hate managing 150+ Rails applications across a ton of different versions.

I feel your pain. I provide infrastructure support for almost 40 Rails apps ranging from 2.3 to 3.2, and maintain a few of those apps myself as well. There are days where I end up hating the entire Ruby ecosystem. We're still on 1.8.7 so I'm simultaneously dreading and looking forward to moving to 1.9.

Novo
May 13, 2003

Stercorem pro cerebro habes
Soiled Meat
From the Rack homepage:

quote:

Rack provides a minimal interface between webservers supporting Ruby and Ruby frameworks.

Rack defines an interface that web servers and Ruby web application frameworks can use to pass HTTP requests and responses back and forth in a standard way. It's what allows things like Passenger to run both Rails and Sinatra applications. It also allows you to put functionality into framework-agnostic "middleware", (things like JSONP responses or cookie verification). As long as the web server and framework support Rack, they can interoperate.

If you are using Sinatra or Rails, or just about any Ruby web app framework, you are probably using Rack. (Though you may not be using the affected middlewares: Rack::File and Rack::Session::Cookie).

Novo fucked around with this message at 23:03 on Feb 8, 2013

Novo
May 13, 2003

Stercorem pro cerebro habes
Soiled Meat

kitten smoothie posted:

Is Railsconf worth the money? Google I/O registration failed me this year and so now I'm trying to decide if I should burn the money on Railsconf instead.

I find that I can get as much or more out of watching videos of conference talks after the fact without flying across the country to sit in a room where everyone is dicking around on their laptops. I've been told that this means I don't "get" conferences though, so YMMV.

Novo
May 13, 2003

Stercorem pro cerebro habes
Soiled Meat

Smol posted:

an IDE like Rubymine (highly recommended, it's well worth the price)

Counterpoint: If you type fast and/or are used to the responsiveness of editors like Sublime Text, Rubymine will feel like you are using VNC or something. This kills my flow, but YMMV.

Novo
May 13, 2003

Stercorem pro cerebro habes
Soiled Meat

The Journey Fraternity posted:

Really should be built-in functionality, but what can you do?

(happy rbenv user here, screw rvm)

gem install rbenv-rehash

Smol posted:

I hope that you're not seriously as a Ruby developer complaining about Java performance.

I wouldn't use an IDE written in Ruby either if it was as slow as RubyMine is. What a lemon.

Novo
May 13, 2003

Stercorem pro cerebro habes
Soiled Meat

xtal posted:

I actually test if my classes inherit from ActiveRecord::Base sometimes. You can't have too many tests.

This is dumb; you might as well be writing tests to make sure your models respond to Object methods.

Novo
May 13, 2003

Stercorem pro cerebro habes
Soiled Meat

Cocoa Crispies posted:

TDD is driving your development with tests, not writing your code once in test form and once in implementation form.

Testing inheritance actively hurts you, because you'll have to fix that test if you change the class's implementation (say, from ActiveRecord to DataMapper or Ripple or a plain old Object that calls methods on other classes).

I justify testing associations by using it as a design phase: I mash out association tests, pending tests for features that haven't been implemented, and validations I'll know I want. Then I use the test output as a gauge to know when I'm done.

Exactly. If you are defining classes at runtime then testing a parent class is defensible, but testing that "class Foo < Bar" has the desired effect is way outside the scope of what you should be testing, TDD or no.

Novo
May 13, 2003

Stercorem pro cerebro habes
Soiled Meat

Fillerbunny posted:

How do you determine what's in scope?

I test the methods I've implemented, not the ones Ruby implements. I also don't test things that are implemented by frameworks or libraries I'm using.

I set up the preconditions (if necessary), feed my code inputs, and check that the outputs conform to my expectations. I don't test that the implementation was expressed in a particular way, because that's not what tests are for.

OOP is supposed to help you separate the "what" from the "how", the interface from the implementation, etc. When I am refactoring part of my implementation, I want my failed tests to tell me "hey, these things stopped working the way they're supposed to!" not "hey, you forgot to subclass FooBarBaz!"

Basically, even a clean well-designed OO codebase is full of assumptions, conventions, and other implicit behaviors. Testing should be about making these things explicit so that if you decide to change a class hierarchy or whatever, you have a way of knowing that you still conform. If your test just fails because you did some refactoring (but didn't change the behavior in any way) then it's not useful.

Novo fucked around with this message at 00:10 on Aug 30, 2013

Novo
May 13, 2003

Stercorem pro cerebro habes
Soiled Meat
I wanted to like RSpec but after using it for a while I realized my Test::Unit tests were way more maintainable. When it comes down to it all you really need in a testing framework is "assert", the DSL frameworks like Cucumber are just trendy conventions created for and by idiots.

Novo
May 13, 2003

Stercorem pro cerebro habes
Soiled Meat
Right, when I said that my RSpec test were less maintainable than my Test::Unit tests, I literally meant that the way I found myself writing RSpec over time (trying different ways of structuring my assertions, trying to apply DRY, etc) was less maintainable for me than the way I found myself writing Test::Unit tests. Follow your bliss.

Novo
May 13, 2003

Stercorem pro cerebro habes
Soiled Meat

prom candy posted:

You know, it's possible to say you prefer Test::Unit without insulting everyone who likes to use different tools.

Can I still make fun of Windows and PHP?

Novo
May 13, 2003

Stercorem pro cerebro habes
Soiled Meat

kayakyakr posted:

yeah, looks like in your cases, you're pretty much doomed. Maybe look at modifying the capistrano 3 bundler gem to check for gemfile change?

Cap 3, btw, is nice once you get through the learning curve. Having switched to pure rake helped.

I've had pretty good success using "bundle check || bundle install" instead of just "bundle install"

Adbot
ADBOT LOVES YOU

Novo
May 13, 2003

Stercorem pro cerebro habes
Soiled Meat

The Journey Fraternity posted:

Am I the only person left in TYOOL 2016 that manually adds every JS library that he uses? Granted, I don't use much, but still.

I never use bullshit like bower or npm and I get along just fine. If a library requires me to use either of those abominations I use something else because chances are the author is an idiot.

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