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
prom candy
Dec 16, 2005

Only I may dance
Check out that Rails for Zombies site I linked, it's supposed to be really good for beginners. Bump this thread if you have any other questions, I'll keep an eye on it.

Adbot
ADBOT LOVES YOU

manero
Jan 30, 2006

someone posted:

code:
# app/models/person.rb
class Person < ActiveRecord::Base
  set_table_name "dudes"
  
  def full_name
    return self.first_name + " " + self.last_name
  end
end

Dudes: Stop writing "self"! (Also "return")

You should be writing code like this:

code:
def full_name
  first_name + " " + last_name
end
You really only need to use "self.foo = " when you're setting an attribute, to make sure you end up calling the foo=(new_foo) method, instead of setting a local variable.

prom candy
Dec 16, 2005

Only I may dance
I know, I mentioned that above my code block. I wrote the example that way because the Ruby style would be a little confusing for someone who is new to Ruby. Adding the self and return hopefully made it clearer in the example but I would never write real code like that.

Big Nubbins
Jun 1, 2004
For those of you that are very familiar with Inherited Resources, I'm having a problem modeling the following association chain in my controllers:

Say I have Users which belong to many Leagues and each user has a single Team for a particular league. The league is identified by a slug as the subdomain, and since there is only a single team for a particular user and league, I don't need to identify that either. So instead of a URL that looks like http://example.com/leagues/1/teams/1, I get http://myleague.example.com/team, which looks a hell of a lot better to me. The problem I ran into with Inherited Resources is describing the relationship between a league (identified by the slug in the subdomain), the logged in user, and a team singleton using its idioms for describing the associations without having to resort to hacky poo poo like before_filter hooks and injecting ID attributes in the params hash. It really seems that something like this would work:
code:
class League::TeamController
    before_filter :authenticate_user!

    defaults :singleton => true
    belongs_to :league, :finder => :find_by_slug, :param => request.subdomain

    def begin_of_association_chain
        current_user
    end

epswing
Nov 4, 2003

Soiled Meat
Hi!

I've had a bitnami redmine 1.0.2 stack running for about a year. It came with...

ruby 1.8.7
rails 2.3.9
rack 1.0.1

Now I want to bring redmine up to date, to version 1.3.0. http://www.redmine.org/projects/redmine/wiki/RedmineInstall tells me 1.3.x requires...

ruby 1.8.7
rails 2.3.14
rack 1.1.x

So I can't just replace the application files, I'd also need to update rails and rack. So it's probably in my interest to use a fresh bitnami redmine 1.3.0 stack because it will come with all the necessary dependencies, none of which I have any experience using and/or keeping up to date (which was the point of using a bitnami stack in the first place).

But if I just back up the database / conf files / uploaded files, uninstall the 1.0.2 stack, and install the 1.3.0 stack, the database schema will be different, so I won't likely be able to just use the old database.

How do I do this?

epswing fucked around with this message at 18:29 on Feb 2, 2012

Physical
Sep 26, 2007

by T. Finninho
code:
class Tweet < ActiveRecord::Base
  
end
What does the ::Base do?

Lamont Cranston
Sep 1, 2006

how do i shot foam

Physical posted:

code:
class Tweet < ActiveRecord::Base
  
end
What does the ::Base do?

Base is the class' name, ActiveRecord is the module or package, :: is the namespace operator.

Vivian Darkbloom
Jul 14, 2004


Here's a non-rails longshot: Anyone have experience with Shoes, the graphical app builder built on Ruby? I'm trying to build an app where the (very simple) UI changes modes a few times as the program runs; what's the most sensible way to program this?

e: To make this a little clearer, I'd like the program to first have a mode where it displays some instructions in a text box; then those disappear and other controls appear after the user does something; then the user does something else and some other controls appear. Is it even possible to delete graphics items in a Shoes app?

Vivian Darkbloom fucked around with this message at 09:31 on Feb 4, 2012

Soup in a Bag
Dec 4, 2009
It's been a while since I've played with Shoes, but everything has show/hide/toggle methods for visibility and a remove method. Slots also have a clear method to remove their contents. The manual covers all that and more.

Vivian Darkbloom
Jul 14, 2004


Soup in a Bag posted:

It's been a while since I've played with Shoes, but everything has show/hide/toggle methods for visibility and a remove method. Slots also have a clear method to remove their contents. The manual covers all that and more.

Oh, good. Guess I overlooked those options. I think what was really blowing my mind with Shoes is that it seems to run every Shoes command all at once, so you can't do this, for instance:

code:
Shoes.app do
	para "This app will self-destruct in 5 seconds"
	sleep 5
	exit
end

Vivian Darkbloom
Jul 14, 2004


Another Shoes question! When I first coded the following, I expected it to have two steps: first, the instructions display; then after hitting the spacebar the task displays. Instead, both display at once.

code:
Shoes.app :width => 800, :height => 500 do

	def present_instructions (instructions)
		text = stack {para instructions}
		keypress do |key|
			if key == " " then
				text.remove	
				break
			end
		end
	end

	def main_task
		stack do
			@textdisplay = para "Here's the main task. Enter your answers below."
			@freeresponse = edit_box :width => 400, :height => 200
		end
	end	

	present_instructions('Instructions go here. Press space to continue')
	main_task()	

end
What's going on here - is this just down to Shoes' unusual handling of its stacks and flows? What's the right way to implement this? It "works" if you put a call to main_task in present_instructions, instead of returning straightaway, but it sets off my personal BAD PROGRAMMING alarm and introduces other problems.

0xB16B00B5
Aug 24, 2006

by Y Kant Ozma Post
whats the best tutorials that don't focus on scaffolding? it seems like 90% of the ruby tutorials focus on running 3 commands and VOILA you got a blog. just while im getting started id like to get walked through ultimately what the scaffolding does, since it spits out tons of garbage files each time and i get lost in the weeds trying to reconstruct it all

Smol
Jun 1, 2011

Stat rosa pristina nomine, nomina nuda tenemus.
The official Rails guides are just fine for that. Just ignore the "Getting Started with Rails" (or skim through it) tutorial and concentrate on the ones that focus on models, controllers/routing and views.

enki42
Jun 11, 2001
#ATMLIVESMATTER

Put this Nazi-lover on ignore immediately!
I think Railstutorial.org starts with scaffolds, but quickly abandons them and starts over within a chapter. It's also an awesome tutorial, and there was just a new version released.

plasticbugs
Dec 13, 2006

Special Batman and Robin
I made the bad decision to begin my Rails app with Disqus comments. I figured it would be easy to export them when I outgrew the simple stop-gap solution it provided.

I know how it's organized, but I'm having trouble figuring out a simple way of parsing all the entries.

The XML file is basically set up like this:

pre:
<thread dsq:id="12345">
  <id>foo</id>
 </thread>
where "foo" is a unique entry in my Post model's "code" column (and appears in the Post URL, so that Disqus knows to display the right comments on the right post)

then, much further down the same XML file

pre:
<post id="177002220">
		<id/>
		<message><![CDATA[This is my awesome comment!]]></message>
		<createdAt>2011-04-02T19:02:04Z</createdAt>
		<author>
			<email>me@example.com</email>
			<name>myname</name>
			<isAnonymous/>
		</author>
		<ipAddress>192.168.1.1</ipAddress>
		<thread dsq:id="12345"/>
</post>
I basically need to parse through all the "threads", and for each unique thread ID, I need to search across all the "posts" in the XML to find all the entries that share that "thread id". Then, save out a Comment for the matching data.

This is what I have, but it just hangs. Am I even close?

pre:
require 'nokogiri'

@doc = Nokogiri::XML(File.open("~/Downloads/disqus.xml"))

@doc.css("thread").each do |t|
    @t = t
    @thread_id = t.attributes["id"].value
      @doc.css("post").each do |p|
        @p = p
        @post_id = p.attributes["id"].value
      end
  
    if @thread_id == @post_id
      c = Comment.new
      c.code = @t.css("id").text
      c.author = @p.css("author name").text
      c.contents = @p.css("message").text
      c.save!
    end
end

plasticbugs fucked around with this message at 06:08 on Feb 12, 2012

UxP
Aug 27, 2007

plasticbugs posted:

I basically need to parse through all the "threads", and for each unique thread ID, I need to search across all the "posts" in the XML to find all the entries that share that "thread id". Then, save out a Comment for the matching data.

There isn't much of a reason to separate out the threads and comments loops, just go for it all in one shot. I've never used css selectors for XML node traversal, so I'd honestly just go for basic XPath.

pre:
require 'nokogiri'

doc = Nokogiri::XML(File.open(File.join(Dir.home(), "Downloads", "disqus.xml")))

doc.xpath('/xmlns:disqus/xmlns:thread').each do |t|
  doc.xpath(%Q<//xmlns:post[xmlns:thread[@dsq:id="#{t.attr('id')}"]]>).each do |c|
    comment = Comment.new
    comment.code = t.attr('id')
    comment.author = c.xpath('xmlns:author/xmlns:name').inner_text()
    comment.message = c.xpath('xmlns:message').inner_text()
    comment.save!
  end
end
Edit: Your variable scope is all over the place. @doc doesn't need to be an instance variable, unless this Disqus importer is some kind of class object and casting p as an instance variable is not the way to go. The way you have the inner loop all sorts of confusing. I know what it is supposed to do, but there's no reason to be doing it that way. Keep It Simple (Stupid).

Long story short, @thread_id and @post_id in your script are never going to be the same.

UxP fucked around with this message at 18:07 on Feb 12, 2012

plasticbugs
Dec 13, 2006

Special Batman and Robin

UxP posted:

There isn't much of a reason to separate out the threads and comments loops, just go for it all in one shot. I've never used css selectors for XML node traversal, so I'd honestly just go for basic XPath.

pre:
require 'nokogiri'

doc = Nokogiri::XML(File.open(File.join(Dir.home(), "Downloads", "disqus.xml")))

doc.xpath('/xmlns:disqus/xmlns:thread').each do |t|
  doc.xpath(%Q<//xmlns:post[xmlns:thread[@dsq:id="#{t.attr('id')}"]]>).each do |c|
    comment = Comment.new
    comment.code = t.attr('id')
    comment.author = c.xpath('xmlns:author/xmlns:name').inner_text()
    comment.message = c.xpath('xmlns:message').inner_text()
    comment.save!
  end
end
Edit: Your variable scope is all over the place. @doc doesn't need to be an instance variable, unless this Disqus importer is some kind of class object and casting p as an instance variable is not the way to go. The way you have the inner loop all sorts of confusing. I know what it is supposed to do, but there's no reason to be doing it that way. Keep It Simple (Stupid).

Long story short, @thread_id and @post_id in your script are never going to be the same.

Thanks so much for your help! This worked. I get in over my head quickly and this is an instance where I should go back to basics for a little while and learn more Ruby and less Rails. I also need to look into xpath, as it seems to be more powerful than using just CSS to traverse the hierarchy.

EDIT: Can you or anyone recommend a good book or site to learn more about working with XML with Ruby or any other non C language?

Originally I had it scoped with locals, but when that didn't work, I tried it the other way with all instance variables thinking maybe the first block wasn't passing variables into the next block. Me code pretty one day.

plasticbugs fucked around with this message at 20:23 on Feb 12, 2012

Physical
Sep 26, 2007

by T. Finninho
I would really like to use Eclipse for RoR development. I found Apana's RadRails but no current tutorials. And the syntax highlighting doesn't work. This is really important to me, having proper syntax highlighting. Has anyone got some current or working tutorials of Eclipse and RoR or Apana's radrails? Here are some of the links I've used but they are all old and none of the screens match up and I can't even find where to put the path to the ruby interpreter.

http://www.ibm.com/developerworks/opensource/library/os-rubyeclipse/
http://napcs.com/howto/railsonwindows.html
http://oldwiki.rubyonrails.org/rails/pages/HowToUseEclipseForRailsDevelopment

Johnny Cache Hit
Oct 17, 2011
Hi Rails people! I'm going to be picking up RoR soon for my job. Does anyone have any recommended tutorials that are focused on someone who has plenty of experience under their belt?

I'll be moving from a mostly Python/Django environment, if that helps. So I'm comfortable with MVC, ORMs, etc.

Sorry if this has been asked before. I tried to look through the thread but it's kinda massive...

MrDoDo
Jun 27, 2004

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

Kim Jong III posted:

Hi Rails people! I'm going to be picking up RoR soon for my job. Does anyone have any recommended tutorials that are focused on someone who has plenty of experience under their belt?

I'll be moving from a mostly Python/Django environment, if that helps. So I'm comfortable with MVC, ORMs, etc.

Sorry if this has been asked before. I tried to look through the thread but it's kinda massive...

The Rails 3 Way seems to be the de facto encyclopedia on rails. Its not really a tutorial but will cover everything at a fairly in-depth level since you are already familiar with MVC etc.

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!

Kim Jong III posted:

Hi Rails people! I'm going to be picking up RoR soon for my job. Does anyone have any recommended tutorials that are focused on someone who has plenty of experience under their belt?

I'll be moving from a mostly Python/Django environment, if that helps. So I'm comfortable with MVC, ORMs, etc.

Sorry if this has been asked before. I tried to look through the thread but it's kinda massive...

http://ruby.railstutorial.org/ruby-on-rails-tutorial-book

Pragmatic has a good book but the latest version is a skinny shell of it's former self: http://pragprog.com/book/rails4/agile-web-development-with-rails

Pardot
Jul 25, 2001




Kim Jong III posted:

I'll be moving from a mostly Python/Django environment, if that helps. So I'm comfortable with MVC, ORMs,

The responsibilities of the M V and C in django do not line up with the M V and C in rails, so figure out the differences before you get super confused.

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

Pardot posted:

The responsibilities of the M V and C in django do not line up with the M V and C in rails, so figure out the differences before you get super confused.

Rails MVC:

M: all business logic
V: templates (erb and haml)
C: turning GET and POST parameters into calls into model methods, turning models into JSON/XML/template renders

Smol
Jun 1, 2011

Stat rosa pristina nomine, nomina nuda tenemus.
Is there a good way to make action caching worth with multiple formats? As far as I can tell, Rails doesn't seem to care about the Accept header when determining cache keys, so when I enable action caching, Rails will always return the response to the first format that was requested (all formats will have the same cache key).

There is a :cache_path parameter, but I'm not sure what would be the recommended way to use it in this case, or if it helps at all.

Vivian Darkbloom
Jul 14, 2004


What's your favorite GUI toolkit? Shoes is kind of a mess, despite how clever and Ruby-ish it can be, and I find it very hard to control. I don't care if it's ugly, so I'm leaning towards Ruby Tk because it looks relatively easy and well documented.

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

Vivian Darkbloom posted:

What's your favorite GUI toolkit? Shoes is kind of a mess, despite how clever and Ruby-ish it can be, and I find it very hard to control. I don't care if it's ugly, so I'm leaning towards Ruby Tk because it looks relatively easy and well documented.
Rails with bootstrap, and tell the client to htfu and accept that http is the future.

Vivian Darkbloom
Jul 14, 2004


BonzoESC posted:

Rails with bootstrap, and tell the client to htfu and accept that http is the future.

I'm just a grad student writing behavioral experiments, no clients except a mob of bored undergrads.

0xB16B00B5
Aug 24, 2006

by Y Kant Ozma Post

enki42 posted:

I think Railstutorial.org starts with scaffolds, but quickly abandons them and starts over within a chapter. It's also an awesome tutorial, and there was just a new version released.

awesome, i can't believe i dismissed this one before. finally a tutorial that i'm really clicking with. good stuff.

asveepay
Jul 7, 2005
internobody
so who's heading to Austin this april?

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.

Smol posted:

Is there a good way to make action caching worth with multiple formats? As far as I can tell, Rails doesn't seem to care about the Accept header when determining cache keys, so when I enable action caching, Rails will always return the response to the first format that was requested (all formats will have the same cache key).

There is a :cache_path parameter, but I'm not sure what would be the recommended way to use it in this case, or if it helps at all.

With Action Caching it should just work out of the box, e.g. cache JSON separately from HTML or XML. With Page Caching I have no idea what would happen, but you probably don't want page caching...

Oh My Science
Dec 29, 2008
I'm playing around with sorting a list of products, and need some help.

After following Ryan Bates HABTM tutorial I now have products that can belong to a number of categories. I now want to filter products by their category specified by the user.

Can someone give me an example or hints on how to do this?

prom candy
Dec 16, 2005

Only I may dance
It might be a good idea to use nested resources for that. Here's a screencast about it: http://railscasts.com/episodes/139-nested-resources

dustgun
Jun 20, 2004

And then the doorbell would ring and the next santa would come
Ok, I'm crying uncle: How do I get rid of pages and pages of
code:
Started GET "/assets/layout/footer.png" for 127.0.0.1 at 2012-02-22 21:20:59 -0500


Started GET "/assets/layout/header-bg.png" for 127.0.0.1 at 2012-02-22 21:20:59 -0500


Started GET "/assets/layout/footer_bg.png" for 127.0.0.1 at 2012-02-22 21:20:59 -0500


Started GET "/images/ticks/ajax-loader.gif" for 127.0.0.1 at 2012-02-22 21:20:59 -0500
per request in dev testing with rails s thin ?

dustgun fucked around with this message at 03:25 on Feb 23, 2012

Pardot
Jul 25, 2001




Why do you want to get rid of them? It's useful to have access logs. Logs are streams.

prom candy
Dec 16, 2005

Only I may dance
I think I read on Github or Stack Overflow that suppressing the asset log messages is an option that's coming soon.

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.

I'm ready to kill myself. Okay not really but my friend/frontend dev guy did a pull request today where he basically rewrote our entire landing page, started using compass / blueprint, and now I'm running into problems with the asset pipeline deploying to heroku. It's gotten to the point where I'm ready to scrap the whole thing and redo it in Sinatra. Also I'm a little hung over because I got day-drunk at lunch, but still. Can anyone point me to info on the stylesheet link tree? I think that's where the problem is, like it's not precompiling a few stylesheets because they aren't in the tree or something. Here's what my log looks like:

code:
 ActionView::Template::Error (screen.css isn't precompiled):
2012-02-23T01:54:24+00:00 app[web.1]:   Rendered pages/home.html.haml within layouts/application (3.4ms)
2012-02-23T01:54:24+00:00 app[web.1]:     6:     -# Compass and blueprint stylesheets
2012-02-23T01:54:24+00:00 app[web.1]:     8:     = stylesheet_link_tag 'print', :media => 'print'
2012-02-23T01:54:24+00:00 app[web.1]:   app/views/layouts/application.html.haml:7:in 
`_app_views_layouts_application_html_haml___2737759042931766110_26806720'
Any help is appreciated.

dustgun
Jun 20, 2004

And then the doorbell would ring and the next santa would come

Pardot posted:

Why do you want to get rid of them? It's useful to have access logs. Logs are streams.
I'm treating it like a stream right now and pipping it through grep, and I guess I just can't quite articulate why I don't need dozens of GET requests logged (streamed, whatever) of static, unchanging images per page to development.log beyond "it's dumb and worthless information". God know that file can kill my greps enough as-is. Maybe I'll start streaming nyan cat each request to Rails.logger too, because that'll at least cheer me up.

That came out a bit bitchier than I wanted. Sorry.

prom candy posted:

I think I read on Github or Stack Overflow that suppressing the asset log messages is an option that's coming soon.
code:
config.assets.logger = false
It just removes Served asset blahblahblah.png - 304 Not Modified (1ms). Hopefully more is coming.

prom candy
Dec 16, 2005

Only I may dance

A MIRACLE posted:

I'm ready to kill myself. Okay not really but my friend/frontend dev guy did a pull request today where he basically rewrote our entire landing page, started using compass / blueprint, and now I'm running into problems with the asset pipeline deploying to heroku. It's gotten to the point where I'm ready to scrap the whole thing and redo it in Sinatra. Also I'm a little hung over because I got day-drunk at lunch, but still. Can anyone point me to info on the stylesheet link tree? I think that's where the problem is, like it's not precompiling a few stylesheets because they aren't in the tree or something. Here's what my log looks like:

code:
 ActionView::Template::Error (screen.css isn't precompiled):
2012-02-23T01:54:24+00:00 app[web.1]:   Rendered pages/home.html.haml within layouts/application (3.4ms)
2012-02-23T01:54:24+00:00 app[web.1]:     6:     -# Compass and blueprint stylesheets
2012-02-23T01:54:24+00:00 app[web.1]:     8:     = stylesheet_link_tag 'print', :media => 'print'
2012-02-23T01:54:24+00:00 app[web.1]:   app/views/layouts/application.html.haml:7:in 
`_app_views_layouts_application_html_haml___2737759042931766110_26806720'
Any help is appreciated.

If you're including an asset directly in a template that's not called application.js.*, application.css.*, or is not an image Rails won't automatically precompile it in production. You either need to require screen.css from application.css, or you need to add it to your list of precompiles in production.rb

code:
 config.assets.precompile += ["screen.css", "print.css"]
Edit: The asset pipeline is full of gotchas, make sure you read the documentation on it or it's going to drive you nuts. I speak from experience.

8ender
Sep 24, 2003

clown is watching you sleep

prom candy posted:

Edit: The asset pipeline is full of gotchas, make sure you read the documentation on it or it's going to drive you nuts. I speak from experience.

Just one that note: When we did the asset pipeline here we ended up creating a few different directories in the stylsheets folder and putting a directoryname.css.erb in each one along with the other stylesheets that just had this in it:

*= require_self
*= require_tree .

This was for the different distinct sections of our site like admin, public, team, etc. Then we added it to the precompile line. We're pretty happy with the results.

The asset pipeline is a complete bastard but I can't argue with the results. According to newrelic it dropped our page load times significantly.

Adbot
ADBOT LOVES YOU

Big Nubbins
Jun 1, 2004

Kim Jong III posted:

Hi Rails people! I'm going to be picking up RoR soon for my job. Does anyone have any recommended tutorials that are focused on someone who has plenty of experience under their belt?

I'll be moving from a mostly Python/Django environment, if that helps. So I'm comfortable with MVC, ORMs, etc.

Sorry if this has been asked before. I tried to look through the thread but it's kinda massive...

If you're going to be using ActiveRecord as your ORM, I highly suggest using Squeel. The AR query methods aren't completely horrible, but get verbose very quickly. If you're doing the "skinny controller, fat model" thing, it'll make the sea of scopes you'll invariably end up with 100 times more readable, and up much less space.

To make your stay at Rails more pleasant, check out these guys' projects on Github to see if there's anything you might want to use:
https://github.com/plataformatec
https://github.com/josevalim
https://github.com/collectiveidea
I personally wouldn't fly without inherited resources, has_scope, and responders again, namely. Coming into Rails, I was initially depressed by how much code repetition there was in controllers.

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