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
The Journey Fraternity
Nov 25, 2003



I found this on the ground!

TheFlyingDutchman posted:

On that note, is there any good books out there for RoR? Yes, there's the online documentation, but sometimes a book does a bit more for me. Anyone have any suggestions?

The book on Rails is probably "Agile Web Development with Rails", by Dave Thomas. It touches on almost every aspect of the framework, but since the community likes to talk about how they'd rather "run on edge," parts of it get outdated rather quickly. You'll also find yourself reading the very sparsely documented API docs online rather often, but there's a project to spruce that up as well.

Adbot
ADBOT LOVES YOU

The Journey Fraternity
Nov 25, 2003



I found this on the ground!

Taliesin posted:

What's that one blog about RoR where the guy is also a writer?

why the lucky stiff, maybe?

The Journey Fraternity
Nov 25, 2003



I found this on the ground!
"./script/generate X foo -c" will add it to SVN as well, if you like saving three keystrokes v:)v

The Journey Fraternity
Nov 25, 2003



I found this on the ground!

burntoutjoy posted:

@posts.empty?

:)

This is the best one right here. Short, concrete, and simple.

The Journey Fraternity
Nov 25, 2003



I found this on the ground!

Hop Pocket posted:

Is there a way to disable SQL logging for some statements within Rails? I have an images table that I'm uploading somewhat large files to, and the SQL logging is causing all sorts of Terminal.app problems. I don't want to disable SQL logging completely in the development environment, but I would like to be able to not have it log those statement that are doing INSERTs into the images table.

If you're putting them in their own column, I suppose you could use filter_parameter_logging, but that might not completely fit your needs. This might also cause problems if you have columns similarly-named to your image data column elsewhere in your models.

The Journey Fraternity fucked around with this message at 07:09 on Nov 29, 2007

The Journey Fraternity
Nov 25, 2003



I found this on the ground!

Adolf Hitler posted:

Oh my god, why doesn't Hobo support rails 2.3.2? I'm going to shoot myself in the head.

Have you looked into its bleeding edge? According to their blog, they're prepping for a 1.0 release that might scratch your 2.3.2 itch.

The Journey Fraternity
Nov 25, 2003



I found this on the ground!
VirtualBox is pretty nice, I'd toss my vote in for that too.

The Journey Fraternity
Nov 25, 2003



I found this on the ground!

Mental Filler posted:

I was really hoping this was the case and I was just an idiot but sadly it's the post that went wrong, it really is TestModule (and test_module.rb if that makes any difference).

You still need to explicitly require the module.

require 'lib/test_module' at the top of your controller.

The Journey Fraternity
Nov 25, 2003



I found this on the ground!

clockwork automaton posted:

If anyone is in the Madison area or is interested in going to Madison Ruby Conf I will be there speaking and I have a $50 off coupon code for admission: lindseybieda. I will also be speaking at Steel City Ruby Conf which luckily is only $50, so no coupon necessary. :)

Awesome, I'll be at SCRC with my coworker. I'd say it's a small world but there really are way too many SA members everywhere.

The Journey Fraternity
Nov 25, 2003



I found this on the ground!

etcetera08 posted:

Not Rails-related, but since we don't have a Ruby thread.. I am having a weird thing where a
Ruby code:
require 'lastfm'
is giving me a "stack level too deep" error. Any ideas what's happening/why? I figure it's something simple I'm too stupid to see.

edit: if I do it in irb it gives me the same error and then says "Maybe IRB bug!!" :confused:

Do you have another file in your load path named lastfm.rb that might be requiring the file with that line?

The Journey Fraternity
Nov 25, 2003



I found this on the ground!
If you're dropping the entire database to reseed it, why not just load the database from schema?

code:
rake bundle exec rake db:drop db:create db:schema:load db:seed db:test:prepare

The Journey Fraternity
Nov 25, 2003



I found this on the ground!
I should also probably point out that you have three different things that rating could be referring to in the vote_for_user method. :v:

Even though you have a local var as one of the three and the other two aren't being referred to, it still would be confusing in the future when things don't work as expected.

The Journey Fraternity
Nov 25, 2003



I found this on the ground!

Physical posted:

I'm using MySQL and my where clauses is changed to
Ruby code:
:criteria => 'NULL' 
and it is still substituting it with 0. The db column is a integer type.
Doing this
Ruby code:
where('criteria is NULL')
Get's the results I want, but then I have to build my own string before hand which is what I wanted to avoid.

e: It looks like it should work, I just have to examine what I have a little bit more. Thanks for confirming that it should work err'body.

ee: :cripes: Oh god it was because the value was being set to "", my numerous .nil? check's did nothing to prevent that, I am now using 'blank?'

We've all had that happen. Plus side is that it's a learning experience!

The Journey Fraternity
Nov 25, 2003



I found this on the ground!

Physical posted:

I still don't get what went wrong with
Ruby code:
:criteria => 'NULL' 
but maybe I just couldn't find it sifting through my console output for webrick since there is alot.

code:
irb(main):001:0> 'NULL'.to_i
=> 0

The Journey Fraternity
Nov 25, 2003



I found this on the ground!
Not sure how homebrew ruby adds executables from gems- have you considered rbenv?

The Journey Fraternity
Nov 25, 2003



I found this on the ground!

Lexicon posted:

No kidding. Pardot - do yourself a favour and put Rails aside until you're comfortable with Ruby and universal programming constructs such as looping, objects, etc. I'm an experienced software engineer and I find Rails to be a big beast to handle at times.

I want to quote this to keep it around. :allears:

The Journey Fraternity
Nov 25, 2003



I found this on the ground!

Bob Morales posted:

Is there a way around adding a gem to your project and then having to run bundle install again? Just add the one new gem without using bundler?

I was at Starbucks earlier and was actually motivated but the wifi sucks and it was too slow or timing out and I kept getting errors like:
code:
Could not reach [url]https://rubygems.org/[/url]
code:
Gem::RemoteFetcher::FetchError: too many connection resets ([url]https://rubygems.org/gems/rdoc-3.12.gem[/url])
An error occurred while installing rdoc (3.12), and Bundler cannot continue.
Make sure that `gem install rdoc -v '3.12'` succeeds before bundling.
Of course when I get home and have my cable connection it updates in 30 seconds. Just a pain in the butt because now I have to get pizza and watch the Super Bowl.

If you already have the gem installed, you should be able to just require it somewhere early on in your app.

The Journey Fraternity
Nov 25, 2003



I found this on the ground!

Must be Monday. :allears:

The Journey Fraternity
Nov 25, 2003



I found this on the ground!
http://rapgenius.com/James-somers-herokus-ugly-secret-lyrics

Well, this certainly explains all the random timeouts we've been seeing for so long.

The Journey Fraternity
Nov 25, 2003



I found this on the ground!
*edit* I'm a moron

The Journey Fraternity
Nov 25, 2003



I found this on the ground!

mmachine posted:

Having an issue with a form_for on a routes resource and Rails' automatic pluralization. I have a definition in my routes.rb that goes like this:

pre:
resources :magic_fives
...and then a form for that goes like:

pre:
form_for @magic_five ...etc
This works just fine on the create / new form, but I've discovered that the route it's expecting on an edit for the plural of 'magic_fives' is actually 'magic_fife', according to rake:routes. So, I'm interested in the best method of either getting my form_for to generate the proper route -- magic_fife, but I don't think a form_for will generate routes dynamically like that... -- OR define that route resource so it builds the expected routes to be what I thought they would be -- magic_five(s).

I've researched this a bit already, and I see it's a consistent bug in Rails 3 regarding route :resource definitions automagically being pluralized. What I'm curious about is the most efficient way of addressing it. Has anyone had to deal with this before?

Try tossing this in an initializer:

code:
ActiveSupport::Inflector.inflections do |inflect|
  inflect.irregular 'magic_five', 'magic_fives'
end

The Journey Fraternity
Nov 25, 2003



I found this on the ground!

plasticbugs posted:

I know this is a really stupid question, but what the heck.

I'm working with push notifications in my iOS app. Each notification needs its own unique identifier. The notifications are very ephemeral. They're created and pushed to a remote server (Urban Airship) from my Rails server in one step, so I don't need to store a notification object on my Rails server. I just need to assign the notifications a unique ID when they're created just before they're pushed.

My thought was to just create a Notification model and give it no attributes. Then, every time the Rails app creates a new notification to push, it will also create and save a new Notification instance, which will auto-increment its id and assign the object's id to the notification's unique identifier (UA calls these aliases).

This sounds like a dumb way to create a counter. If my app is successful, it's going to fill my database with tens of thousands of empty records over the lifetime of the app.

Is there a cleaner, more pragmatic way to create a counter like this that also avoids race conditions?

https://rubygems.org/gems/uuid

The Journey Fraternity
Nov 25, 2003



I found this on the ground!
It doesn't help that there are multiple definitions. Most of the Ruby core/standard library treats it as an indicator that the method will not return anything and will modify its calling object directly. Rails tends to use it to designate that failure should raise an exception (at least in ActiveModel's viewpoint).

The Journey Fraternity
Nov 25, 2003



I found this on the ground!

raej posted:

Maybe I need to step back and get an idea of the Ruby way to accomplish the task I want to accomplish.

Users have many beers through cellared_beers (A person can cellar many different kinds of beers)
Beers have many users through cellared_beers (A particular beer can be cellared by many different users)
Cellared_beers has extra attributes that describe the physical bottle in a user's cellar (year, size)

A Beer page displays info on a particular beer (Brewery, description, ABV, etc).
If a person is logged in, there should be a button to add the beer to the current_user's cellar.
This will ask for year and size, as well as quantity.
If a cellared_beer matches for the current_user for that beer with the same year and size, add quantity to that cellared_beer's existing quantity (Bought a 6 pack of 2013 12oz bottles, then bought another 6-pack of 2013 12oz bottles)
If cellared_beer does not match on all three attributes, create cellared_beer with that quantity.

I think I am trying to over complicate this.

What they're trying to say is that cellar! is not a particularly good method for a User to have. If it were me, I'd be doing something like this:

Ruby code:
class CellaredBeer < ActiveRecord::Base
  def self.add_to_cellar(user_id, beer_id, size, quantity)
    cb = where(user_id: user_id, beer_id: beer_id, size: size).first_or_initialize(quantity: 0)
    cb.quantity += quantity
    cb.save!
    cb
  end 
end
Then call that in your controller method. Or a service object, whatever floats your boat.

This is completely off-the-cuff and I may have had a few beers.

The Journey Fraternity fucked around with this message at 05:04 on Jun 13, 2013

The Journey Fraternity
Nov 25, 2003



I found this on the ground!

raej posted:

I have a form for cellaring beers right now which looks like this:
code:
%= form_for(@cellared_beer) do |f| %>

      <%= f.label @beer.name %>
      <%= f.hidden_field :beer_id, value: @beer.id, :readonly => true %>

      <%= f.label :year %>
      <%= select_year(Date.today, :start_year => Time.now.year, :end_year => Time.now.year - 50, :field_name => :year, :prefix => :cellared_beer) %>

      <%= f.label :size %> 
      <%= f.select :size, options_for_select([["12oz","12oz"],["22oz","22oz"],["750mL","750mL"],["Other","Other"]]) %>

      <%= f.label :qty %>
      <%= f.text_field :qty, :required => true, :value => "1" %>

      <%= f.submit "Cellar", class: "btn btn-warning" %>
    <% end %>
My controller looks like this:
code:
@cellared_beer = CellaredBeer.add_to_cellar(current_user.id, @beer.id, params[:year], params[:size], params[:date])
When I'm in console, I can run the command
CellaredBeer.add_to_cellar(1, 1, 2009, "12oz", 6)
And I get the expected result of adding an entry for user 1, of cellaring beer 1 with year 2009, 12oz size, and 6 of them. However, in my view, when I load the page, I get a "nil can't be coerced into Fixnum" error.

Looking at the local variables, I see that indeed, year, size, and quantity are all nil. What do I need to do to get the form to not try to add_to_cellar until the user clicks submit?

Your params bit is wrong. :year, :size, and :date are attributes on @cellared_beer, so you'd want something like this:

code:
@cellared_beer = CellaredBeer.add_to_cellar(current_user.id, @beer.id, params[:cellared_beer][:year], params[:cellared_beer][:size], params[:cellared_beer][:date])
As an aside, `render text: params.inspect` is a lifesaver in these sorts of situations.

(or appropriate debugging gems but hell I'm still in printf mode)

The Journey Fraternity
Nov 25, 2003



I found this on the ground!

Smol posted:

I'm using https://github.com/rails-api/active_model_serializers

Works well, but then again, I don't need any fancy features for this project. YMMV.

This is what I've preferred for simple ones as well, but I've found it falls over when you want more than one way of looking at the same model. I also didn't throw more than a few hours at trying to get it to work though, so :shrug:

The Journey Fraternity
Nov 25, 2003



I found this on the ground!

Kallikrates posted:

unless you run the auto rehash plugin theres going to be a couple
code:
rbenv rehash
s in there also

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

(happy rbenv user here, screw rvm)

The Journey Fraternity
Nov 25, 2003



I found this on the ground!

androo posted:

Newbie question: I had trouble getting into RoR a couple years back after casually chatting with some web developers at the company I worked at. I found the path to "Hello, World" a bit too abstract and gave up shortly after.

Fast foward to yesterday and I had a neat idea for a web app, and spent a good part of the night looking into Zend Framework 2 (seriously wtf) and Code Igniter before I remembered RoR. I've been doing some Java game dev in my spare time recently, and I've picked up a lot about OOP and MVC, so it makes more sense now.

But there's only one problem: my site on Lithium Hosting (goon discounts :D) doesn't support anything higher than rails 2.3.18. Here's the environment:

Ruby version 1.8.7 (x86_64-linux)
RubyGems version 1.8.25
Rack version 1.1.6
Rails version 2.3.18
Active Record version 2.3.18
Active Resource version 2.3.18
Action Mailer version 2.3.18
Active Support version 2.3.18
Application root /home/blah/blah
Environment development
Database adapter sqlite3
Database schema version 0


A lot of the more recent modules for, let's say security, that I've been seeing in this thread and around the net need at least Rails 3 for the most recent versions, which fix critical security flaws and the like. Can I get away with using an older version of Rails or should I find another way to start developing? Some of the main feature additions to Rails 3-4 sound neat, albeit mostly beyond me.

It actually reminds me of another issue I had, that this community moves so drat fast recommended modules and programming concepts seem out-dated in many of the undated articles I read. It's another one of those things that raise the barrier of entry. Are there any updates guides or wikis on current best practices / gems?

You should just use Heroku if you're getting started (http://heroku.com) for hosting. It'll take whatever Rails version you throw at it, and you get a dyno and small database instance for free.

The Journey Fraternity
Nov 25, 2003



I found this on the ground!

Newbsylberry posted:

MailChimp looks like a good way to go! Thanks for the help.

On to the next issue, I am using devise and assigning a role to the user immediately when they sign up. I want to have a select_tag that gives the two role options 'student' and 'professional' since those are the only two roles I want people to be able to give themselves. Unfortunately devise tells me:


NoMethodError in Devise::Registrations#new

undefined method `select_tag' for #<ActionView::Helpers::FormBuilder:0x007fbbd415e608>

Here's the part of the code from the viewer that is giving me trouble:

code:
  <div><%= f.label :role %><br />
    <%= f.select_tag(:role, options_for_select('professional', 'student'))  %>
  </div>
  <br />

select_tag is its own standalone helper method- it isn't called on a form object. Drop the 'f.'

The Journey Fraternity
Nov 25, 2003



I found this on the ground!

KoRMaK posted:

In Ruby, I have a ASCII-8bit stream that contains a xls workbook. I want to open the workbook via the Workbook gem, but it requires a file. Is there a way I can read the stream into a file object without actually writing it to disk? This link seems close to what I am trying http://stackoverflow.com/questions/5931263/memory-streams-in-ruby

e: I tried it and it almost works. The Workbook gem tries to do a rewind on it, and I get an "Illegal Seek" error.

https://github.com/zdavatz/spreadsheet/blob/master/lib/spreadsheet/excel/reader.rb#L1198

You could try wrapping it in a delegator that declares rewind as a noop.

The Journey Fraternity
Nov 25, 2003



I found this on the ground!

prom candy posted:

The problem with Heroku is that the minimum monthly cost to get rid of the really long application spin-up time is like $36. The free tier is fine for testing stuff out but if your app is low traffic a person's initial request can take like 10s which the app spins up.

You can get around that by adding the free tier of New Relic. It can be configured to use a heartbeat URL to determine if the server is still up. Since it pings the server more frequently than the dyno sleep time, it'll never sleep.

The Journey Fraternity
Nov 25, 2003



I found this on the ground!

prom candy posted:

That's not really sustainable though, I think Heroku's free tier only works if people aren't artificially keeping their apps alive.

They offer you a hair over 31 full days worth (750) of dyno hours for free. If they didn't want you using it they wouldn't offer it.

The Journey Fraternity
Nov 25, 2003



I found this on the ground!

Obsurveyor posted:

That's not how businesses able to offer a free tier work. You will see the free tier quickly be replaced or the hours reduced if every free account starts using every single dyno hour of a month.

Every single application that uses multiple dynos gets one of those for free all month. I've done this for multiple work projects that I want to stand up easily but do not require more than one process, like Hubot. I have yet to hear a peep. Salesforce has more than enough throwaway cash to earn that small bit of goodwill for one of the few products of theirs that people like.

*edit* And this is the Ruby thread, not a Heroku one, so I'm going to stop this derail.

The Journey Fraternity fucked around with this message at 18:32 on Dec 15, 2013

The Journey Fraternity
Nov 25, 2003



I found this on the ground!

Obsurveyor posted:

I prefer sprinkle. Puppet and Chef are horrible nightmares to setup imho because they're designed for huge enterprise deployment, requiring a server just for managing recipes and such.

I've been using Ansible for exactly this reason.

The Journey Fraternity
Nov 25, 2003



I found this on the ground!

Newbsylberry posted:

I am trying to use radio buttons and can't get them to work:

This works locally but when I deploy the checkboxes don't even show up on the site:

code:
<%= f.collection_radio_buttons :drink_type_id, DrinkType.all, :id, :name %><br>
These don't work locally, or when I deploy (neither does radio_button_tag):

code:
<% @drink_types.each do |drink_type| %>
   <%=  f.radio_button :drink_type_id, drink_type.id %><%= f.label :drink_type_id, drink_type.name %>
<% end %>
Any ideas?

Edit - Alright, even though the checkboxes aren't showing up I can still select the drink_type, so I think it's the style sheets, I'm pretty sure I can figure this out.

You should check out app/assets/stylesheets/myFridge.css:119-121

The Journey Fraternity
Nov 25, 2003



I found this on the ground!

Peristalsis posted:

I have a download action in a controller that's giving me some trouble.

The basic structure is this:

code:
if (user has selected some files on the page)
  construct a single zip file out of selected files
  send_file zipfile.path, :type => 'application/zip', :disposition => 'attachment', :filename => file_name
  zipfile.delete
else
  redirect_to calling page, with an alert that no files were selected
end
It works as is, and when the user specifies files, they get a file selection dialog, the zip file downloads from that, and the user drops back to the page with the list of files on it (apparently without refreshing).

My problem is that, in some cases after a successful download, I want to refresh that calling screen to put a list of certain files at the bottom. I tried putting a redirect_to in the first if-block, but got an error that I can only use one render or redirect per action. I figured Rails just wasn't smart enough to realize that only one of them could be called, so I tried to move the existing redirect after the if-else-end block and fiddle with the parameters, but that also gave me the same multiple render/redirect error.

So, I guess there must be an implicit render involved in the block with the send_to, but I don't know how to find, trace, or edit that. It doesn't seem to be using any actual view code, and rake routes shows me the route for getting into this download action, but I don't see anything that leads out of it to a view.

I'm sure I'm missing something simple, but I'm not sure where to go from here.

send_file is a 'renderer' here.

The Journey Fraternity
Nov 25, 2003



I found this on the ground!

The Milkman posted:

Welcome to the industry.

I can't wait for a year from now when people want Angular devs with 4+ years of experience.

The Journey Fraternity
Nov 25, 2003



I found this on the ground!

Oh My Science posted:

https://github.com/elabs/pundit is the new authorization kid on the block if you want to give it a shot.

This actually looks pretty fantastic- I might try to use it in some of my stuff at work.

The Journey Fraternity
Nov 25, 2003



I found this on the ground!
He's on Ubuntu, but that doesn't mean he isn't terribly terribly wrong about his choice of version management. :v:

Adbot
ADBOT LOVES YOU

The Journey Fraternity
Nov 25, 2003



I found this on the ground!

Ravendas posted:

No, it doesn't exit, it waits for user input. An example simple program would be a series of couts in a console like so:
"Welcome to Rav's City Generator!"
"What is the population of this settlement?"
">"

Then it waits for a cin from the console, an inputted number.

I thought the IO bit was supposed to print out all the couts, then take in the cins for the program.

The programs are all just a series of cin/couts like this. Can ruby do this kind of thing (on windows)?

Edit: Tried it out on programs that don't require input, they just open and spit out a ton of info. Those 'connect' and spit out the proper info. It seems that if it requires any kind of input, it hangs, which makes this whole thing impossible unless I know a better way to do it.

IO.popen also takes a block with a single argument, which links to stdin/stdout on in the child process. If your program is waiting on input, write whatever is necessary, then read the results.

code:

IO.popen("my.exe") do |p|
  p.write "hello"
  output = p.read
  # do something with output here
end

This is bar coding and I don't have access to a windows machine anyway, but it seems like this is something you'd want.

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