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
Hammertime
May 21, 2003
stop

rugbert posted:

how doe reject work?

Uve got a model PageContent with a field called type.

Its its controller Im trying to get all PageContent entries except ones that have the type of BlogPost.

I tried
@pages = PageContent.all.reject{|p| p.type == 'BlogPost'}

But thats not doing poo poo

Ahh, undocumented rails "gotcha". You can't call any of your columns 'type', it's an inherent ruby property. Take a look at what you get when you actually run "p.type" in the console.

As a side note, if you're just trying to filter the models, you should be applying the predicate as part of the conditions given to the database. Doing a PageContent.all.reject hydrates all the objects first then throws a bunch of them out.

If you're rails 2.3.9 land:

@pages = PageContent.all :conditions => "content_type != 'BlogPost'"

Adbot
ADBOT LOVES YOU

rugbert
Mar 26, 2003
yea, fuck you

Hammertime posted:

Ahh, undocumented rails "gotcha". You can't call any of your columns 'type', it's an inherent ruby property. Take a look at what you get when you actually run "p.type" in the console.

As a side note, if you're just trying to filter the models, you should be applying the predicate as part of the conditions given to the database. Doing a PageContent.all.reject hydrates all the objects first then throws a bunch of them out.

If you're rails 2.3.9 land:

@pages = PageContent.all :conditions => "content_type != 'BlogPost'"

I was taught to use type so that I could have inherited models. So I have a PageContent model and then I have a BlogPost model inheriting from it and I needed to have they type field to make it work. Which is does, but in this case I want to reject all BlogPost entries.

Also Im using rails 3

drjrock_obgyn
Oct 11, 2003

once i started putting pebbles in her vagina, there was no going back

rugbert posted:

I was taught to use type so that I could have inherited models. So I have a PageContent model and then I have a BlogPost model inheriting from it and I needed to have they type field to make it work. Which is does, but in this case I want to reject all BlogPost entries.

Also Im using rails 3

Try doing the equivalent query that Hammertime suggested but with rails 3 syntax:

code:
@pages = PageContent.where('type != ?', 'BlogPost').all
As a side note, if you did want to use reject here, you'd have to use the class or cast it to a string because it's inherited:

code:
@pages = PageContent.all.reject{|p| p.class.to_s == 'BlogPost'}
I'd recommend the first one, though, so you're not doing extra work.

rugbert
Mar 26, 2003
yea, fuck you

drjrock_obgyn posted:

Try doing the equivalent query that Hammertime suggested but with rails 3 syntax:

code:
@pages = PageContent.where('type != ?', 'BlogPost').all
As a side note, if you did want to use reject here, you'd have to use the class or cast it to a string because it's inherited:

code:
@pages = PageContent.all.reject{|p| p.class.to_s == 'BlogPost'}
I'd recommend the first one, though, so you're not doing extra work.

Oh cool thanks. That did it.


So about heroku, Ive noticed that when Im using s3 and paperclip to upload images everytime I push new changes all of my uploaded images break. Is there a specific way I should set up my model? Right now I have:"

code:
:url => "/:attachment/:id/:style/:basename.:extension",
:path => "/:rails_root/public/:attachment/:id/:style/:basename.:extension"

drjrock_obgyn
Oct 11, 2003

once i started putting pebbles in her vagina, there was no going back

rugbert posted:

Oh cool thanks. That did it.


So about heroku, Ive noticed that when Im using s3 and paperclip to upload images everytime I push new changes all of my uploaded images break. Is there a specific way I should set up my model? Right now I have:"

code:
:url => "/:attachment/:id/:style/:basename.:extension",
:path => "/:rails_root/public/:attachment/:id/:style/:basename.:extension"

You're telling paperclip to use the options for local storage there so it's probably not getting symlinked on each deploy (which you don't want to use with Heroku anyway). Have a look at the rdoc for the s3 storage and adjust the parameters accordingly: http://rdoc.info/github/thoughtbot/paperclip/master/Paperclip/Storage/S3.

Obsurveyor
Jan 10, 2003

Any recommendations on where to start with choosing an authentication library for Rails 3 that will utilize existing database columns?

Unfortunately, there is simply no way I can re-write everything I have in PHP for the new website I am working on in the time I have. That means I need to pull usernames and passwords from my existing CRM database. I do not need fancy features such as password reminders, resets, roles, etc. I just need something that will block page access based on logged in/not logged in and one other flag about the user's company in the database.

Anveo
Mar 23, 2002

Obsurveyor posted:

Any recommendations on where to start with choosing an authentication library for Rails 3 that will utilize existing database columns?

Unfortunately, there is simply no way I can re-write everything I have in PHP for the new website I am working on in the time I have. That means I need to pull usernames and passwords from my existing CRM database. I do not need fancy features such as password reminders, resets, roles, etc. I just need something that will block page access based on logged in/not logged in and one other flag about the user's company in the database.

For simple needs like that you probably don't need a full blown library, just roll your own. And by rolling your own I just mean creating controller that checks a username and password and sets a session var with the user_id, and maybe a helper method that returns the user object from that session id.

NotShadowStar
Sep 20, 2000

Anveo posted:

For simple needs like that you probably don't need a full blown library, just roll your own. And by rolling your own I just mean creating controller that checks a username and password and sets a session var with the user_id, and maybe a helper method that returns the user object from that session id.

Absolutely not. You will get it wrong an it will have holes

Just use Devise. It's a granular as you want it to be and very simple to set up.

Obsurveyor
Jan 10, 2003

NotShadowStar posted:

Just use Devise. It's a granular as you want it to be and very simple to set up.
I wish Devise had actual documentation. Having an unsearchable wiki on github filled with a bunch of extremely specific howtos and no introduction to the API is classic. There appears to be nothing on how to add Devise to an existing rails app(rake devise:setup would not be wholly appropriate for me, with the legacy database involved). It just makes me want to look elsewhere for something better documented.

maskenfreiheit
Dec 30, 2004
.

maskenfreiheit fucked around with this message at 21:32 on Apr 28, 2019

Sharrow
Aug 20, 2007

So... mediocre.
code:
#!/usr/bin/ruby
require 'rubygems'
require 'nokogiri'

maskenfreiheit
Dec 30, 2004
.

maskenfreiheit fucked around with this message at 21:32 on Apr 28, 2019

maskenfreiheit
Dec 30, 2004
.

maskenfreiheit fucked around with this message at 21:32 on Apr 28, 2019

Sharrow
Aug 20, 2007

So... mediocre.
Check this page: http://nokogiri.org/tutorials/parsing_an_html_xml_document.html

There's no variable called table_string defined anywhere. It's expecting table_string to contain something, like this:

code:
table_string = "<html><head></head><body><table><tr><td>butts</td></tr></table></body></html>"
doc = Nokogiri::HTML(table_string)
Without defining table_string, it's going to be nil.

If you put a URL in there, that URL isn't magically going to be parsed either. It's going to try and run an XPath query against the string "http://www.goatse.fr/" instead, and won't print anything because your XPath query returns no TR nodes to iterate through.

However, the open-uri module gives you a dead simple way to pull a document in:

code:
require 'open-uri'
doc = Nokogiri::HTML(open("http://www.goatse.fr/"))
which is functionally the same as

code:
require 'open-uri'
goatse_html = open("http://www.goatse.fr/")
doc = Nokogiri::HTML(goatse_html)

NotShadowStar
Sep 20, 2000

Obsurveyor posted:

I wish Devise had actual documentation. Having an unsearchable wiki on github filled with a bunch of extremely specific howtos and no introduction to the API is classic. There appears to be nothing on how to add Devise to an existing rails app(rake devise:setup would not be wholly appropriate for me, with the legacy database involved). It just makes me want to look elsewhere for something better documented.

http://railscasts.com/episodes/209-introducing-devise

http://railscasts.com/episodes/210-customizing-devise

You may also try Omniauth, but I have no experience with it. Devise and Warden have been battle tested

http://railscasts.com/episodes/241-simple-omniauth

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.

Edit: Grrrrrrrrrr...nevermind, fixed it. Problem was NoScript.

Plastic Jesus fucked around with this message at 23:12 on Jan 19, 2011

bitprophet
Jul 22, 2004
Taco Defender

NotShadowStar posted:

http://railscasts.com/episodes/209-introducing-devise

http://railscasts.com/episodes/210-customizing-devise

You may also try Omniauth, but I have no experience with it. Devise and Warden have been battle tested

http://railscasts.com/episodes/241-simple-omniauth

Oh good, screencasts.

I love using Ruby, but it seriously irks me that the community's idea of "documentation" is one of: read a simple-rear end Hello World style README, pay money for a book, watch a (sometimes for-pay, almost always limited in scope) video, or learn the source code inside and out.

Heaven forbid the author take the time to write some comprehensive usage + API documentation so non-trivial non-common use cases aren't left out in the cold.

One of these days the Ruby and Python communities should switch places for a bit so Python can learn how to have usable test frameworks and Ruby can learn how to document their projects.

I'm not bitter, who's bitter?

NotShadowStar
Sep 20, 2000
I LIKE screencasts :colbert: Especially Railscasts, they're very well done. Show, don't tell. Most documentation is awful anywhere you go.

Most of the time with Ruby code, all you need is the basic idea and the RDoc. Ruby code is mostly self-documenting unless it's loving horrible, then nobody uses it.

Now, if you want to talk about 'give us money because our poo poo is so hosed you can't figure it out', I'm getting into Drupal and holy gently caress. It's like they took acid hits on mars and designed some sort of hosed information system in PHP and completely didn't care about documenting it.

Obsurveyor
Jan 10, 2003

bitprophet posted:

I love using Ruby, but it seriously irks me that the community's idea of "documentation" is one of: read a simple-rear end Hello World style README, pay money for a book, watch a (sometimes for-pay, almost always limited in scope) video, or learn the source code inside and out.

Heaven forbid the author take the time to write some comprehensive usage + API documentation so non-trivial non-common use cases aren't left out in the cold.
I feel like this a lot when I start working with ruby again. Thank god this time it is for real, for my day job, instead of just dinking around. I completely forgot about railscasts but at least there always seem to be matching asciicasts too. I dislike watching programming videos, it is so contrived. I also always forget about about rdoc but you have to have the gem already installed, not many projects actually have their rdocs online it seems.

Thanks for the links NotShadowStar, I will be reviewing the asciicasts at work tomorrow. I think I have put together everything I need to use Devise and work with my legacy databases for the time being.

bitprophet
Jul 22, 2004
Taco Defender

NotShadowStar posted:

I LIKE screencasts :colbert: Especially Railscasts, they're very well done. Show, don't tell.

I'm sure they're well executed (I actually don't watch very many and probably should check out more), I just would much rather search or skim text documentation than watch an hour long video in the vain hope that the info I need is at the 45 minute mark :(

And aren't most screencasts just a glorified README? Or do some of them actually go in-depth?

quote:

Most of the time with Ruby code, all you need is the basic idea and the RDoc. Ruby code is mostly self-documenting unless it's loving horrible, then nobody uses it.

Eh, many Ruby projects link to an RDoc with a bare skeleton of actual information (as in, great, I can see that X class has foo, bar and baz methods, but none of them actually have any documentation, just the function signature) and call it a day.

Especially recently when some very nice doc oriented tools like YARD exist, there's just no excuse not to flesh out your API content and throw in at least a few pages' worth of prose docs explaining the relationship between the classes.

quote:

Now, if you want to talk about 'give us money because our poo poo is so hosed you can't figure it out', I'm getting into Drupal and holy gently caress.

See also Magento, EZPublish, and probably every other huge PHP project :eng99: It's true, I'd much rather wade through Ruby source code to figure something out, than use PHP -- but I still shouldn't have to.

As mentioned, though, nothing is perfect. Python spoils me on documentation and so I get frustrated trying to put Ruby stuff together on a deadline; and when I'm working in Python using basic JUnit-style unit tests, I look back at rspec/cucumber/etc and cry a little.

rugbert
Mar 26, 2003
yea, fuck you
So I have a model, FloorPlan that has_many images. Can I use will_paginate to paginate just those images?

my controller just has @floorplan = FloorPlan.find(params[:id])

and the view is a loop:
code:
<% @floorplan.images.each do |i| %>
  <li>
  <%= link_to image_tag(i.photo.url(:small)), i.photo.url(:primary), :class => "gallery_image" %>
  </li>
<% end -%>

8ender
Sep 24, 2003

clown is watching you sleep
Ugh. A client of ours wants to do adhoc excel based data reports with our software. My experience is that this feature is always requested buy rarely actually used. Oh well.

Does anyone know of a good gem to make this less painful?

NotShadowStar
Sep 20, 2000
Does it HAVE to be .xls? Usually I've had good success with just doing .csv and making sure the column headings are there, Excel imports it fine, just one extra click after opening it.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense

rugbert posted:

So I have a model, FloorPlan that has_many images. Can I use will_paginate to paginate just those images?

my controller just has @floorplan = FloorPlan.find(params[:id])

and the view is a loop:
code:
<% @floorplan.images.each do |i| %>
  <li>
  <%= link_to image_tag(i.photo.url(:small)), i.photo.url(:primary), :class => "gallery_image" %>
  </li>
<% end -%>

Will_paginate is exceedingly easy to use. it's just @floorplan.images.paginate(:page => params[:page]).... what did you think you needed to do? Probably one of the best gems out there.

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.
I want to use the Linked In gem, but my app also uses Omniauth. Omniauth requires a newer version of oauth than the Linked In gem specifies, and Linked In specifies an exact version rather than '>='. This is easy to fix locally, but it'll break when I push to Heroku since Heroku will download the linkedin gem. Is there any clean way to supply a gemfile when pushing to Heroku?

Obsurveyor
Jan 10, 2003

Anyone know the proper way to unit test a Rails initializer? Specifically, a custom decryptor for Devise? I know how to test it but I have no idea how you get at initializers with a test. I put it in a controller test but that feels really, really dirty.

enki42
Jun 11, 2001
#ATMLIVESMATTER

Put this Nazi-lover on ignore immediately!

Plastic Jesus posted:

I want to use the Linked In gem, but my app also uses Omniauth. Omniauth requires a newer version of oauth than the Linked In gem specifies, and Linked In specifies an exact version rather than '>='. This is easy to fix locally, but it'll break when I push to Heroku since Heroku will download the linkedin gem. Is there any clean way to supply a gemfile when pushing to Heroku?

I had the exact same problem, and fixed it locally and forked the linked in gem and pointed it to my own git repository. You can use mine if you like:

code:
git://github.com/ryanbrunner/linkedin.git
My branches are a bit of a mess though, if you use "new_search" oauth should play nicely with omniauth. Gemfiles take git repositories with no problems, just use:

code:
gem 'linkedin', :git => 'git://github.com/ryanbrunner/linkedin.git', :branch => 'new_search'
To be honest, I think someone needs to take over the linkedin gem. I've had these commits open in a fork request for months now and the gem is starting to get seriously out of date.

HIERARCHY OF WEEDZ
Aug 1, 2005

bitprophet posted:

Oh good, screencasts.

I love using Ruby, but it seriously irks me that the community's idea of "documentation" is one of: read a simple-rear end Hello World style README, pay money for a book, watch a (sometimes for-pay, almost always limited in scope) video, or learn the source code inside and out.

Heaven forbid the author take the time to write some comprehensive usage + API documentation so non-trivial non-common use cases aren't left out in the cold.

One of these days the Ruby and Python communities should switch places for a bit so Python can learn how to have usable test frameworks and Ruby can learn how to document their projects.

I'm not bitter, who's bitter?

Uhh, if you had gone to any of the Railscasts pages, you would see that every one has a complete transcript located on ASCIIcasts: http://asciicasts.com/episodes/209-introducing-devise

8ender
Sep 24, 2003

clown is watching you sleep

NotShadowStar posted:

Does it HAVE to be .xls? Usually I've had good success with just doing .csv and making sure the column headings are there, Excel imports it fine, just one extra click after opening it.

Well no, CSV is fine actually. The main part, which I know is going to be a time vortex for our developers, is the adhoc "click all these dropdowns to get the data the way you want" part. Its also the part that in my experience no one ever ends up using.

I may just push back and go for canned reports. Excel is better at massaging flat data than we'll ever be.

bitprophet
Jul 22, 2004
Taco Defender

HIERARCHY OF WEEDZ posted:

Uhh, if you had gone to any of the Railscasts pages, you would see that every one has a complete transcript located on ASCIIcasts: http://asciicasts.com/episodes/209-introducing-devise

Where did I say I was doing Rails work? ;) (Yea, I know, it's technically a Rails thread...but it's not like there's a regular Ruby thread or I'd post in there. Another one of my beefs...Ruby the language is way too overshadowed by Rails.)

Good to know that there are sometimes transcripts, anyway. I'll keep an eye out for such offerings next time I find myself looking at screencasts.

bitprophet fucked around with this message at 19:11 on Jan 21, 2011

NotShadowStar
Sep 20, 2000
To be fair people were doing wacky as hell things to the syntax and structure of the Ruby language until ActiveRecord and ActiveSupport. I maintain the LDAP library and holy crap, Ruby code from 2003-2004 is bizarre.

Then after Rails people started doing wacky as hell things to the metaprogramming aspect of Ruby mirroring Rails, but the structure was nicer.

Then Yehuda Katz and Sam Ruby came in and kicked all the wacky metaprogramming out and now everything is :cool:

bitprophet
Jul 22, 2004
Taco Defender

NotShadowStar posted:

To be fair people were doing wacky as hell things to the syntax and structure of the Ruby language until ActiveRecord and ActiveSupport. I maintain the LDAP library and holy crap, Ruby code from 2003-2004 is bizarre.

Then after Rails people started doing wacky as hell things to the metaprogramming aspect of Ruby mirroring Rails, but the structure was nicer.

Then Yehuda Katz and Sam Ruby came in and kicked all the wacky metaprogramming out and now everything is :cool:

I love Yehuda, he's awesome. (He's also not ashamed to look at what Python and Django did right and to steal ideas, which is cool. And Rails 3 struck me as sort of a Rails version of Django's 2006-2007 "magic-removal" upheaval.)

And I'm not saying that Rails is bad or that its influence on Ruby has not been a net positive; just that as a not-usually-Rails dev using Ruby, it's frustrating that everything out there (blog posts, documentation, the way stuff is packaged, answers to questions etc) often assumes a Rails context.

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.

enki42 posted:

I had the exact same problem, and fixed it locally and forked the linked in gem and pointed it to my own git repository. You can use mine if you like:

code:
git://github.com/ryanbrunner/linkedin.git
My branches are a bit of a mess though, if you use "new_search" oauth should play nicely with omniauth. Gemfiles take git repositories with no problems, just use:

code:
gem 'linkedin', :git => 'git://github.com/ryanbrunner/linkedin.git', :branch => 'new_search'
To be honest, I think someone needs to take over the linkedin gem. I've had these commits open in a fork request for months now and the gem is starting to get seriously out of date.

Thanks for this. I have no clue why it didn't occur to me to use github but it didn't. Wait, right, it's because I'ma deeply stupid man.

rugbert
Mar 26, 2003
yea, fuck you
Ok, so I thought I had this figured out (in my head) but I need some help.

If I have a model say, Drawing and it belongs_to :user and each User has_one :drawing how would I set it up so when a user logs in, he only see's his drawings and no one else's?

All I can think of off hand is to add a user_id to all drawings and have the controller look for all drawings where user_id == current_user.id (Im using devise).

Whats the best way to tackle this or whats a good tutorial for scenarios like this?

NotShadowStar
Sep 20, 2000
code:
class User<ActiveRecord::Base
 has_many :drawings
end

class Drawing<ActiveRecord::Base
 belongs_to :user #foreign key user_id
end

u = User.find(id)
u.drawings

rugbert
Mar 26, 2003
yea, fuck you

NotShadowStar posted:

code:
u = User.find(id)
u.drawings

ok so what that does is grabs a user's ID and then uses that ID to find the corresponding drawing. When and how do I use that tho? In the controller?

Like @drawings = u.drawings.all?

Could I use devise's current_user to get the drawings like current_user.drawings?

Sorry, Im just started to get deeper into RoR but I havent had a hell of a lot of time so Im learning as I go.

NotShadowStar
Sep 20, 2000
You really should learn the fundamentals of ActiveRecord associations, because this is really basic stuff.

http://guides.rubyonrails.org/association_basics.html

Triggerle
Jun 3, 2001
I'm doing my first rails project and I'm wondering if there is a best practice for admin interfaces? From my research it seems that I can go with namespaces or with dual-use controllers and views.

With namespaces I have to make two controllers with their respective views and in each I have 95% of identical code. If I go without namespaces I have my views all cluttered up by <%= if current_user.admin? %> and inserted admin functionality.

Neither seems very appealing to me.

skidooer
Aug 6, 2001

Triggerle posted:

If I go without namespaces I have my views all cluttered up by <%= if current_user.admin? %> and inserted admin functionality.
You can use different views within the same controller. Something like:

code:
class FoosController < ApplicationController
  def index
    @foos = Foo.all    

    render 'admin/foos/index' if current_user.admin?
  end
end

Adbot
ADBOT LOVES YOU

8ender
Sep 24, 2003

clown is watching you sleep

Triggerle posted:

With namespaces I have to make two controllers with their respective views and in each I have 95% of identical code. If I go without namespaces I have my views all cluttered up by <%= if current_user.admin? %> and inserted admin functionality.

I went with namespaces across three projects and I haven't regretted it. My experience with admin stuff is that although there are going to be times (in views in particular) when you're duplicating some things its worth it in the long run to have the flexibility to do something completely different for the admin without putting logic everywhere and hoping there isn't some edge case you're missing thats exposing that to regular users.

I've even got a namespace for the "manager" interface in one of our apps because its so fundamentally different from the regular way employees access the same data.

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