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
necrotic
Aug 2, 2005
I owe my brother big time for this!
code:
@some_array.to_json

That will print out a json encoded version of the object, which can go directly into your javascript block.

Adbot
ADBOT LOVES YOU

Pollyanna
Mar 5, 2005

Milk's on them.


We're being taught differently, as we use Rails as an API server (database, AJAX, etc.) and don't bother to use the asset pipeline or Javascript libraries since it's headless. Instead, we create separate front-end static asset servers that hand out HTML, CSS and JS to the client which then access the API server to render everything. Is this common?

Lexicon
Jul 29, 2003

I had a beer with Stephen Harper once and now I like him.

Pollyanna posted:

We're being taught differently, as we use Rails as an API server (database, AJAX, etc.) and don't bother to use the asset pipeline or Javascript libraries since it's headless. Instead, we create separate front-end static asset servers that hand out HTML, CSS and JS to the client which then access the API server to render everything. Is this common?

I would characterize this approach as 'modern' if not necessarily 'common' (yet). It's not unusual to see this done with front-end frameworks like Ember.js, etc.

MasterSlowPoke
Oct 9, 2005

Our courage will pull us through
I do ajax this way:

  • I have an action that responds to format.js
  • I have a link like this on a view
    Ruby code:
    link_to 'Action', action_model_path(model), remote: true
  • I have my javascript that I want executed when that link is pressed in action.js.erb under that model's views

kayakyakr
Feb 16, 2004

Kayak is true
e: ^^ that's a very old-school way of doing it.

Pollyanna posted:

We're being taught differently, as we use Rails as an API server (database, AJAX, etc.) and don't bother to use the asset pipeline or Javascript libraries since it's headless. Instead, we create separate front-end static asset servers that hand out HTML, CSS and JS to the client which then access the API server to render everything. Is this common?

It happens. I am doing something similar in one of my projects, except that I am using the asset pipeline and ember-rails to package up my assets and I have 3 traditional controllers that are serving the root pages.

I don't think that should be something you always do, some apps (both simple and complex) are better designed as traditional rails apps. I would only do that if it makes sense for the site to be a single page or if you're going to have a high amount of JS interaction anyway.

KoRMaK
Jul 31, 2012



MasterSlowPoke posted:

I do ajax this way:

  • I have an action that responds to format.js
  • I have a link like this on a view
    Ruby code:
    link_to 'Action', action_model_path(model), remote: true
  • I have my javascript that I want executed when that link is pressed in action.js.erb under that model's views
Same.

Why ditch erb?

Dystram
May 30, 2013

by Ralp

Peristalsis posted:

Well there's 3 hours of my life I'm never getting back.

It turns out that this is an array variable (list of row id's). Is there a best way to send this through the AJAX data setting?


That ship has sailed for this app.

https://github.com/haml/html2haml

necrotic
Aug 2, 2005
I owe my brother big time for this!

Pollyanna posted:

We're being taught differently, as we use Rails as an API server (database, AJAX, etc.) and don't bother to use the asset pipeline or Javascript libraries since it's headless. Instead, we create separate front-end static asset servers that hand out HTML, CSS and JS to the client which then access the API server to render everything. Is this common?

We're doing this now at work. Our monorail, which contains the old dashboard and all of our business logic, now exposes an api that a separate project speaks to. The assets are served up through the monorail in the end (well, a cdn, but the view lives in the monorail), but we are eventually moving that away too.

Smol
Jun 1, 2011

Stat rosa pristina nomine, nomina nuda tenemus.
Yep using asset pipeline sucks for SPAs. Best to keep the frontend completely separate. You get to use better build systems as well.

A MIRACLE
Sep 17, 2007

All right. It's Saturday night; I have no date, a two-liter bottle of Shasta and my all-Rush mix-tape... Let's rock.

Knockout is a pretty good library if you just want a nice interface helper. You have to write all the ajax stuff yourself though.

MasterSlowPoke
Oct 9, 2005

Our courage will pull us through

kayakyakr posted:

e: ^^ that's a very old-school way of doing it.

Do you know of a tutorial for a more modern way of doing it? I learned that from the rails docs.

kayakyakr
Feb 16, 2004

Kayak is true

MasterSlowPoke posted:

Do you know of a tutorial for a more modern way of doing it? I learned that from the rails docs.

Not really. New school is mostly using Backbone, Angular, Ember, or something similar, combined with jquery to make calls against a JSON interface, or in the case of Ember, a custom made store (since I didn't like ember-data or the other ember data stores)

Myself, if I'm doing a traditional rails app with server-rendered interfaces, I usually build and initialize singleton classes listening for events on various items (generally class-based, rarely id based), pulling data out of a data element. Serve it all up via sprockets. Ex:

JavaScript code:
// table-handler.js
(function(){
  // put load actions here. This is called once.

  var self,
      h = function(){
    $(document).on('click', 'tr btn.delete-item', this.deleteItem);
  };

  h.prototype = {
    deleteItem: function(){
      var row = $(this).closest('tr'),
          id = $(this).closest('tr').data('id');
      $.ajax({url: '/items/' + id, method: 'DELETE'}).then(function(){
        row.hide();
      });
    }
  }

  window.TableHandler = self = new h();
})();

$(function(){
  // put page actions here, such as loading a table. This is called every page change
});
If I have to do anything more complex, say loading and drawing rows, I'll bring in a library like backbone or can.js, which integrates templating nicely with the above style. If I there is more than a mid level of JS interaction, say more than 4-5 buttons/page to hook up, client side business logic, and whatnot, then I'll opt for a heavier framework like Ember.

Mind, this is my own style that I've sorta built up from using various frameworks and various lack of frameworks. It's pretty old school prototypal JS, but I'm going to claim that makes it flexible. I can easily refactor to use a framework's classing system and it doesn't make me feel dirty like most jquery code does.

Pollyanna
Mar 5, 2005

Milk's on them.


Factory Girl is pissing me off. It can't seem to find the factories that it makes itself. I have a spec/factories/tests.rb with this content:

Ruby code:
FactoryGirl.define do
  factory :test do
    text "test"
  end
end
which was generated directly by Factory Girl using rails g factory_girl:model Test text. I still get this error:

code:
Failures:

  1) Tests API #index lists all tests
     Failure/Error: test = FactoryGirl.build(:test)
     ArgumentError:
       Factory not registered: test
     # ./spec/requests/tests_spec.rb:8:in `block (3 levels) in <top (required)>'

even though trying to run the command again tells me it is registered:

code:
/usr/local/lib/ruby/gems/2.1.0/gems/factory_girl-4.5.0/lib/factory_girl/decorator.rb:10:
in `method_missing': Factory already registered: test (FactoryGirl::DuplicateDefinitionError)
I have no idea why my factory both exists and doesn't exist at the same time. I've checked all the configurations and they look correct. This is driving me insane :tizzy:

edit: Apparently factories can't exist without models. I'm crying.

edit 2: NOPE I take that back it's still not working.

Pollyanna fucked around with this message at 22:01 on Nov 10, 2014

kayakyakr
Feb 16, 2004

Kayak is true

Pollyanna posted:

edit: Apparently factories can't exist without models. I'm crying.

this makes sense: factory girl is all about initializing your models with data in a repeatable, predictable way. If you don't have a model, then what is it initializing?

Pollyanna posted:

edit 2: NOPE I take that back it's still not working.

This also sorta makes sense if you haven't required your factory files in some form or fashion.

necrotic
Aug 2, 2005
I owe my brother big time for this!

kayakyakr posted:

This also sorta makes sense if you haven't required your factory files in some form or fashion.

Yeah, do you have the factory_girl_rails gem? It handles loading factories automatically. It looks like you do based on the use of generators. I've never had this problem myself. Maybe something weird with your bundle file and test not finding factory_girl_rails? :iiam:

Pollyanna
Mar 5, 2005

Milk's on them.


I forgot to add require 'rails_helper' to my spec files. :shepicide:

necrotic
Aug 2, 2005
I owe my brother big time for this!

Pollyanna posted:

I forgot to add require 'rails_helper' to my spec files. :shepicide:

Yeah that'll do it. Glad it was a dumb mistake and not some arcane issue with gem deps!

Pollyanna
Mar 5, 2005

Milk's on them.


Is there a recommended guide/book for making a Rails API out there? There's a bunch of different ones that talk about using RSpec for integration, Cucumber for integration, doing metaprogramming for all your different resource controllers, etc. All of them tend to be very sketchy quality and I still don't even know if what I'm doing is a good idea. Does anyone have a workflow/specs they like?

plasticbugs
Dec 13, 2006

Special Batman and Robin
I'm driving down to Rubyconf in San Diego from San Francisco on Sunday. Are any of you going? It's my first, and I have no friends. :ohdear:

Pardot
Jul 25, 2001




plasticbugs posted:

I'm driving down to Rubyconf in San Diego from San Francisco on Sunday. Are any of you going? It's my first, and I have no friends. :ohdear:

They rejected my talk :mad:

plasticbugs
Dec 13, 2006

Special Batman and Robin

Pardot posted:

They rejected my talk :mad:

Well, that sucks. What was your talk going to be on?

I'm torn between a few of the scheduled talks that take place at the same time. I consider myself at the intermediate level, but some of the beginner talks sound interesting enough that I might learn a couple new things. The alternative is taking a chance on an advanced talk and it all going a little over my head.

KoRMaK
Jul 31, 2012



New Question: whats the best way to replicate the functionality of rails 4 routing conerns? This looks pretty good, is there anything better? http://ruby-journal.com/how-to-dry-your-rails-routes/

I have a handful of routes that I want to go to a non-restful controller, how do I group a couple of routes to the same controller without doing this:
Ruby code:
match "my_controller/action_1"
match "my_controller/action_2"
match "my_controller/action_3"
etc
I'd rather see

Ruby code:
:controller => :my_controller do
  match :action_1
  match :action_2
  etc...
end
and still have the route be "my_controller/action_1". The doc mentions this, but it gives me "action_1" as a route instead.



e: Got it

Ruby code:
namespace :my_controller do
  controller :my_controller do
    match :action_1
    match :action_2
    etc...
  end
end
or

Ruby code:
namespace :my_controller, :controller => :my_controller do
    match :action_1
    match :action_2
    etc...
end

KoRMaK fucked around with this message at 16:23 on Nov 18, 2014

The Gman Drive
Jun 28, 2012

I'm a Mac Hole. Yes... I do like Macs up the ass.
I am part of the ETCE (Ethical Treatment of Consumer Electronics).
Now go shove an apple up your ass... you'll see....
I'm being forced to take the

Ruby Association Certified Ruby Programmer Silver given by "Ruby Association". Was wondering if anyone had taken it, and had any study guidance. I contacted the "Ruby Association" and they state they are planning on putting out a study guide.

I also realize this cert, it probably bogus, and is a waste of my time... but figured I would ask if any of you poor souls had/chose to take the drat test.

fantastic in plastic
Jun 15, 2007

The Socialist Workers Party's newspaper proved to be a tough sell to downtown businessmen.
Some poor idiot will actually pay for that? Clearly I'm in the wrong business and should stop everything, make a clone of RubyMonk that includes "competence tests" and charge people a couple hundred bucks to get the Tao Jones Ruby Institute Silver Star or whatever.

Molten Llama
Sep 20, 2006
Partnering with Prometric isn't cheap and the Ruby Association claims to be chaired by Matz, so it's probably not bogus.

Probably not very useful, either, but not totally bogus.

xenilk
Apr 17, 2004

ERRYDAY I BE SPLIT-TONING! Honestly, its the only skill I got other than shooting the back of women and calling it "Editorial".
Random questions but what would you recommend someone who's almost done with the rails tutorial ebook (I'm at chapter 10/12) and has done the Code School classes and would be looking for something to continue learning?

MrDoDo
Jun 27, 2004

You better remember quick before we haul your sweet ass down to the precinct.

xenilk posted:

Random questions but what would you recommend someone who's almost done with the rails tutorial ebook (I'm at chapter 10/12) and has done the Code School classes and would be looking for something to continue learning?

Find a pet project and just starting working on it. Its the best learning experience you can get. Also try your best to do as much TDD as possible while doing it. If you want to learn how to be a better ruby programmer I would suggest the books Eloquent Ruby (haven't read it yet but hear great things) and Metaprogramming Ruby.

EDIT: Also The Rspec Book and Practical Object Oriented Design in Ruby are great as well.

Chilled Milk
Jun 22, 2003

No one here is alone,
satellites in every home
Yeah, Eloquent Ruby, POODR, and The RSpec Book are the three must have books. You're unlikely to grasp the majority of it the first time through but the more you expose yourself to the concepts as you work the more you'll absorb and improve.

Peristalsis
Apr 5, 2004
Move along.

The Milkman posted:

Yeah, Eloquent Ruby, POODR, and The RSpec Book are the three must have books. You're unlikely to grasp the majority of it the first time through but the more you expose yourself to the concepts as you work the more you'll absorb and improve.

I'm sure this is heresy or something, but is RSpec really that much better than the built in testing suite? I found a quick introduction to it in one of the books I read (Eloquent Ruby, maybe?), very poor documentation and examples online, and I just didn't see how it was fundamentally better than the unit tests and fixtures. Are there tests I can do in RSpec that I can't do with the built-in testing framework, or is it just supposed to be more slick and fashionable?

I'll admit that I find it tiresome that every Rails tool I use has something newer/shinier/better that everyone says I should be using instead (except when I shouldn't) - Pry instead of the built-in debugger, HAML instead of erb, RSpec instead of whatever the built-in tools are called, and so on. I'd rather get proficient at using a set of good tools, than constantly be in learning curves for things that are only marginally better, and possibly not as stable.

As an example, we recently updated our Ruby and Rails versions, which made the built-in debugger almost useless. So, we're using Pry now, but I get some weird behavior from it sometimes, as well as some memory errors in my testing. I don't know if Pry isn't quite ready for prime time, or I just don't know how to use it correctly. I also don't know if Pry is causing the memory errors, or if something in my code changes is doing that. So, everything slows down and gets harder just because we want to recapture the same functionality we had before upgrading.

Sorry, I'll quit ranting now.

Peristalsis fucked around with this message at 16:51 on Nov 21, 2014

necrotic
Aug 2, 2005
I owe my brother big time for this!

Peristalsis posted:

I'm sure this is heresy or something, but is RSpec really that much better than the built in testing suite? I found a quick introduction to it in one of the books I read (Eloquent Ruby, maybe?), very poor documentation and examples online, and I just didn't see how it was fundamentally better than the unit tests and fixtures. Are there tests I can do in RSpec that I can't do with the built-in testing framework, or is it just supposed to be more slick and fashionable?

I'll admit that I find it tiresome that every Rails tool I use has something newer/shinier/better that everyone says I should be using instead (except when I shouldn't) - Pry instead of the built-in debugger, HAML instead of erb, RSpec instead of whatever the built-in tools are called, and so on. I'd rather get proficient at using a set of good tools, than constantly be in learning curves for things that are only marginally better, and possibly not as stable.

As an example, we recently updated our Ruby and Rails versions, which made the built-in debugger almost useless. So, we're using Pry now, but I get some weird behavior from it sometimes, as well as some memory errors in my testing. I don't know if Pry isn't quite ready for prime time, or I just don't know how to use it correctly. I also don't know if Pry is causing the memory errors, or if something in my code changes is doing that. So, everything slows down and gets harder just because we want to recapture the same functionality we had before upgrading.

Sorry, I'll quit ranting now.

RSpec doesn't let you do more than the core Test framework, but it is far more expressive. If you're doing BDD/TDD its a much stronger tool as you can literally describe your code before your write it out. Then you write the code and it all works. You can probably do it successfully with Test, and really it boils down to personal preference. I can use either, but if I am doing TDD I vastly prefer rspec.

Jaded Burnout
Jul 10, 2004


Peristalsis posted:

I'm sure this is heresy or something, but is RSpec really that much better than the built in testing suite? I found a quick introduction to it in one of the books I read (Eloquent Ruby, maybe?), very poor documentation and examples online, and I just didn't see how it was fundamentally better than the unit tests and fixtures. Are there tests I can do in RSpec that I can't do with the built-in testing framework, or is it just supposed to be more slick and fashionable?

I'll admit that I find it tiresome that every Rails tool I use has something newer/shinier/better that everyone says I should be using instead (except when I shouldn't) - Pry instead of the built-in debugger, HAML instead of erb, RSpec instead of whatever the built-in tools are called, and so on. I'd rather get proficient at using a set of good tools, than constantly be in learning curves for things that are only marginally better, and possibly not as stable.

As an example, we recently updated our Ruby and Rails versions, which made the built-in debugger almost useless. So, we're using Pry now, but I get some weird behavior from it sometimes, as well as some memory errors in my testing. I don't know if Pry isn't quite ready for prime time, or I just don't know how to use it correctly. I also don't know if Pry is causing the memory errors, or if something in my code changes is doing that. So, everything slows down and gets harder just because we want to recapture the same functionality we had before upgrading.

Sorry, I'll quit ranting now.

RSpec has a larger feature set than TestUnit, things like more human-readable syntax, much more useful feedback, that sort of thing. It's also been around long enough to respond to a lot of its early problems, like too much Ruby metaprogramming magic which it's now backing away from. There's a whole suite of features I never use in RSpec but I'm happy for the ones that exist. TestUnit hasn't changed in 10 years and that's the way some people like it.

With any of these things nobody who isn't a complete tool will think less of you for using one over the other, be that rspec vs testunit, erb vs haml, etc etc. Use what works for you and your team.

With this and all things I've always found having a standard to work to is more important than what that standard actually is.

kayakyakr
Feb 16, 2004

Kayak is true
when I have to write tests, I prefer rspec.

Kiddos, learn how to do TDD early, because when you get into the bad habit of not doing it, then you'll hate testing forever.

Jaded Burnout
Jul 10, 2004


kayakyakr posted:

when I have to write tests, I prefer rspec.

Kiddos, learn how to do TDD early, because when you get into the bad habit of not doing it, then you'll hate testing forever.

I was fortunate enough that my first Rails job insisted on 100% BDD.

xenilk
Apr 17, 2004

ERRYDAY I BE SPLIT-TONING! Honestly, its the only skill I got other than shooting the back of women and calling it "Editorial".

MrDoDo posted:

Find a pet project and just starting working on it. Its the best learning experience you can get. Also try your best to do as much TDD as possible while doing it. If you want to learn how to be a better ruby programmer I would suggest the books Eloquent Ruby (haven't read it yet but hear great things) and Metaprogramming Ruby.

EDIT: Also The Rspec Book and Practical Object Oriented Design in Ruby are great as well.

Yeah I've been working on a project to harden my rails skills but I just like reading on it from time to time :)

I do need to practice my TDD tho!

Thanks for the recommendation, I've ordered Eloquent Ruby :)

MasterSlowPoke
Oct 9, 2005

Our courage will pull us through
First thing I did at my new job was look in /test/. Nothing.

good jovi
Dec 11, 2000

'm pro-dickgirl, and I VOTE!

I had a interviewer ding my code sample for no tests because he didn't look in spec/.

Peristalsis
Apr 5, 2004
Move along.

xenilk posted:

Yeah I've been working on a project to harden my rails skills but I just like reading on it from time to time :)

I do need to practice my TDD tho!

Thanks for the recommendation, I've ordered Eloquent Ruby :)

I enjoyed Eloquent Ruby pretty well. I also liked Design Patterns In Ruby. It's more specialized, but there's still some good, generally useful stuff in there, in addition to the pattern-specific parts.


Arachnamus posted:

With any of these things nobody who isn't a complete tool will think less of you for using one over the other, be that rspec vs testunit, erb vs haml, etc etc. Use what works for you and your team.

With this and all things I've always found having a standard to work to is more important than what that standard actually is.

Somewhere deep within the bowels of my mind, I knew this. I guess I was just blowing off some steam.

Peristalsis fucked around with this message at 21:30 on Nov 21, 2014

A Big... Dog
Mar 25, 2013

HELLO DAD

kayakyakr posted:

when I have to write tests, I prefer rspec.

Kiddos, learn how to do TDD early, because when you get into the bad habit of not doing it, then you'll hate testing forever.

a thousand times this. it happened to me and i'm just getting good at testing a year on.

Smol
Jun 1, 2011

Stat rosa pristina nomine, nomina nuda tenemus.

MasterSlowPoke posted:

First thing I did at my new job was look in /test/. Nothing.

I assume it was right before you left your resignation letter.

Maintaining rails apps is hard without good test coverage. Don't even try to skimp on tests, kids.

Adbot
ADBOT LOVES YOU

necrotic
Aug 2, 2005
I owe my brother big time for this!

Smol posted:

Maintaining rails apps programming is hard without good test coverage. Don't even try to skimp on tests, kids.

Extends to libraries as well.

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