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
Bob Morales
Aug 18, 2006


Just wear the fucking mask, Bob

I don't care how many people I probably infected with COVID-19 while refusing to wear a mask, my comfort is far more important than the health and safety of everyone around me!

Plastic Jesus posted:

You really, really, really don't want to execute shell commands from a script that accepts user-supplied data. I also doubt that you'd want to be able up/down a network interface or change its IP address via a web application.

It would be for a couple internal people to use, instead of ssh'ing into the server and running commands. It'd actually be for 'clone database', 'sync database' and 'dump database'

Adbot
ADBOT LOVES YOU

5436
Jul 11, 2003

by astral
What are some good books for learning rails? I have basic programming experience (Java,C++), and I already know some ruby.

How popular is ruby in the job market?

NotShadowStar
Sep 20, 2000

5436 posted:

What are some good books for learning rails? I have basic programming experience (Java,C++), and I already know some ruby.

How popular is ruby in the job market?

If you already have core concepts of Ruby down then Rails will go much easier than most. This is the book for you.

Ruby is still somewhat niche but like other niche things you can find a sweet gig that pays great and you'll love being there because you're with like-minded people that appreciate beauty and simplicity. Either that or some hot startup like thing that uses Rails because Rails is/was the new hotness and the startup crashes and burns after a few years because it's inept; but hey as long as the paychecks cash.

Doc Hawkins
Jun 15, 2010

Dashing? But I'm not even moving!


I don't know about everywhere else, but here in SF, Rails seems like the hottest poo poo ever shat.

("poo poo" not intended as a pejorative)

5436
Jul 11, 2003

by astral
I don't think I have the core concepts of ruby down. I've written some basic ruby (cucumber poo poo) but I have no idea how to start from scratch. I never majored in comp sci so I don't have a high level understanding of CS either.

Doc Hawkins
Jun 15, 2010

Dashing? But I'm not even moving!


I found Pragmatic's Programming Ruby 1.9 to be very good at teaching the language from end-to-end, including gem distribution, project structure (the book taught me /bin and /lib :downs:), which a lot of language books seem to assume the reader to already know.

A CS degree is like an Architecture degree: you don't technically need one in order to work on houses....no, wait, that's not right. Okay, being a computer scientist is like being an architect, and being a developer is like being a building contractor: there's a fair amount of overlap between the jobs, and one is more abstract than the other, but they aren't exactly the same thing, and building houses is a business you can join without any specialized schooling at all, as long as you can self-teach and spend plenty of time practicing the craft (hopefully because you enjoy it).

Anyway, you can always just read Structure and Interpretation of Computer Programs off MIT's OpenCourseWare site and know more CS-stuff than 90% of developers.

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

5436 posted:

I don't think I have the core concepts of ruby down. I've written some basic ruby (cucumber poo poo) but I have no idea how to start from scratch. I never majored in comp sci so I don't have a high level understanding of CS either.

Do the Ruby Koans.

bacon!
Dec 10, 2003

The fierce urgency of now
My favorite book for pure ruby is The Ruby Way. Everyone here loves it as well.

MrTheDevious
May 7, 2006

Ahh nostalgia, you cruel bitch
What about resources for people who are fine with Ruby but need the Rails stuff? Just got handed an exploratory project using RoR and I am NOT a web programmer - I understand why MVC is the way it is, but that's about as far as I've gotten. I already have my project done in plain Ruby but now have to integrate it into Rails to merge with the rest of the code base.

All I need is a web form that accepts a single field, pipes that input into a Ruby function, then displays results. I could do it in PHP in 5 minutes but this Rails stuff seems totally overboard and mysterious :(

skidooer
Aug 6, 2001

GodIsInTheTrees posted:

I could do it in PHP in 5 minutes but this Rails stuff seems totally overboard and mysterious :(
Rails kind of is overboard for something that simple. What's the justification for using Rails over something like, say, Sinatra or plain old Rack?

8ender
Sep 24, 2003

clown is watching you sleep

GodIsInTheTrees posted:

All I need is a web form that accepts a single field, pipes that input into a Ruby function, then displays results. I could do it in PHP in 5 minutes but this Rails stuff seems totally overboard and mysterious :(

I've always liked this tutorial:
http://guides.rubyonrails.org/getting_started.html

Once you go through this you'll see that making a form that accepts input is a 15 second task with Rails and scaffolding.

NotShadowStar
Sep 20, 2000

GodIsInTheTrees posted:

What about resources for people who are fine with Ruby but need the Rails stuff? Just got handed an exploratory project using RoR and I am NOT a web programmer - I understand why MVC is the way it is, but that's about as far as I've gotten. I already have my project done in plain Ruby but now have to integrate it into Rails to merge with the rest of the code base.

All I need is a web form that accepts a single field, pipes that input into a Ruby function, then displays results. I could do it in PHP in 5 minutes but this Rails stuff seems totally overboard and mysterious :(

You want Sinatra or Padrino.

<3 Padrino, it's an awesome middle ground between Sinatra and Rails.

MrTheDevious
May 7, 2006

Ahh nostalgia, you cruel bitch

8ender posted:

I've always liked this tutorial:
http://guides.rubyonrails.org/getting_started.html

Once you go through this you'll see that making a form that accepts input is a 15 second task with Rails and scaffolding.

Working through this now!

To everybody else: I've already figured out this is massive overkill, but I have no choice (yay work). This is an "exploratory" project to weigh pros and cons of RoR, so I absolutely HAVE to use Rails...

Thanks for all the help!

rugbert
Mar 26, 2003
yea, fuck you
So I keep getting this error on my heroku app:

code:
NoMethodError (undefined method `empty?' for nil:NilClass):
off this line in my controller:
code:
    @team = Bio.where(:bio_type => "Employee").reject{ |r| r.email.empty? }
but I dont get that error in my dev environment and I have almost identical conditions. All Im trying to do is reject any entries with empty email fields. What gives?

Soup in a Bag
Dec 4, 2009
There's String#empty?, but no NilClass#empty? method. If r.email is nil rather than an empty String you'll get that error.

rugbert
Mar 26, 2003
yea, fuck you

Soup in a Bag posted:

There's String#empty?, but no NilClass#empty? method. If r.email is nil rather than an empty String you'll get that error.

Ahh ok, well I changed it to .blank? and it seems to work fine. thanks!

OK, so Ive set up my mailer in Rails which works fine, but I wanted to make a new action (or maybe just a view?) to have a slimmed down contact form in a lightbox so you can email a specific user from their profile. I can do that all fine and dandy but it would use the default layout which I dont want. So I added:
code:
render :layout => 'lightbox'
to the action so that I could use a new layout. Unfortunately that seems to block off my access to the model as I get this error when the lightbox pops up
code:
undefined method `model_name' for NilClass:Class
 #on this line
<% form_for @contact, :url => {:action => "create"}, :html => {:method => :post} do |f| %>
So by using a different layout I cant use the resources I set up in my routes which is here:
code:
  resources :contacts, :only => [:new, :create], :as => :contacts
  #Im passing in a name to the email form
  match "contacts/direct/:name" => "contacts#direct", :as => :direct_email
I hope that made sense. But what do I do?

rugbert fucked around with this message at 11:41 on Jun 29, 2011

Cock Democracy
Jan 1, 2003

Now that is the finest piece of chilean sea bass I have ever smelled
I'm not sure what you're trying to do, but lightbox is a JS tool for fancy image viewing. Why are you using it to show a form? I don't get it.

render :layout => 'something' is to change the layout you are using for that action, which may not be what you want to do. Read up on it here.

And the :as => :contacts in your routes is unnecessary. It would be named that anyway.

smug forum asshole
Jan 15, 2005
I'm a total idiot when it comes to testing and tdd/bdd. I would really appreciate if anyone could give some guidelines/opinions on the right way to do things, frameworks to use (or to avoid) -- really just throw anything at me and I'll be grateful.

I'm working on something now and I want to add tests to it, but in the future I'd like to get into the habit of writing tests for code right alongside of the code I'm writing. I just can't seem to get there, mentally.

PS - I'm thinking about using Capybara and RSpec, mainly on the strength of this post.

smug forum asshole fucked around with this message at 17:19 on Jun 30, 2011

rugbert
Mar 26, 2003
yea, fuck you

Cock Democracy posted:

I'm not sure what you're trying to do, but lightbox is a JS tool for fancy image viewing. Why are you using it to show a form? I don't get it.

render :layout => 'something' is to change the layout you are using for that action, which may not be what you want to do. Read up on it here.

And the :as => :contacts in your routes is unnecessary. It would be named that anyway.

Well, Im using colorbox actually (which also displays webpages), I just thought it would be cool way to send an email. you know, like you click "send so and so a message" and a lightbox with a small form pops up. I need to use a different layout tho, so that way ONLY the form is shown. If I use the default layout then Ill have all this other shown like the navbar, footer, header ect ect

Slow News Day
Jul 4, 2007

Sorry if this is not a Rails question specifically, but I didn't know where else to post it.

I'm new to Ruby. I wrote this simple one-line script in an analyzer.rb file:

code:
File.open("text.txt").each { |line| puts line }
But when I run it, it gives me the following error:

>ruby analyzer.rb
analyzer.rb:1:in `initialize': No such file or directory - text.txt (Errno::ENOENT)
from analyzer.rb:1:in `open'
from analyzer.rb:1:in `<main>'
>Exit code: 1


The thing I don't understand is that both the analyzer.rb and text.txt files are located in C:\Ruby\bin folder, which is where ruby.exe is located. Considering this, why am I getting this error?

Slow News Day fucked around with this message at 05:17 on Jul 4, 2011

skidooer
Aug 6, 2001

enraged_camel posted:

Considering this, why am I getting this error?
The search path will be your current working directory, not the location of the script. If test.txt will always be located along side the script, you can do something like this:
code:
path = File.join(File.dirname(__FILE__), 'test.txt')
File.open(path) { |file| file.each { |line| puts line } }

Slow News Day
Jul 4, 2007

skidooer posted:

The search path will be your current working directory, not the location of the script. If test.txt will always be located along side the script, you can do something like this:
code:
path = File.join(File.dirname(__FILE__), 'test.txt')
File.open(path) { |file| file.each { |line| puts line } }

Thank you, I'll try that. Dumb question: how do I find out what my current working directory is? I mean, all I'm doing is pressing F5 within SciTE.

edit: that didn't work either. I get a very similar No such file or directory - ./text.txt (Errno::ENOENT) error.

edit2: I figured it out by discovering Dir.pwd, which showed me the working directory. It was C:\Ruby192. Holy hell.

vvvvvv yeah :)

Slow News Day fucked around with this message at 06:59 on Jul 4, 2011

xtal
Jan 9, 2011

by Fluffdaddy
I think you can get the current working directory with Dir.pwd.

xtal fucked around with this message at 05:52 on Mar 30, 2012

Oh My Science
Dec 29, 2008
I'm starting to learn about polymorphic associations, and have run into a road block of sorts. So far I have followed and understood the basic concepts presented by ryan baits in episode #154, and have even created the same basic app. I can create new comments, and destroy, however I cannot figure out how to edit them.

I understand that I should be using edit_polymorphic_path or edit_polymorphic_url, and am even 99% sure I am passing in the right models. However when I click the link I get a hash error, which may suggest something is wrong with my controller.

so... does anyone have a working example or know of one that shows a complete polymorphic association in action? My final goal is to create a polymorphic image model with carrierwave, but cannot find many working examples.

Or should I avoid using polymorphic associations all together?

smug forum asshole
Jan 15, 2005
So as per my last post I started using RSpec. I'm not very good, but I think I'll get better with time. Where I'm dumbest right now is testing authenticated users (Devise, Omniauth). I couldn't figure out how to properly mock an authenticated User that takes advantage of Devise's current_user helper method, but luckily I found a post where someone had the same problem as me.

I ended up doing something like this, where the warden environment variable gets mocked up. See comment:

code:
describe ItemsController, " handle POST /items for current user" do
  before(:each) do
    @user = User.make # I'm using Machinist here to make test objects
    sign_in @user
    # I hate that I'm mocking the authentication here. Any suggestions on a different approach?
    request.env['warden'] = mock(Warden, :authenticate => @user, :authenticate! => @user)
    controller.stub!(:current_user).and_return(@user)
  end

  it 'should display current user\'s items' do
    post :create, :item => {:title => 'Who said what?'}
    response.should redirect_to(items_path)
  end
end

smug forum asshole
Jan 15, 2005
Here's a terrible association-related question for the thread I appear to have killed:

I have Groups, Users, and Posts as models.

I want a User to be able to create a Group.
If a Group with a given name already exists, I want the User to be able to associate with that Group.

Once a User has associated with a Group, they should be able to create Posts that are associated with that Group.

A User should be able to see all Posts associated with a Group, and all Users should be able to see other Users

I'm thinking:

code:
class Group < ActiveRecord::Base
  has_and_belongs_to_many :users
  has_many :posts
end

class Post < ActiveRecord::Base
  belongs_to :user
  belongs_to :group
end

class User < ActiveRecord::Base
  has_and_belongs_to_many :groups
  has_many :posts
end

NotShadowStar
Sep 20, 2000
HABTM is generally used with legacy schemas because you can't manipulate the intermediate join, which you don't think you will at first but you will. When I started with Rails so many years ago I always did HABTM because it was easier to understand and every time I regretted it and switched to has_many :through.

http://guides.rubyonrails.org/association_basics.html#choosing-between-has_many-through-and-has_and_belongs_to_many

smug forum asshole
Jan 15, 2005

NotShadowStar posted:

HABTM is generally used with legacy schemas because you can't manipulate the intermediate join, which you don't think you will at first but you will. When I started with Rails so many years ago I always did HABTM because it was easier to understand and every time I regretted it and switched to has_many :through.

I'd really like to do has_many :through, but I can't grasp how to make it sense with this problem.

NotShadowStar
Sep 20, 2000
You already have it :D, though you should probably use Group as the central model instead of Post

code:
class Group < ActiveRecord::Base
  has_many :users, through: :post
  has_many :posts
end

class Post < ActiveRecord::Base
  belongs_to :user
  belongs_to :group
end

class User < ActiveRecord::Base
  has_many :groups, through: :post
  has_many :posts
end

smug forum asshole
Jan 15, 2005
Is it possible for a User to create a Group without creating a Post to intermediate the relationship that way?

Sorry I'm having trouble grasping this :ohdear:

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

NotShadowStar posted:

You already have it :D, though you should probably use Group as the central model instead of Post


The Post model isn't the Group membership:


code:
class Group < ActiveRecord::Base
  has_many :users, through: :group_memberships
  has_many :posts
end

class GroupMembership < ActiveRecord::Base
  belongs_to :user
  belongs_to :group
end

class Post < ActiveRecord::Base
  belongs_to :user
  belongs_to :group
end

class User < ActiveRecord::Base
  has_many :groups, through: :group_memberships
  has_many :posts
end

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.

BonzoESC posted:

The Post model isn't the Group membership:


code:
class Group < ActiveRecord::Base
  has_many :users, through: :group_memberships
  has_many :posts
end

class GroupMembership < ActiveRecord::Base
  belongs_to :user
  belongs_to :group
end

class Post < ActiveRecord::Base
  belongs_to :user
  belongs_to :group
end

class User < ActiveRecord::Base
  has_many :groups, through: :group_memberships
  has_many :posts
end


I think he wants this
code:
class Group < ActiveRecord::Base
  belongs_to :user # a user owns a group when he starts it
  has_many :posts
  has_many :members, :through => :group_memberships
end

class GroupMembership < ActiveRecord::Base
  belongs_to :user
  belongs_to :group
end

class Post < ActiveRecord::Base
  belongs_to :user
  belongs_to :group
end

class User < ActiveRecord::Base
  has_many :group_memberships # groups the user belongs to
  has_many :groups # groups the user started
  has_many :posts
end
The belongs_to macro gets tricky with users and groups because you immediately think "Oh hey, users belong to groups because they're members of groups," which is not what belongs_to means.

Oh My Science
Dec 29, 2008
I'm running into an issue implementing a simple contact form using this http://www.railsmine.net/2010/03/rails-3-action-mailer-example.html example.

Basically I changed it from support to contact, and although I believe the code is all correct, it will not render the form partial for the contact controller.

What could be causing this?

enki42
Jun 11, 2001
#ATMLIVESMATTER

Put this Nazi-lover on ignore immediately!
If you're not getting any errors but the partial just doesn't seem to be showing up, the obvious typo to check for is using <% render instead of <%= render.

Oh My Science
Dec 29, 2008
No that is the first thing I look for now, and I noticed the typo in the tutorial. I have however solved my own problem, it only required 6 hours of sleep to track it down.

Thanks for the suggestion.

ZanderZ
Apr 7, 2011

by T. Mascis

enki42 posted:

div city

Everything I make is div city. I've tried doing it properly. I've done tables before, but for some reason my clients love the fact that I use divs.

enki42
Jun 11, 2001
#ATMLIVESMATTER

Put this Nazi-lover on ignore immediately!

ZanderZ posted:

Everything I make is div city. I've tried doing it properly. I've done tables before, but for some reason my clients love the fact that I use divs.

Wow, that has to be the king of blast-to-the-past quoting. I had to use forums search to find where my original post came from, and it's from 3 years before I was actually a Ruby dev :)

For the record, I still like the idea of Haml, but it still puts divs up on an undeserved pedestal. A div tag should be the tag of last resort, not the default. Whenever you use a div, you're basically giving up and saying 'welp, i can't assign any semantic meaning to this thing at all.' Haml really ought to require specifying tags. Sass is the bomb though, particularly in SCSS syntax.

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.
I've run into something in Rails 3 that I don't understand. Assume I have the following models:

code:
class User < ActiveRecord::Base
  has_many :email_providers
end

class EmailProviders < ActiveRecord::Base
  belongs_to :user
end
Then if I want to create a new user in the user controller this will not work:
code:
@user = User.new
@user.email_providers.push EmailProvider.create do |prov|
   prov.email_address = params[:user][:email_address]
end

@user.save
@user.email_providers.last.email_address will be nil. The following, however, works:

code:
@user = User.new
eprov = EmailProvider.create do |prov|
  prov.email_address = params[:user][:email_address]
end
@user.email_providers.push eprov
@user.save
With that @user.email_providers.last.email_address is correct. Is this expected behavior?

skidooer
Aug 6, 2001

Plastic Jesus posted:

Then if I want to create a new user in the user controller this will not work:
If we add brackets, your code is equivalent to this:

code:
@user.email_providers.push(EmailProvider.create) do |prov|
   prov.email_address = params[:user][:email_address]
end
As a result, you are not passing any attributes to your EmailProvider model.

Personally, given your input, I'd be inclined to do something like this:
code:
class User < ActiveRecord::Base
  has_many :email_providers
  
  def email_address=(email_address)
    email_providers.build(:email_address => email_address)
  end
end

skidooer fucked around with this message at 23:57 on Jul 12, 2011

Adbot
ADBOT LOVES YOU

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.

skidooer posted:

If we add brackets, your code is equivalent to this:

code:
@user.email_providers.push(EmailProvider.create) do |prov|
   prov.email_address = params[:user][:email_address]
end

Do'h, thanks. I assumed that it was something syntactical I was missing from staring at the same code for too long. Thanks also for the tip of adding a setter method, which may be useful for more than 1 reason.

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