ruby-on-rails-3

Can multiple objects be inserted with the << operator in Rails 3.1?

拟墨画扇 提交于 2019-12-22 11:34:00
问题 Could I write the following ... raw_data.categories.each do |category| obj.categories << category end As the following instead? ... obj.categories << raw_data.categories 回答1: obj.categories |= raw_data.categories 回答2: Take a look at Array#<< and Array#push . Array#<< takes one which is appended in place to given array. For example: irb> array = %w[ a b c ] # => ["a", "b", "c"] irb> array << 'd' # => ["a", "b", "c", "d"] however, if you pass an array, you'll be surprised at the result irb>

Can Rails' javascript_include_tag ignore previously-loaded scripts?

旧时模样 提交于 2019-12-22 11:33:49
问题 I'm using this line: = javascript_include_tag :all, :recursive => true, :cache => true in the footer of a Rails app to do the following: Load all the scripts under public/javascripts recursively In production, lump them all into one file (the :cache part) However, I want to load one of these scripts in the head, because some inline JS depends on it. Is there an option for javascript_include_tag to exclude files that have already been loaded? I know it doesn't do this by default, because right

Ruby on Rails with REST API

前提是你 提交于 2019-12-22 11:28:30
问题 I am new to the Ruby on Rails thing, and while I like the organization and standards provided, I am a little confused on how to make rails work for me in this specific case. I have a Webservice that I want to use my rails application with. Making a direct connection to the database would be nice and provide me instantly with the models I need to make my Rails application work. However, I would have to replicate all of the logic provided by the webservice(which isn't trivial). If I didn't make

How to select the latest record of each hour in a day

妖精的绣舞 提交于 2019-12-22 11:19:13
问题 I want to select the latest record of each hour in a given date, based on the datetime column 'reading_on'. I have executed the below query hourly_max = InverterReading .where("DATE(reading_on) = ? AND imei = ?", Date.today, "770000000000126") .group("HOUR(reading_on)") .having("max(HOUR(reading_on))") hourly_max.group_by(&:id).each { |k,v| puts v.last.reading_on } In the above query I am not getting the required result. What is the proper way to select the latest record of each hour in a day

Rails nested resource with respond_with destroy action

岁酱吖の 提交于 2019-12-22 11:11:09
问题 What is the appropriate respond_with line for a nested resources destroy action? My routes: resources :vendors do resources :products, :except => [:index] end Product#destroy (note @vendor and @product are found with a before_filter which is omitted here) def destroy @product.destroy respond_with @vendor, @product end According to my functional tests, this is returning /vendors/X/products/X and not /vendors/X Should I change it to just responed_to @vendor ? 回答1: I believe Rails is smart

Serve a static JSON object file in rails

我只是一个虾纸丫 提交于 2019-12-22 11:06:32
问题 How do I serve a static JSON object from a file in rails? (I want to access it in an ajax call)? What is the best method? 回答1: Just place what you want to render in a variable, then use render :json => variable There are sensible defaults for lists, dicts, etc... See this: http://guides.rubyonrails.org/layouts_and_rendering.html Item: "2.2.8 Rendering JSON" 来源: https://stackoverflow.com/questions/9348712/serve-a-static-json-object-file-in-rails

Rails form with ajax and non-ajax buttons

為{幸葍}努か 提交于 2019-12-22 10:59:16
问题 I have a form_for an @object with two buttons. While the first button renders the 'show action', the second button renders the same form again. So I'd like the latter to be ajax-handled. Is it possible to have a non-ajax button and an ajax button in the same form or do I have to change strategy? Maybe I need a form_for with 'remote: true' so that both the buttons are ajax but then, how would I manage the first button to render the proper 'show view'? Or maybe the only real solution is to have

auth-hmac functional testing problem

烂漫一生 提交于 2019-12-22 10:54:44
问题 I want to use this gem in my api application https://github.com/seangeo/auth-hmac/ I have a question about creating tests for request authentification. I want to sign request with hmac but rails controller has no http headers after next code def setup #load from fixture @client = clients(:client_2) end def sign_valid_request(request,client) auth_hmac = AuthHMAC.new(client.key => client.secret ) auth_hmac.sign!(request,client.key) request end def test_response_client_xml @request = sign_valid

delayed_job: how to force processing of failed jobs

落爺英雄遲暮 提交于 2019-12-22 10:53:33
问题 I am maintaining a rails app, on which i'm running the delayed_job gem for sending emails. I just noticed that all my delayed jobs've been failing the last few days due to a bug in the application. Now the bug is fixed and I want to process the jobs asap, but they are with already too many failed attempts, and the worker pulls them from the database with a huge delay. I've tried by updating the delayed_jobs table and set the number of attempts to a smaller number and the run_at attribute to

Redirect on catching an exception in a method in the model

青春壹個敷衍的年華 提交于 2019-12-22 10:53:29
问题 I am using Authlogic-connect to connect various service providers. There is a method in user.rb def complete_oauth_transaction token = token_class.new(oauth_token_and_secret) old_token = token_class.find_by_key_or_token(token.key, token.token) token = old_token if old_token if has_token?(oauth_provider) self.errors.add(:tokens, "you have already created an account using your #{token_class.service_name} account, so it") else self.access_tokens << token end end When a service provider is