Search Results

Search found 67 results on 3 pages for 'randombits'.

Page 3/3 | < Previous Page | 1 2 3 

  • Ruby 1.9: turn these 4 arrays into hash of key/value pairs

    - by randombits
    I have four arrays that are coming in from the client. Let's say that there is an array of names, birth dates, favorite color and location. The idea is I want a hash later where each name will have a hash with respective attributes: Example date coming from the client: [name0, name1, name2, name3] [loc0, loc1] [favcololor0, favcolor1] [bd0, bd1, bd2, bd3, bd4, bd5] Output I'd like to achieve: name0 => { location => loc0, favcolor => favcolor0, bd => bd0 } name1 => { location => loc1, favcolor => favcolor1, bd => bd1 } name2 => { location => nil, favcolor => nil, bd => bd2 } name3 => { location => nil, favcolor => nil, bd => bd3 } I want to have an array at the end of the day where I can iterate and work on each particular person hash. There need not be an equivalent number of values in each array. Meaning, names are required.. and I might receive 5 of them, but I only might receive 3 birth dates, 2 favorite colors and 1 location. Every missing value will result in a nil. How does one make that kind of data structure with Ruby 1.9?

    Read the article

  • Rails 2.x provide meaningful error message with http basic authentication

    - by randombits
    I'm using basic http authentication in my rails app via the following code: class ApplicationController < ActionController::Base helper :all # include all helpers, all the time before_filter :authenticate private def authenticate authenticate_or_request_with_http_basic do |username, password| if username.nil? || password.nil? render :inline => %(xml.instruct! :xml, :version => "1.0", :encoding => "UTF-8" xml.errors do xml.error('Could not authenticate you.') end), :type => :builder, :status => 401 end end end end The problem is, if you do a curl http://127.0.0.1:3000/foo/1.xml without providing the -u username:password flag, you get a dead beat response like this: HTTP/1.1 401 Unauthorized Cache-Control: no-cache WWW-Authenticate: Basic realm="Foo" X-Runtime: 1 Content-Type: text/html; charset=utf-8 Content-Length: 27 Server: WEBrick/1.3.1 (Ruby/1.9.1/2010-01-10) Date: Thu, 03 Jun 2010 03:09:18 GMT Connection: Keep-Alive HTTP Basic: Access denied. Is it possible at all to render the inline XML I have above in the event a username and password is not provided by the user?

    Read the article

  • Rails: How do I validate against this code that I put into the lib/ directory?

    - by randombits
    Having a bit of difficulty finding out the proper way to mix in code that I put into the lib/ directory for Rails 2.3.5. I have several models that require phone validation. I had at least three models that used the same code, so I wanted to keep things DRY and moved it out to the lib/ directory. I used to have code like this in each model: validate :phone_is_valid Then I'd have a phone_is_valid method in the model: protected def phone_is_valid # process a bunch of logic errors.add_to_base("invalid phone") if validation failed end I moved this code out into lib/phones/ and in lib/phones I have lib/phones/phone_validation.rb, and in there I copy pasted the phone_is_valid method. My question is, how do I mix this into all of my models now? And does my validate :phone_is_valid method remain the same or does that change? I want to make sure that the errors.add_to_base method continues to function as it did before while keeping everything DRY. I also created another file in lib/phones/ called lib/phones/phone_normalize.rb. Again, many models need the value input by the user to be normalized. Meaning turn (555) 222-1212 to 5552221212 or something similar. Can I invoke that simply by invoking Phones::Phone_Normalize::normalize_method(number)? I suppose I'm confused on the following: How to use the lib directory for validation How to use the lib directory for commonly shared methods that return values

    Read the article

  • Core Data strategy using in memory cache, or no core data at all?

    - by randombits
    I have a user interface where the user can check off a bunch of items from a tableview, almost like a todo list. The items are populated from a Core Data stack. I need to be able to take all of the items they're clicking through and put them into a "temporary" shopping cart. Once they're in the shopping cart, users can go through the list and remove the items, or just submit them to a server. The thing is, the selected items are temporary just like an internet based shopping cart. It's nothing something that gets persisted once the application closes. Once the view is no longer in display, I can assume that the shopping cart is safe to discard. What's the best way to approach this? Since the user is essentially clicking on instances that map back to a Core Data entity .. should I setup a different persistence store such as in memory and add that store to my managed object context?

    Read the article

  • Ruby on Rails: Is there a way to tell what fields failed validation in ActiveRecord?

    - by randombits
    I'm attempting to create an XML builder file that tells a user to know exactly what fields failed validation in the output. I also want to display their input back to them, so that requires me figuring out which fields failed validation. Meaning if someone fails on creating a new user resource, I want to display XML that's meaningful (Besides a meaningful HTTP status number) such as: <errors> <user> <email>bad@email: Invalid email format</email> </user> <errors> The above is tough to do in an XML builder file without knowing what field failed. And if I just iterate over error messages, I won't know how to prob my @user object to get the value that the user supplied.

    Read the article

  • C pointer initialization and dereferencing, what's wrong here?

    - by randombits
    This should be super simple, but I'm not sure why the compiler is complaining here. #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { int *n = 5; printf ("n: %d", *n); exit(0); } Getting the following complaints: foo.c: In function ‘main’: foo.c:6: warning: initialization makes pointer from integer without a cast I just want to print the value that the pointer n references. I'm dereferencing it in the printf() statement and I get a segmentation fault. Compiling this with gcc -o foo foo.c.

    Read the article

  • Rails 2.3.5: How does one add an error when it doesn't make sense to put it in a validation?

    - by randombits
    I recently was trying to add errors.add_to_base code in the middle of some model logic and was wondering why it wasn't showing up in my view that was iterating over all errors. I then ran across this e-mail which explains why: http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/e045ec1dead1ff06?pli=1 The question is then, how does one add errors with add_to_base if it doesn't make sense to put them into a validate method? I have some complex logic. The model needs to talk to a has_many relationship which has its own relationships that go through a myriad of conditionals to figure out if a request makes sense. It's nothing that can be tied to a validate method easily. How does one add errors then accordingly?

    Read the article

  • Rails 2.3.5: How does one access code inside of lib/directory/file.rb?

    - by randombits
    I created a file so I can share a method amongst many models in lib/foo/bar_woo.rb. Inside of bar_woo.rb I defined the following: module BarWoo def hello puts "hello" end end Then in my model I'm doing something like: def MyModel < ActiveRecord::Base include Foo::BarWoo def some_method Foo::BarWoo.hello end end The interpreter is complaining that it expected bar_woo.rb to define Foo::BarWoo. The Agile Web Development with Rails book states that if files contain classes or modules and the files are named using the lowercase form of the class or module name, then Rails will load the file automatically. I didn't require it because of this.

    Read the article

  • Rails 2.3.5: How to handle this type of validation

    - by randombits
    The use case is simple. I allow users to enter in an expiration field which needs to be between 1 and 15 into a form. The model takes that number and converts it into a datetime (such as adding 15 days from today) and stores it in the database. What's the correct way to actually validate that though? Do I validate against the datetime format that gets persisted in the database or the select box (1..15) that the user gets to pick through the form? I want to be able to validate that the user is putting in 1..15.. How is this done with ActiveRecord validation in Rails 2.3.5?

    Read the article

  • Ruby on Rails: temporarily update an attribute into cache without saving it?

    - by randombits
    I have a bit of code that depicts this hypothetical setup below. A class Foo which contains many Bars. Bar belongs to one and only one Foo. At some point, Foo can do a finite loop that lapses 2+ iterations. In that loop, something like the following happens: bar = Bar.find_where_in_use_is_zero bar.in_use = 1 Basically what find_where_in_use_is_zero does something like this in as far as SQL goes: SELECT * from bars WHERE in_use = 0 Now the problem I'm facing is that I cannot run the following line of code after bar.in_use =1 is invoked: bar.save The reason is clear, I'm still looping and the new Foo hasn't been created, so we don't have a foo_id to put into bars.foo_id. Even if I set to allow foo_id to be NULL, we have a problem where one of the bars can fail validation and the existing one was saved to the database. In my application, that doesn't work. The entire request is atomic, either all succeeds or fails together. What happens next, is that in my loop, I have the potential to select the same exact bar that I did on a previous iteration of the loop since the in_use flag will not be set to 1 until @foo.save is called. Is there anyway to work around this condition and temporarily set the in_use attribute to 1 for subsequent iterations of the loop so that I retrieve an available bar instance?

    Read the article

< Previous Page | 1 2 3