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
Obsurveyor
Jan 10, 2003

I am about at my breaking point with using Devise. I got everything working today with my legacy database and datamapper. I am using my mongodb as the session store but now my spork server stuff is completely broken. database.yml is not being loaded under spork, everything works fine in 'rails server' and 'rails console'. I cannot figure out why it is doing this.

None of this is really Devise's fault but it has been super frustrating dealing with it and its lack of documentation. There does not seem to be anywhere where all the callback hooks are documented in one place. Most of the documentation a step away from using the generators is really vague or really, really specific and ends up not being very helpful. I hate all the magic that happens in devise_for. At this point, I am really tempted to just roll my own based on the Ruby on Rails Tutorial and the railscast that is out there and maybe trying to integrate warden into that.

Other than fixing spork, the last thing I need to get working is making it so a user can only be logged in on a single session. There does not seem to be any support for this kind of thing built-in to Devise, which is really strange to me because it seems like it would be an important feature. I am hoping there is a hook in Devise for post-login, before the redirect, but I cannot find it if there is.

Obsurveyor fucked around with this message at 22:54 on Mar 16, 2011

Adbot
ADBOT LOVES YOU

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 a rails noob working through a tutorial. I added a CSS layout to my template and now the method buttons like "Show", "Edit" etc include a path, like "Show /bugs/field_samples/3".

I was wondering if there was something in the CSS that could cause it to change like that. The CSS is "Blueprint" if that matters.

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!

A MIRACLE posted:

I'm a rails noob working through a tutorial. I added a CSS layout to my template and now the method buttons like "Show", "Edit" etc include a path, like "Show /bugs/field_samples/3".

I was wondering if there was something in the CSS that could cause it to change like that. The CSS is "Blueprint" if that matters.

That code is on the button itself? Do a pastebin or put the code in your post so we can see, and maybe a screenshot of the actual app.

This tutorial does a few things like buttons with Blueprint - http://ruby.railstutorial.org/ruby-on-rails-tutorial-book

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.

here's the code:
code:
<h1>Listing Field Samples</h1>
<b>Hello!</b>
<table>
  <tr>
    <th>Name</th>
    <th>Latitude</th>
    <th>Longitude</th>
  </tr>

<% @field_samples.each do |field_sample| %>
  <tr>
    <td><%=h field_sample.name %></td>
    <td><%=h field_sample.latitude %></td>
    <td><%=h field_sample.longitude %></td>
    <td><%= link_to 'Show', field_sample %></td>
    <td><%= link_to 'Edit', edit_field_sample_path(field_sample) %></td>
    <td><%= link_to 'Destroy', field_sample, :confirm => 'Are you sure?', :method => :delete %></td>
  </tr>
<% end %>
</table>

<br />

<%= link_to 'New field_sample', new_field_sample_path %>
here's a screenshot:

hmm yes
Dec 2, 2000
College Slice
Looks like you loaded the blueprint print stylesheet and not the screen stylesheet.

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.

atastypie posted:

Looks like you loaded the blueprint print stylesheet and not the screen stylesheet.

Thanks. Is there a reason the changes I make on a views/ file take some time to propagate (as opposed to showing the results instantly)?

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

Obsurveyor posted:

I am about at my breaking point with using Devise.

You might not want to hear this, but ripping Devise out of a project that used it was one of the best things I've ever done for productivity.

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

A MIRACLE posted:

Thanks. Is there a reason the changes I make on a views/ file take some time to propagate (as opposed to showing the results instantly)?

Are you editing on the live server running in production mode? If so, don't do that; edit locally in development mode, use version control, and a deployment tool. PHP amateur hour poo poo doesn't fly in Rails.

Anveo
Mar 23, 2002
Just a heads up that the MountainWest Ruby Conference is livestreaming: http://mtnwestrubyconf.org/

Obsurveyor
Jan 10, 2003

BonzoESC posted:

You might not want to hear this, but ripping Devise out of a project that used it was one of the best things I've ever done for productivity.
I dumped Devise from my auth branch today. I think the dm-devise gem(Devise support for Datamapper) is what was screwing up my spork. I guess he monkey patches the crap out of stuff to get it to work and it broke something having to do with database.yml when spork loads the stack.

Dumbest thing I found today in my code:
code:
require 'database_cleaner'
DatabaseCleaner.strategy = :truncation
DatabaseCleaner.orm = "mongoid"
Before { DatabaseCleaner.clean }
wherever I read that this was the proper way to configure database_cleaner for mongoid was dead wrong. Unless you like it truncating(dropping all your databases) every ORM you use by default when you run cucumber. This was very bad for Datamapper and legacy databases. Thankfully, I use paranoid user permissions for the database access.

8ender
Sep 24, 2003

clown is watching you sleep

Anveo posted:

Any specific reason you can't just use a tempfile?


Thanks for this. I tried out a tempfile before and wasn't able to get it to work. No dice this time either. I think the problem is that the tempfile is being created correctly, but simple_xlsx_writer likes to make a whole mess of temp files in the same directory as the specified file and it isn't able to. Either way I was getting some bizarre zip header errors.

I ended up creating a temp directory in my rails public folder, creating a file for export, writing the xlsx and streaming it with send_file, then using FileUtils to manually delete it afterwards. Its a kludge but it works.

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

8ender posted:

I ended up creating a temp directory in my rails public folder, creating a file for export, writing the xlsx and streaming it with send_file, then using FileUtils to manually delete it afterwards. Its a kludge but it works.

If you're using send_file, you probably don't need to put it in $APP/public/, you could get away with $APP/tmp or /tmp/application_name.

If generating the xlsx is a slow task and you're not on Heroku or Windows, I'd probably use resque to move the xlsx generation out of the web request.

8ender
Sep 24, 2003

clown is watching you sleep

BonzoESC posted:

If you're using send_file, you probably don't need to put it in $APP/public/, you could get away with $APP/tmp or /tmp/application_name.

If generating the xlsx is a slow task and you're not on Heroku or Windows, I'd probably use resque to move the xlsx generation out of the web request.

Actually the best part of simple_xlsx_writer is that its really drat fast. I tried it out with a 800 row spreadsheet and while it put some heat on the database the actual spreadsheet generation was quick. Either way its quick enough for a web request right now.

Edit: Forgot to say thanks for the link to Resque. Looks great and I'm already thinking of a few things I can offload.

Oh My Science
Dec 29, 2008
code:


Installing bcrypt-ruby (2.1.4) /Library/Ruby/Site/1.8/rubygems/installer.rb:574:in `initialize': Permission denied - /Library/Ruby/Gems/1.8/gems/bcrypt-ruby-2.1.4/.gitignore (Errno::EACCES)
	from /Library/Ruby/Site/1.8/rubygems/installer.rb:574:in `open'
	from /Library/Ruby/Site/1.8/rubygems/installer.rb:574:in `extract_files'
	from /Library/Ruby/Site/1.8/rubygems/installer.rb:550:in `each'
	from /Library/Ruby/Site/1.8/rubygems/installer.rb:550:in `extract_files'
	from /Library/Ruby/Site/1.8/rubygems/installer.rb:158:in `install'
	from /Library/Ruby/Gems/1.8/gems/bundler-1.0.10/lib/bundler/source.rb:96:in `install'
	from /Library/Ruby/Gems/1.8/gems/bundler-1.0.10/lib/bundler/installer.rb:55:in `run'
	from /Library/Ruby/Gems/1.8/gems/bundler-1.0.10/lib/bundler/spec_set.rb:12:in `each'
	from /Library/Ruby/Gems/1.8/gems/bundler-1.0.10/lib/bundler/spec_set.rb:12:in `each'
	from /Library/Ruby/Gems/1.8/gems/bundler-1.0.10/lib/bundler/installer.rb:44:in `run'
	from /Library/Ruby/Gems/1.8/gems/bundler-1.0.10/lib/bundler/installer.rb:8:in `install'
	from /Library/Ruby/Gems/1.8/gems/bundler-1.0.10/lib/bundler/cli.rb:275:in `update'
	from /Library/Ruby/Gems/1.8/gems/bundler-1.0.10/lib/bundler/vendor/thor/task.rb:22:in `send'
	from /Library/Ruby/Gems/1.8/gems/bundler-1.0.10/lib/bundler/vendor/thor/task.rb:22:in `run'
	from /Library/Ruby/Gems/1.8/gems/bundler-1.0.10/lib/bundler/vendor/thor/invocation.rb:118:in `invoke_task'
	from /Library/Ruby/Gems/1.8/gems/bundler-1.0.10/lib/bundler/vendor/thor.rb:246:in `dispatch'
	from /Library/Ruby/Gems/1.8/gems/bundler-1.0.10/lib/bundler/vendor/thor/base.rb:389:in `start'
	from /Library/Ruby/Gems/1.8/gems/bundler-1.0.10/bin/bundle:13
	from /usr/bin/bundle:19:in `load'
	from /usr/bin/bundle:19
I'm having an issue using devise with a new install of OS X 10.6 + xCode 4. Anyone else encounter this and know of a fix? Google has failed me so far.

NotShadowStar
Sep 20, 2000
The Rails Thread: USE RVM AND HEROKU. gently caress.

The first applies to you.

rugbert
Mar 26, 2003
yea, fuck you
So whats some good e-commerce gems? Im looking at spreecommerce and that looks OK I guess. Ive never worked with selling poo poo from a website other than using paypal so this is a first.

NotShadowStar posted:

The Rails Thread: USE RVM AND HEROKU. gently caress.

The first applies to you.

Can you use multiple accounts on heroku from the same computer? I dont like the idea of hosting a bunch of client sites on my heroku account. Sometimes I want to just push it off on them after Im done building so I dont have to deal with them anymore.

Anveo
Mar 23, 2002

rugbert posted:

So whats some good e-commerce gems? Im looking at spreecommerce and that looks OK I guess. Ive never worked with selling poo poo from a website other than using paypal so this is a first.

Spree is a whole storefront package in Rails, but if you just need to deal with processing payments Active Merchant is probably what you want.

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

rugbert posted:

Can you use multiple accounts on heroku from the same computer? I dont like the idea of hosting a bunch of client sites on my heroku account. Sometimes I want to just push it off on them after Im done building so I dont have to deal with them anymore.

Make a new account for the client site, grant your account permissions. As part of the client handoff, make sure they log in, change and document the credentials, and remove your permissions.

rugbert
Mar 26, 2003
yea, fuck you

Anveo posted:

Spree is a whole storefront package in Rails, but if you just need to deal with processing payments Active Merchant is probably what you want.

I mean, I guess having a shopping cart is a good idea. Ive never even attempted to build one. Is there a good tutorial out there to help me out? I just got 2 jobs that want e-commerce.

hmm yes
Dec 2, 2000
College Slice
My recommendation is to identify who provides the merchant accounts and use the hosted payment pages offered by those companies. ActiveMerchant will let you integrate with responses back to your server. Do not under and circumstances have a user enter credit card information on your server. Read up on PCI and make sure you understand it. Figuring the rest out is all pretty easy in rails. Bare minimum for a shopping cart is probably Products, Carts, Cart Items, Orders, Order Items. If you're not sure where to start, you might be better off subcontracting.

Or just use Spree. It's awesome.

hmm yes fucked around with this message at 02:45 on Mar 26, 2011

Obsurveyor
Jan 10, 2003

Does anyone understand how before_filters work in Rails 3? I have been struggling with trying to secure js/json access to my app using the auth filters I already have setup to avoid code duplication. before_filter is supposed to be an alias for append_before_filter which implies that it is not messing with filters created beforehand, thus the word "append". However, if you do something like:

code:
before_filter :authenticate
before_filter :content_editor?
#append_before_filter :authenticate, :only => [:show]

...
private

  def authenticate
    puts "Authenticating"
  end

  def content_editor?
    puts "Are you a content editor?"
  end
Everything works great, all actions run both filters, as expected. However, if you uncomment the append_before_filter line, authenticate stops being called for all actions and is only called for :show now.

This seems broken to me.

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

Obsurveyor posted:

Does anyone understand how before_filters work in Rails 3? I have been struggling with trying to secure js/json access to my app using the auth filters I already have setup to avoid code duplication. before_filter is supposed to be an alias for append_before_filter which implies that it is not messing with filters created beforehand, thus the word "append". However, if you do something like:

code:
before_filter :authenticate
before_filter :content_editor?
#append_before_filter :authenticate, :only => [:show]

...
private

  def authenticate
    puts "Authenticating"
  end

  def content_editor?
    puts "Are you a content editor?"
  end
Everything works great, all actions run both filters, as expected. However, if you uncomment the append_before_filter line, authenticate stops being called for all actions and is only called for :show now.

This seems broken to me.

Why are you trying to run the same before_filter multiple times?

code:
class DicksController < ApplicationController
  before_filter :require_login

  def require_login
    require_http_session || require_oauth_session
  end

  def require_http_session
    return false unless current_user
  end

  def current_user
    @current_user ||= User.find(session[:user_id])
  end
end

rugbert
Mar 26, 2003
yea, fuck you

BonzoESC posted:

Make a new account for the client site, grant your account permissions. As part of the client handoff, make sure they log in, change and document the credentials, and remove your permissions.

Ok I made a new account to test this out. Where can I grant my account permissions? I guess what I need to do is create an app on the new account, and then give my account the ability to collaborate? Is there a way to do that from my computer? Because my computer is tied to my account, should I use a VM?

Obsurveyor
Jan 10, 2003

BonzoESC posted:

Why are you trying to run the same before_filter multiple times?
The actual filter would not run multiple times in a real scenario, that was just to show the Rails behavior. This is exactly what I wanted to do earlier:

code:
before_filter :authenticate, :except => [:show, :list]
before_filter :content_editor, :except => [:show, :list]
before_filter :authenticate, :only => [:list], :if => :request_js?
before_filter :content_editor, :only => [:list], :if => :request_js?
So you see, it only runs once for any request. I need to block access to a specific action's js/json format if the user is not a content editor. This does not actually work though.

I ended up just making a new filter just for :list that does the right thing, even if it does duplicate code.

On a related note: Is there any way to make Rails render a 404 for formats that are not supported instead of a 500 and throwing an ActionView::MissingTemplate? I really do not want to add a respond_to block to do it for every action for every format I do not care about supporting.

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

Obsurveyor posted:

The actual filter would not run multiple times in a real scenario, that was just to show the Rails behavior. This is exactly what I wanted to do earlier:

code:
before_filter :authenticate, :except => [:show, :list]
before_filter :content_editor, :except => [:show, :list]
before_filter :authenticate, :only => [:list], :if => :request_js?
before_filter :content_editor, :only => [:list], :if => :request_js?
So you see, it only runs once for any request. I need to block access to a specific action's js/json format if the user is not a content editor. This does not actually work though.
Why would you need to block a specific action for a specific format? If the js version of the action does something wildly different, it's not the same action.

But answering the question you asked instead of the question you should be asking, this is passable I guess:
code:
before_filter :authenticate, :except=>[:show, :list]
before_filter :authenticate_for_show, :only=>:show
before_filter :authenticate_for_list, :only=>:list
The easiest thing might just be to override authenticate in that controller and check conditions before calling super.

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

rugbert posted:

Ok I made a new account to test this out. Where can I grant my account permissions? I guess what I need to do is create an app on the new account, and then give my account the ability to collaborate? Is there a way to do that from my computer? Because my computer is tied to my account, should I use a VM?
From the command-line heroku tool:
code:
sharing:add <email>                        # add a collaborator
sharing:remove <email>                     # remove a collaborator
sharing:transfer <email>                   # transfers the app ownership

rugbert
Mar 26, 2003
yea, fuck you

BonzoESC posted:

From the command-line heroku tool:
code:
sharing:add <email>                        # add a collaborator
sharing:remove <email>                     # remove a collaborator
sharing:transfer <email>                   # transfers the app ownership

ahh awesome. sharing:transfer saves the day! thanks!

FamDav
Mar 29, 2008
EDIT: Wheeee, turns out that "resource" and "resources" gives you different routes.

FamDav fucked around with this message at 01:28 on Mar 30, 2011

rugbert
Mar 26, 2003
yea, fuck you
Looks like watermarking doesnt work with paperclip in rails3. Anyone have an easy way to watermark photos on upload?

edit - wait, looks like I cant put this somewhere in paperclip:
https://gist.github.com/708077 Im not sure where to put it tho.

rugbert fucked around with this message at 21:07 on Mar 29, 2011

NotShadowStar
Sep 20, 2000
Why wouldn't it work?

You can use any Imagemagick option on any different type of file that Paperclip supports.
I don't have it here but this should work:

code:
has_attached_file :photo, :convert_options => {:watermarked => 'opts' }
Where 'opts' is any Imagemagick command line parameters, as such

http://www.imagemagick.org/Usage/annotating/#watermarking

rugbert
Mar 26, 2003
yea, fuck you

NotShadowStar posted:

Why wouldn't it work?

You can use any Imagemagick option on any different type of file that Paperclip supports.
I don't have it here but this should work:

code:
has_attached_file :photo, :convert_options => {:watermarked => 'opts' }
Where 'opts' is any Imagemagick command line parameters, as such

http://www.imagemagick.org/Usage/annotating/#watermarking

Apparently there was a watermark feature:

quote:

:processors => [:watermark],
:geometry => '300x250#',
:watermark_path => ':rails_root/public/images/watermark.png',
:position => 'SouthEast' },

Im trying this:

quote:

:convert_options => [:watermarked =>"-dissolve 25% -gravity south #{RAILS_ROOT}/public/images/watermark.png"]

but Im getting a TypeError: "Symbol as array index"

Anveo
Mar 23, 2002

rugbert posted:

but Im getting a TypeError: "Symbol as array index"

Hash{} not Array[] for :convert_options.

rugbert
Mar 26, 2003
yea, fuck you

Anveo posted:

Hash{} not Array[] for :convert_options.

DOH! Duh, I really need new glasses!

Ok, I tried this:
:convert_options => {:blog =>"-dissolve 25% -gravity south #{RAILS_ROOT}/public/images/watermark.png"}

but Im getting a paperclip error in the console:
PaperclipError: There was an error processing the thumbnail for stream

I guess Im not passing something in correctly or leaving something out? The imagemagick documentation looks like it gets the watermark, original image, and watermarked image passed in.

rugbert fucked around with this message at 23:58 on Mar 29, 2011

plasticbugs
Dec 13, 2006

Special Batman and Robin
I'm writing a simple app that allows you to input a text message and you get back a random code number so you can retrieve the text message later.

I have a Message model with two columns:
contents
codeNumber

I would like the app to generate a unique codeNumber and save it to the database along with the message contents. If I go into the console and do

code:
m = Message.new
m.codeNumber
a random code gets spit out on the command line, BUT it doesn't add the code to the instance's codeNumber.

If I do:

code:
m.codeNumber = m.codeNumber
it works, but obviously that's a terrible work-around.

What am I doing wrong?


My Message model:

__________
class Message < ActiveRecord::Base

def codeNumber
generateCode
end

private
def generateCode
Code Logic in here
end

end
____________


EDIT: I may have found a solution, but it may not be elegant. Please look below.

I revised my Model to institute a callback. This still seems wrong, but it works.

code:
class Message < ActiveRecord::Base
  before_create :codeNumber
    
  def codeNumber
    self.codeNumber = generateCode  
  end
    
private
  def generateCode
    CODE LOGIC HERE
  end
  
end
Is there a better way to do this?

plasticbugs fucked around with this message at 08:14 on Mar 30, 2011

Pardot
Jul 25, 2001




Yeah, you're on the right track, but this is probably what closer to what you want
code:
before_create :ensure_code_number
private
def ensure_code_number
  self.code_number ||= generate_code # don't use generateCode ruby is snake case, idiomatically 
end
The ||= will make it so that if you happen to set a code before you save it the first time, that'll stay, otherwise new records will get a new code, and the ensure should be private

Pardot fucked around with this message at 08:30 on Mar 30, 2011

plasticbugs
Dec 13, 2006

Special Batman and Robin

Pardot posted:

Yeah, you're on the right track, but this is probably what closer to what you wan

Thanks for your help! I'm reading up on memoization right now.

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

plasticbugs posted:

Thanks for your help! I'm reading up on memoization right now.

If you're on Rails 3 (or are otherwise pulling in activesupport 3), you can get a bunch of exrta tools (cache flushing, priming, for free by using the Memoizable module:
code:
class Thingy
  extend ActiveSupport::Memoizable
  
  def slow_thing
    1_000_000.times {…}
  end
  memoize :slow_thing
end
See http://api.rubyonrails.org/classes/ActiveSupport/Memoizable.html and http://api.rubyonrails.org/classes/ActiveSupport/Memoizable/InstanceMethods.html

hmm yes
Dec 2, 2000
College Slice
Anyone know how to prevent helpers from being cached? Even a fresh rails app will cache helpers for me in development mode, forcing me to restart in order to reload changes.

Obsurveyor
Jan 10, 2003

Anyone know how to tell capistrano: "I want to ssh into the target machine as userX but then I want you to execute all commands as userY via sudo"?

Adbot
ADBOT LOVES YOU

rugbert
Mar 26, 2003
yea, fuck you
Is something going on with heroku? Im testing an site on it that works fine on my PC but breaks on heroku and heroku logs isnt giving me anything.

Heroku logs is just showing me GET requests instead of errors now

edit - I re pushed and it seems to be working again.

rugbert fucked around with this message at 23:23 on Mar 31, 2011

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