What's Sinatra's equivalent of Rails' redirect_to method? I need to follow a Post/Redirect/Get flow for a form submission whilst preserving the instance variables that are passed to my view.
Does the redirect method preserve them? (I'm at work at the moment and don't have access to Sinatra to try for myself.)
I have controller with action new, and I want it to create ActiveRecord::Base descendant object, and write it into database (without showing it to user).
def new
active_order = current_user.orders.find {|o| o.status > 0 }
active_order = Order.new if active_order.nil?
(...)
end
Order.new creates local object, but my question is -- how to make Rails to fill it with default values and write to database?
Here's one that stumped me for a while, though in retrospect it should have been obvious. I was getting the error message
NoMethodError: undefined method `constantize' for 0:Fixnum
when accessing a model through a polymorphic association. Turns out the table on the belongs_to side of the association had an integer type column instead of a string.
Easily fixed, but it seems like Rails ought to raise an error in this situation -- instead it happily adds the row with 0 in the type column.
I have a file "Gemfile" in my Rails project root.
It says source :gemcutter in the very first line.
Do I still need the line? If I'm correct, http://rubygems.org/ is the official source, and Gem Cutter was merged into rubygems.org.
I'm writing a small ruby daemon that I am hoping will do the following:
Check if a specific directory has files (in this case, .yml files)
If so, take the first file (numerically sorted preferrably), and parse into a hash
Do a 'yield', with this hash as the argument
What I have right now is like:
loop do
get_next_in_queue { |s| THINGS }
end
def get_next_in_queue
queue_dir = Dir[File.dirname(__FILE__)+'/../queue']
info = YAML::load_file(queue_dir[0]) #not sure if this works or not
yield info
end
I'd like to make the yield conditional if possible, so it only happens if a file is actually found. Thanks!
what is the best way to implement real time updates in rubyonrails using Node.JS? It would be great to hear either real examples or your thoughts on alternative solutions
I've got a parser written using ruby's standard StringScanner. It would be nice if I could use it on streaming files. Is there an equivalent to StringScanner that doesn't require me to load the whole string into memory?
Does such a thing exist?
Building a Rails based web application where users can upload an audio file of them speaking that then needs to be compared to another audio file for the purposes of determining how similar to voices are. Ideally I'd like to simply get a response that gives me a score of how similar they are in terms of percentage (e.g. 75% similar etc...).
Anyone have any ideas?
Thanks
We are in web era standalone applications are almost gone everyone wants their internet application to run inside browser, programming languages like Ruby, Python and scala are becoming more and more mainstream.
Sometimes I wonder what these programming language offer which make them top choice of IT companies, if I plan to become a freelance web developer is it worth learning C# or Java. I read beginner's book for both of them, but to master any of them require some time investment.
Hi,
I jus created a rails application. I created a model using ruby script/generate model Article
next i edited the my articles.rb file by adding these lines in self.up method
def self.up
create_table :articles do |t|
t.string :title
t.text :body
t.string :published_at
t.timestamps
end
end
Now i ran rake db:migrate . But migrate does not work, it simply does no print anything. Anyone knows where i am going wrong?
I'm working with an older version of Rails and ActiveRecord that does not have the :primary_key option in has_many relationships of more recent ActiveRecord versions and unfortunately I don't get to control when we upgrade. Is there any way to hack this solution via :conditions or :finder_sql options?
I have a sinatra service foo.rb
To run it I on a different port I would type "ruby foo.rb -p 5000".
While the program is running I would like to be able to get the port (in this case 5000) and other information. Is there a way to get info like this while the app is running?
Thanks
Getting the customer's to_s method by looping through
Is there a Ruby idiom to write the code in 1 line (or shorter than 3 lines of code)?
def method
string = ""
@customers.each { |customer| string += customer.to_s + "\n" }
string
end
RubyParser.new.parse "1+1"
s(:call, s(:lit, 1), :+, s(:array, s(:lit, 1)))
Above code is from this link
Why there is array after + in the Sexp. I am just trying to learn ruby parser and the whole AST thing. I have been programming for a while but have no formal education in computer science. So do point to good article which explains AST etc. Please no dragon book. I tried couple of times but couldn't understand much of that book
from title, I create Rails application and use Paperclip to manage file upload but I don't want to store file in local machine, I want to store file in other machine via ftp.
Paperclip provide to store file in S3 storage, I want to make paperclip can do like S3 but via ftp.
Help me please.
I am trying to test a controller method with the following code:
it "should set an approved_at date and email the campaign's client" do
@campaign = Campaign.create(valid_attributes)
post :approve, id: @campaign.id.to_s
@campaign.reload
@campaign.approved_at.should_not be(nil)
end
However, when I run this test, I get the following error:
Failure/Error: @campaign.reload
ActiveRecord::RecordNotFound:
Couldn't find Campaign without an ID
When I run the analagous lines in the rails console, the reload works and the value is set as I need it to be. Why isn't reload working for me when I run the code in an rspec test?
Hi all !
I'm using the Sequel (Taps) ruby gem for a remote backup of my production database (PostgreSQL).
I wonder if storing that backup with SQLite is a good solution.
What's your feeling ?
Thx !
i want to put validation in rubyonrails that my username should not contain special characters like !@#$%^*()_-+=/<?:'";.
Please tell me how can i implement it in my code.
Hi,
I'm developing a Rails application with Rspec for unit testing.
Weeks ago, Rspec used to migrate the database to the last version automatically when executing 'rake spec', but now it doesn't do it automatically, I have to implement everything for myself.
This happens in test environment, because my development data doesn't desappear.
Is my fault? I didn't change anything, I think :)
Thanks in advance.
When generating specs with :
rails g controller Home index
A spec is generated with the older object.should syntax
require 'spec_helper'
describe HomeController do
describe "GET 'index'" do
it "returns http success" do
get 'index'
response.should be_success
end
end
end
Is it possible to configure the generator to use the expect syntax instead?
Desired output:
require 'spec_helper'
describe HomeController do
describe "GET 'index'" do
it "returns http success" do
get 'index'
expect(response).to be_success
end
end
end
in config/application.rb:
config.generators do |g|
g.test_framework :rspec, fixture: true
g.fixture_replacement :factory_girl, dir: 'spec/factories'
g.view_specs false
g.stylesheets = false
g.javascripts = false
end
I want alternative rows in my table to be shaded. what is the best way to do this, javascript, rails?
Today, i do a simple <% num % 2%, but this is such a common operation that i think there should be a smarter way to do it
I've been reading all kinds of tutorials on how to deploy rails apps on heroku from windows.
I've tried installing git, heroku gem, generating ssh keys and setting paths and everything...
I get either public key error (without putty) or fatal no auth found (with putty)...
I am using rubyonrails 2.3.5 .
I have user instance. On run time depending on certain conditions I want to add an after_save callback just for this instance.
The other issue is that this after_save should take a block or a proc as parameter.
What I want is something like this. This is psuedo code.
user = User.first
proc = Proc.new do
puts 'this is foo'
end
user.after_save proc
Hi All,
I am calling a controller method to convert a video file. This process
is called using background_fu job. When the function tries to create a
new folder in rails root it gives error i.e. Permission denied. The
function performs well if not called in background job process.
Can any one point out what can be the trouble.
Thanks in advance,
Anubhaw