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
Thalagyrt
Aug 10, 2006

Sub Par posted:

This is what I originally had, but because of the way Heroku works, I was getting nothing but 10.xx.xx.xx IPs that were likely some kind of Heroku load-balancing or whatever. Anyway I'm now trying this:
code:
@click.request_ip = request.env['HTTP_X_REAL_IP'] ||= request.env['REMOTE_ADDR']
We'll see how that goes.

Might want to double check that conditional assignment there. That looks like it should just be an || not an ||= for what you're trying to do.

Adbot
ADBOT LOVES YOU

Jaded Burnout
Jul 10, 2004


kayakyakr posted:

ruby-debug and debugger are built in to ruby 2, no? debugger statement works as if you had the debugger gem installed.

code:
def test
  a = 1
  debugger
  nil
end

test
code:
$ rvm use ruby-2
Using ~/.rvm/gems/ruby-2.1.2
$ ruby debugger.rb 
debugger.rb:3:in `test': undefined local variable or method `debugger' for main:Object (NameError)
	from debugger.rb:7:in `<main>'

Smol
Jun 1, 2011

Stat rosa pristina nomine, nomina nuda tenemus.
Ruby code:
def test
  a = 1
  require 'debug'
  nil
end

test
http://ruby-doc.org/stdlib-2.0.0/libdoc/debug/rdoc/DEBUGGER__.html

kayakyakr
Feb 16, 2004

Kayak is true
e: ^^ or this

e2: experimenting in rails, this makes strange things happen for me. Continuing winds up stopping on every action. Still, I would think that there should be a gem making use of native debug for a quick debugger statement.

Arachnamus posted:

code:
def test
  a = 1
  debugger
  nil
end

test
code:
$ rvm use ruby-2
Using ~/.rvm/gems/ruby-2.1.2
$ ruby debugger.rb 
debugger.rb:3:in `test': undefined local variable or method `debugger' for main:Object (NameError)
	from debugger.rb:7:in `<main>'

Yup, ignore what I've said, use byebug for everything.

kayakyakr fucked around with this message at 01:49 on Jul 26, 2014

Jaded Burnout
Jul 10, 2004


Good to see how to invoke the native one. They're all a bit flakey, I guess due to Ruby (or the community?) lagging behind the rest of the world when it comes to using debuggers.

Given what you've said about use in Rails, I'll keep an eye on it and hope it improves.

FamDav
Mar 29, 2008
since apparently ruby's standard library doesn't have a priority queue (!), whats a solid gem for priority queueing?

Smol
Jun 1, 2011

Stat rosa pristina nomine, nomina nuda tenemus.
https://github.com/boborbt/priority_queue_cxx on MRI, java.util.PriorityQueue on jRuby.

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder
Hello friends,

I work on a very large, old (for rails) codebase. My team has inherited a large section of code and none of us really understand it. Anyhow, I'm trying to write some tests using the existing factories, and they're a mess. I don't want to use them, but they need to remain in place because we have tons and tons of other tests currently relying on them. What is the best way to work around the existing factories without removing them?

My options:

1) Settle for doing something like FactoryGirl.define(:class_new, class: class) with all my new factories
2) Refactor all the old factories/tests to use FactoryGirl.define(:class_old, class: class)

Or something like:
code:
before my_tests
    FactoryGirl.class_eval do 
        @factories.keys.match(/model_regex/).each{|k| @factories.delete(k)}
        define_new_factories
   end
end
after my_tests
    FactoryGirl.class_eval do 
        @factories.keys.match(/model_regex/).each{|k| @factories.delete(k)}
        define_old_factories
   end
end
(that's the wrong attribute accessor but I tried this and it works. But I have no idea what the side effects of this are. I'm sure there's probably a cleaner much cleaner way to remove factories but I havent looked into it because it seems like a bad idea).

4) Use FactoryGirl.modify to modify the classes as needed. I think this would just create more confusion.
5) Just live with what's there.
6) Don't use factory girl.
Which of these is the least idiotic? I'm leaning towards number 2.

DONT THREAD ON ME fucked around with this message at 00:31 on Jul 31, 2014

kayakyakr
Feb 16, 2004

Kayak is true
#2 seems like it'd be the most maintainable.

#3 seems to be the most clever, so that's something.

e: option #7 and the one that will probably cause you the least heart-ache long run is refactor the factories through the complete code base. In other words, fix the factories and then fix broken tests.

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder

kayakyakr posted:

e: option #7 and the one that will probably cause you the least heart-ache long run is refactor the factories through the complete code base. In other words, fix the factories and then fix broken tests.

Yeah, that's the very long term goal. Ideally someday we'll move the tests over to using the new factories but who knows if that will ever happen. At least new tests will have maintainable factories to use.

kayakyakr
Feb 16, 2004

Kayak is true

USSMICHELLEBACHMAN posted:

Yeah, that's the very long term goal. Ideally someday we'll move the tests over to using the new factories but who knows if that will ever happen. At least new tests will have maintainable factories to use.

A half migrated, old rails codebase is a huge pain in the rear end because it will never be fully migrated. You think the factories are bad now? Just wait. Been there, maintained that.

kayakyakr
Feb 16, 2004

Kayak is true
Working on a MongoDB + Mongoid project and suddenly any Integer I store is of the unsigned type. Try to store -1, wind up with 4294967295.

Anyone seen anything like this?

EVGA Longoria
Dec 25, 2005

Let's go exploring!

In Rails 4, is there any way to apply scopes that is not the class methods?

i.e. I have "Post" with the scope "published". Instead of

Ruby code:
Post.published.find(13)
I want to do something like

Ruby code:
Post.scopes(:published).find(13)
The Rails 4 Way says there's a "scoping" class Method, but that doesn't seem to actually exist? Getting told it's an undefined method when I try to use it, and Googling's turning up nothing.

Smol
Jun 1, 2011

Stat rosa pristina nomine, nomina nuda tenemus.

EVGA Longoria posted:

In Rails 4, is there any way to apply scopes that is not the class methods?

i.e. I have "Post" with the scope "published". Instead of

Ruby code:

Post.published.find(13)

I want to do something like

Ruby code:

Post.scopes(:published).find(13)

The Rails 4 Way says there's a "scoping" class Method, but that doesn't seem to actually exist? Getting told it's an undefined method when I try to use it, and Googling's turning up nothing.

What do you actually want to do?

EVGA Longoria
Dec 25, 2005

Let's go exploring!

Smol posted:

What do you actually want to do?

Trying to get rid of code duplication. We have helpers for our API

Ruby code:
def post
 ::Post.find_by_slug_or_id(params[:id])
end

def published_post
 ::Post.published.find_by_slug_or_id(params[:id])
end
I don't like it, so I'm trying to combine it to a single function.

Ruby code:
def post(published = false)
  ??
end
After tweeting the Rails 4 Way authors, I've put the following together

Ruby code:
def post(published = false)
  ::Post.send(published ? :published : :all).find_by_slug_or_id(params[:id])
end
This accomplished my goal, but it's a bit ugly.

Thalagyrt
Aug 10, 2006

EVGA Longoria posted:

Trying to get rid of code duplication. We have helpers for our API

Ruby code:
def post
 ::Post.find_by_slug_or_id(params[:id])
end

def published_post
 ::Post.published.find_by_slug_or_id(params[:id])
end
I don't like it, so I'm trying to combine it to a single function.

Ruby code:
def post(published = false)
  ??
end
After tweeting the Rails 4 Way authors, I've put the following together

Ruby code:
def post(published = false)
  ::Post.send(published ? :published : :all).find_by_slug_or_id(params[:id])
end
This accomplished my goal, but it's a bit ugly.

I'd probably write that a bit differently. It's not a one liner, but easier to understand at a quick glance what's going on.

Ruby code:
def post(published = false)
  posts = ::Post.all
  posts = posts.published if published
  posts.find_by_slug_or_id(params[:id])
end
This does kind of sound like it might be code that belongs in an authorization module instead of in your controller though - are you returning published vs all posts depending on the user that's retrieving the data?

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

kayakyakr posted:

Working on a MongoDB + Mongoid project and suddenly any Integer I store is of the unsigned type. Try to store -1, wind up with 4294967295.

Anyone seen anything like this?

Sounds like it's getting BSON'd (or de-BSON'd) as an unsigned type. Check whichever BSON gem you're using.

kayakyakr
Feb 16, 2004

Kayak is true

Cocoa Crispies posted:

Sounds like it's getting BSON'd (or de-BSON'd) as an unsigned type. Check whichever BSON gem you're using.

using bson-ruby 2.3. this hasn't changed in some time. Suggestions on a different bson implementation?

EVGA Longoria
Dec 25, 2005

Let's go exploring!

Thalagyrt posted:

I'd probably write that a bit differently. It's not a one liner, but easier to understand at a quick glance what's going on.

Ruby code:
def post(published = false)
  posts = ::Post.all
  posts = posts.published if published
  posts.find_by_slug_or_id(params[:id])
end
This does kind of sound like it might be code that belongs in an authorization module instead of in your controller though - are you returning published vs all posts depending on the user that's retrieving the data?

It's based on which endpoint they're using, /posts vs /posts/feed. Technically /posts is restricted for some things, but there's also a different in API representation and a few other things, so it's not as straightforward a authorization.

I'm still fairly new to Rails stuff, would we incur much performance penalty for going from All to .published? My impulse was that we would.

kayakyakr
Feb 16, 2004

Kayak is true

EVGA Longoria posted:

It's based on which endpoint they're using, /posts vs /posts/feed. Technically /posts is restricted for some things, but there's also a different in API representation and a few other things, so it's not as straightforward a authorization.

I'm still fairly new to Rails stuff, would we incur much performance penalty for going from All to .published? My impulse was that we would.

When I want to get ARQI started and want just a blank, get everything query, I like to do something like:

Ruby code:
Post.where('')
# or 
Post.where({})

Thalagyrt
Aug 10, 2006

EVGA Longoria posted:

It's based on which endpoint they're using, /posts vs /posts/feed. Technically /posts is restricted for some things, but there's also a different in API representation and a few other things, so it's not as straightforward a authorization.

I'm still fairly new to Rails stuff, would we incur much performance penalty for going from All to .published? My impulse was that we would.

Nope. Post.all.published and Post.published are nearly equivalent from a runtime perspective. Post.all simply returns a scope of all items which can be scoped further. Scopes aren't actually evaluated until you iterate over them (well, or a few other actions that necessitate making an actual SQL query). What they are is more like a set of parameters for a query that can be evaluated when needed to return a set of objects. In a lot of cases in Rails, that doesn't actually happen until you're displaying the returned records in a view.

EVGA Longoria
Dec 25, 2005

Let's go exploring!

Thalagyrt posted:

Nope. Post.all.published and Post.published are nearly equivalent from a runtime perspective. Post.all simply returns a scope of all items which can be scoped further. Scopes aren't actually evaluated until you iterate over them (well, or a few other actions that necessitate making an actual SQL query). What they are is more like a set of parameters for a query that can be evaluated when needed to return a set of objects. In a lot of cases in Rails, that doesn't actually happen until you're displaying the returned records in a view.

kayakyakr posted:

When I want to get ARQI started and want just a blank, get everything query, I like to do something like:

Ruby code:
Post.where('')
# or 
Post.where({})

Beautiful. Thanks guys, I'm going to work on this and clean it up a bit then.

Peristalsis
Apr 5, 2004
Move along.
We have a Ruby on Rails app that uses Devise for logging in and out. I'm responsible for a backend XML API. Currently, users can use cURL commands to log in, update data, etc. I now need to enable an explicit logout capabitlity through the API, and I'm having trouble making it work. Every page of the UI has a logout link, and I just want to use cULR to simulate clicking that link.

The logout sends a DELETE command to /users/sign_out, with an authenticity_token parameter. I've tried doing that with cURL, but I can't make it work. I've tried attaching the login cookie, and that didn't help. Do I need to get that authenticity token into the command data? If so, how do I do that? Would it be easier to make a new controller action and route to handle the POST/DELETE/whatever in code, so the user can just specify a URL?

kayakyakr
Feb 16, 2004

Kayak is true

Peristalsis posted:

We have a Ruby on Rails app that uses Devise for logging in and out. I'm responsible for a backend XML API. Currently, users can use cURL commands to log in, update data, etc. I now need to enable an explicit logout capabitlity through the API, and I'm having trouble making it work. Every page of the UI has a logout link, and I just want to use cULR to simulate clicking that link.

The logout sends a DELETE command to /users/sign_out, with an authenticity_token parameter. I've tried doing that with cURL, but I can't make it work. I've tried attaching the login cookie, and that didn't help. Do I need to get that authenticity token into the command data? If so, how do I do that? Would it be easier to make a new controller action and route to handle the POST/DELETE/whatever in code, so the user can just specify a URL?

Disable authenticity token for xhr calls. It makes no sense.

Peristalsis
Apr 5, 2004
Move along.

kayakyakr posted:

Disable authenticity token for xhr calls. It makes no sense.

I don't understand this response. I'm just looking for a command to log out via cURL.

The authenticity token already isn't being checked in the controller that handles the API XML inputs, but right now I'm just trying to mimic what the logout link does (i.e. it doesn't use my code, it goes through the Devise stuff).

Peristalsis fucked around with this message at 21:30 on Jul 31, 2014

Pollyanna
Mar 5, 2005

Milk's on them.


This isn't a Rails-specific question, but: I've been having trouble switching from a default layout.erb file to an equivalent layout.haml file in Middleman. I want to use HAML for my layouts since I'm trying to become familiar with it, so I decided to replace the default layout with a HAML version. Unfortunately, when I replace layout.erb with layout.haml, I get this error:

code:
NoMethodError at /
undefined method `force_encoding' for nil:NilClass

Ruby    /Library/Ruby/Gems/2.0.0/gems/tilt-1.4.1/lib/tilt/template.rb: in ensure in binary, line 289
Web GET localhost/

Traceback (innermost first)

/Library/Ruby/Gems/2.0.0/gems/tilt-1.4.1/lib/tilt/template.rb: in ensure in binary
      string.force_encoding(original_encoding)...
/Library/Ruby/Gems/2.0.0/gems/tilt-1.4.1/lib/tilt/template.rb: in binary
      string.force_encoding(original_encoding)...
/Library/Ruby/Gems/2.0.0/gems/tilt-1.4.1/lib/tilt/template.rb: in extract_magic_comment
      binary script do...
/Library/Ruby/Gems/2.0.0/gems/tilt-1.4.1/lib/tilt/template.rb: in extract_encoding
      extract_magic_comment(script) || script.encoding...
/Library/Ruby/Gems/2.0.0/gems/tilt-1.4.1/lib/tilt/template.rb: in precompiled
        template_encoding = extract_encoding(template)...
/Library/Ruby/Gems/2.0.0/gems/tilt-1.4.1/lib/tilt/erb.rb: in precompiled
        source, offset = super...
/Library/Ruby/Gems/2.0.0/gems/tilt-1.4.1/lib/tilt/erb.rb: in precompiled
        source, offset = super...
/Library/Ruby/Gems/2.0.0/gems/tilt-1.4.1/lib/tilt/template.rb: in compile_template_method
      source, offset = precompiled(locals)...
/Library/Ruby/Gems/2.0.0/gems/tilt-1.4.1/lib/tilt/template.rb: in compiled_method
        compile_template_method(locals_keys)...
/Library/Ruby/Gems/2.0.0/gems/tilt-1.4.1/lib/tilt/template.rb: in evaluate
      method = compiled_method(locals.keys)...
/Library/Ruby/Gems/2.0.0/gems/tilt-1.4.1/lib/tilt/template.rb: in render
      evaluate scope, locals || {}, &block...
/Library/Ruby/Gems/2.0.0/gems/middleman-core-3.3.3/lib/middleman-core/core_extensions/rendering.rb: in render_individual_file
          content = template.render(context, locs, &block)...
/Library/Ruby/Gems/2.0.0/gems/middleman-core-3.3.3/lib/middleman-core/core_extensions/rendering.rb: in render_template
            content = render_individual_file(layout_path, locs, opts, context) { content }...
/Library/Ruby/Gems/2.0.0/gems/middleman-core-3.3.3/lib/middleman-core/sitemap/resource.rb: in block in render
          app.render_template(source_file, locs, opts, blocks)...
/Library/Ruby/Gems/2.0.0/gems/activesupport-4.1.4/lib/active_support/notifications.rb: in instrument
          yield payload if block_given?...
/Library/Ruby/Gems/2.0.0/gems/middleman-core-3.3.3/lib/middleman-core/util.rb: in instrument
        ::ActiveSupport::Notifications.instrument(suffixed_name, payload, &block)...
/Library/Ruby/Gems/2.0.0/gems/middleman-core-3.3.3/lib/middleman-core/application.rb: in instrument
    delegate :instrument, to: ::Middleman::Util...
/Library/Ruby/Gems/2.0.0/gems/middleman-core-3.3.3/lib/middleman-core/sitemap/resource.rb: in instrument
      delegate :logger, :instrument, to: :app...
/Library/Ruby/Gems/2.0.0/gems/middleman-core-3.3.3/lib/middleman-core/sitemap/resource.rb: in render
        instrument 'render.resource', path: relative_source, destination_path: destination_path  do...
/Library/Ruby/Gems/2.0.0/gems/middleman-core-3.3.3/lib/middleman-core/core_extensions/request.rb: in process_request
            output = resource.render do...
/Library/Ruby/Gems/2.0.0/gems/middleman-core-3.3.3/lib/middleman-core/core_extensions/request.rb: in block in call!
            process_request(env, req, res)...
/Library/Ruby/Gems/2.0.0/gems/middleman-core-3.3.3/lib/middleman-core/core_extensions/request.rb: in catch
          catch(:halt) do...
/Library/Ruby/Gems/2.0.0/gems/middleman-core-3.3.3/lib/middleman-core/core_extensions/request.rb: in call!
          catch(:halt) do...
/Library/Ruby/Gems/2.0.0/gems/middleman-core-3.3.3/lib/middleman-core/core_extensions/request.rb: in call
          dup.call!(env)...
/Library/Ruby/Gems/2.0.0/gems/rack-1.5.2/lib/rack/builder.rb: in call
      to_app.call(env)...
/Library/Ruby/Gems/2.0.0/gems/rack-1.5.2/lib/rack/urlmap.rb: in block in call
        return app.call(env)...
/Library/Ruby/Gems/2.0.0/gems/rack-1.5.2/lib/rack/urlmap.rb: in each
      @mapping.each do |host, location, match, app|...
/Library/Ruby/Gems/2.0.0/gems/rack-1.5.2/lib/rack/urlmap.rb: in call
      @mapping.each do |host, location, match, app|...
/Library/Ruby/Gems/2.0.0/gems/rack-1.5.2/lib/rack/showexceptions.rb: in call
      @app.call(env)...
/Library/Ruby/Gems/2.0.0/gems/rack-1.5.2/lib/rack/head.rb: in call
    status, headers, body = @app.call(env)...
/Library/Ruby/Gems/2.0.0/gems/rack-1.5.2/lib/rack/lint.rb: in _call
      status, headers, @body = @app.call(env)...
/Library/Ruby/Gems/2.0.0/gems/rack-1.5.2/lib/rack/lint.rb: in call
      dup._call(env)...
/Library/Ruby/Gems/2.0.0/gems/rack-1.5.2/lib/rack/builder.rb: in call
      to_app.call(env)...
/Library/Ruby/Gems/2.0.0/gems/rack-1.5.2/lib/rack/handler/webrick.rb: in service
        status, headers, body = @app.call(env)...
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/webrick/httpserver.rb: in service
      si.service(req, res)...
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/webrick/httpserver.rb: in run
          server.service(req, res)...
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/webrick/server.rb: in block in start_thread
          block ? block.call(sock) : run(sock)
The ERb layout looks like this:

code:
<!doctype html>
<html>
  <head>
    <meta charset="utf-8">

    <!-- Always force latest IE rendering engine or request Chrome Frame -->
    <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">

    <!-- Use title if it's in the page YAML frontmatter -->
    <title><%= current_page.data.title || "The Middleman" %></title>

    <%= stylesheet_link_tag "normalize" %>
    <!-- <%= stylesheet_link_tag "normalize", "all" %> -->
    <%= javascript_include_tag  "all" %>
  </head>

  <body class="<%= page_classes %>">
    <%= partial "header" %>
    <%= yield %>
    <%= partial "footer" %>
  </body>
</html>
and the HAML layout looks like this:

code:
!!!
%html
  %head
    %title
      = current_page.data.title || "The Middleman"
    = stylesheet_link_tag "normalize", "all"
    = javascript_include_tag "all"
  %body
    = partial "header"
    = yield
    = partial "footer"
At first I thought the error was only ocurring because the HAML itself was broken, but even a very simple HAML file that defines only doctype, html, head, title and body tags breaks. I then suspected that only ERb files were being taken as layout files - which I confirmed by renaming layout.haml to layout.erb, which didn't cause an error but just displayed raw HAML code instead. Even keeping layout.erb and making a sublayout article_layout.haml causes a similar error when navigating to a blog article, while making an article_layout.erb works fine.

So, I suspect that Middleman just won't accept anything except ERb for layout files. I can't find a single thing about this in the documentation, though, and I've even seen examples that all have layout.haml files that work perfectly fine. Is mine just different? Is there a flag I need to set with middleman init in order to use HAML files for layouts? What's going on?

Siguy
Sep 15, 2010

10.0 10.0 10.0 10.0 10.0
I'm not sure if this would fix your problem, but in my experience with Middleman I've always had to write layout files with a .html file extension before the .haml extension. So instead of layout.haml I would have layout.html.haml.

Pollyanna
Mar 5, 2005

Milk's on them.


Turns out it's because I wasn't restarting the server whenever I changed layouts. :downs: Whoops. Thanks for the help, though, doing that clued me in to the real problem.

rohan
Mar 19, 2008

Look, if you had one shot
or one opportunity
To seize everything you ever wanted
in one moment
Would you capture it...
or just let it slip?


:siren:"THEIR":siren:




I'm trying to get back into using Rails, but I'm having a real issue getting SECRET_KEY_BASE to work with Rails 4.

I'm using rbenv and I've installed rbenv-vars. I have an ~/.rbenv/vars file with SECRET_KEY_BASE=etc, and when I run 'rbenv vars' it comes up with:

code:
# /home/deploy/.rbenv/vars
export SECRET_KEY_BASE='a59616cf464caead601ba9...
(the same thing works if I have an .rbenv-vars file in my application's root directory)

The /config/secrets.yml file contains:
code:
production:
  secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
and, yes, running 'echo $SECRET_KEY_BASE' gives the output you'd expect.

Is there another step I need to get this working? If it makes a difference I'm running Passenger through nginx. Thanks!

kayakyakr
Feb 16, 2004

Kayak is true
secrets.yml is intended to be a configuration file that you keep locally and do not check in to your git repository. Theoretically, you'd keep a production version on your deployed server.

So, you are seeing this issue in development or production?

Now for certain deployments (like heroku), that won't work and you do need to use ENV variables. My suggestion then would be use use the figaro gem which sets environment variables in a much more repeatable manner than .rbenv/vars.

Jaded Burnout
Jul 10, 2004


kayakyakr posted:

secrets.yml is intended to be a configuration file that you keep locally and do not check in to your git repository. Theoretically, you'd keep a production version on your deployed server.

Not quite; the new world order is to check these files into source control and use environment variables for production config everywhere, not just in Heroku. That's why the secrets.yml Rails 4 ships with has the erb bit. See http://12factor.net/config

Dirigible: there's nothing more you should have to do, so I'd suggest investigating your production setup to make sure the environment variables are available under the user your app runs as, not just you.

If it's development mode, this shouldn't matter at all as there should be a static secret for development.

kayakyakr
Feb 16, 2004

Kayak is true

Arachnamus posted:

Not quite; the new world order is to check these files into source control and use environment variables for production config everywhere, not just in Heroku. That's why the secrets.yml Rails 4 ships with has the erb bit. See http://12factor.net/config

Dirigible: there's nothing more you should have to do, so I'd suggest investigating your production setup to make sure the environment variables are available under the user your app runs as, not just you.

If it's development mode, this shouldn't matter at all as there should be a static secret for development.

Ah, so gems like figaro still have quite a bit of value.

Jaded Burnout
Jul 10, 2004


kayakyakr posted:

Ah, so gems like figaro still have quite a bit of value.

Yeah, so far I've mostly seen hand-rolled solutions which tend towards the crap (including mine).

Edit: In fact they reference the 12 factor app thing in their README.

necrotic
Aug 2, 2005
I owe my brother big time for this!

kayakyakr posted:

Ah, so gems like figaro still have quite a bit of value.

We use dotenv which reads a .env file if it exists. It works out really well, as we use foreman to export our Procfile for the init system which can handle .env files (either when invoking foreman directly, or using its export functionality).

.env files are also valid shell files, so you can use source to pull in any environment variables if you need them.

rohan
Mar 19, 2008

Look, if you had one shot
or one opportunity
To seize everything you ever wanted
in one moment
Would you capture it...
or just let it slip?


:siren:"THEIR":siren:




necrotic posted:

We use dotenv which reads a .env file if it exists. It works out really well, as we use foreman to export our Procfile for the init system which can handle .env files (either when invoking foreman directly, or using its export functionality).

.env files are also valid shell files, so you can use source to pull in any environment variables if you need them.
Thanks for this, I switched to using dotenv and everything seems to be working.

I did have to do a chown www-data:www-data on the file for it to work, though, so I suspect rbenv-vars would have also worked, per Arachnamus' post. I might give that a shot later, but I'm happy with how it works now.

Thalagyrt
Aug 10, 2006

Just wrote up a blog post about how I handle authorization in my business's client portal. Figure you guys might be interested. Spoiler: It's neither CanCan nor Pundit. :)

https://www.vnucleus.com/blog/2014/8/15/18-solving-complex-authorization-requirements-with-scopes

Slow News Day
Jul 4, 2007

I'm working on a hobby CRUD project that allows users to upload stuff and then view their submissions.

Instead of the typical "/resources/:id" path, where id is the primary key, I'm thinking about creating a random url-safe base 64 string and associating it with each record.

My challenge: I don't want to store these strings as part of the record. I would like the lookups to still be done using the primary key.

Can I use something like Redis for this? I was thinking of a structure like { string => id } whereby during every request I feed it the string (which is the request param), find the corresponding id, and then use that id to find and display the record.

Smol
Jun 1, 2011

Stat rosa pristina nomine, nomina nuda tenemus.

enraged_camel posted:

I'm working on a hobby CRUD project that allows users to upload stuff and then view their submissions.

Instead of the typical "/resources/:id" path, where id is the primary key, I'm thinking about creating a random url-safe base 64 string and associating it with each record.

My challenge: I don't want to store these strings as part of the record. I would like the lookups to still be done using the primary key.

Can I use something like Redis for this? I was thinking of a structure like { string => id } whereby during every request I feed it the string (which is the request param), find the corresponding id, and then use that id to find and display the record.

What's the use case?

The Journey Fraternity
Nov 25, 2003



I found this on the ground!

enraged_camel posted:

I'm working on a hobby CRUD project that allows users to upload stuff and then view their submissions.

Instead of the typical "/resources/:id" path, where id is the primary key, I'm thinking about creating a random url-safe base 64 string and associating it with each record.

My challenge: I don't want to store these strings as part of the record. I would like the lookups to still be done using the primary key.

Can I use something like Redis for this? I was thinking of a structure like { string => id } whereby during every request I feed it the string (which is the request param), find the corresponding id, and then use that id to find and display the record.

That would work, yes, but like Smol in curious what the use case is here. Why don't you want that slug in your database?

Adbot
ADBOT LOVES YOU

kayakyakr
Feb 16, 2004

Kayak is true

enraged_camel posted:

I'm working on a hobby CRUD project that allows users to upload stuff and then view their submissions.

Instead of the typical "/resources/:id" path, where id is the primary key, I'm thinking about creating a random url-safe base 64 string and associating it with each record.

My challenge: I don't want to store these strings as part of the record. I would like the lookups to still be done using the primary key.

Can I use something like Redis for this? I was thinking of a structure like { string => id } whereby during every request I feed it the string (which is the request param), find the corresponding id, and then use that id to find and display the record.

I would make that part of your current database. No reason to add additional complexity of bringing in another 3rd party application in a hobby app.

Take a look at Friendly ID as another option.

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