ruby-on-rails-3

Existing Rails model without fetching it from the database

一个人想着一个人 提交于 2019-12-23 02:28:42
问题 Does anyone know if its possible to create a model instance and apply the ID and any other attributes without having to load it from the database? I tried doing this, but the associations are not fetched from the database :( Any ideas? EDIT What I want to accomplish is simply this: Fetch an existing record from the database. Store as "hashed" output of the record into redis or some other memory store. Next time when that record is fetched, fetch the cached store first and if it is not found

Rails 3, create a new route for every resource

六眼飞鱼酱① 提交于 2019-12-23 02:22:40
问题 In a project I'm working on I'd like to add the same route for multiple resources. I know I can do this resources :one do collection do post 'common_action' end end resources :two do collection do post 'common_action' end end I have at least 10 different resources which all need the same route, as each controller will have the same action. Is there a way to define this less repetitively? 回答1: %w(one two three four etc).each do |r| resources r do collection do post 'common_action' end end end

Alternative to prepend/append_view_path in Rails 3?

◇◆丶佛笑我妖孽 提交于 2019-12-23 02:04:45
问题 I'm trying to add another directory to the paths that Rails looks in to find views, and I found a working solution in the methods ActionController::Base.prepend_view_path and ActionController::Base.append_view_path, but they are both marked as deprecated in Rails 3. Is there an alternative that I can use? http://apidock.com/rails/ActionController/Base/prepend_view_path http://apidock.com/rails/ActionController/Base/append_view_path 回答1: The ActionController::Base.append/prepend_view_paths

Traversing directories and reading from files in ruby on rails

a 夏天 提交于 2019-12-23 01:57:19
问题 I'm having some trouble figuring out how to 1) traverse a directory and 2) taking each file (.txt) and saving it as a string. I'm obviously pretty new to both ruby and rails. I know that I could save the file with f=File.open("/path/*.txt") and then output it with puts f.read but I would rather save it as a string, not .txt, and dont know how to do this for each file. Thanks! 回答1: Jake's answer is good enough, but each_with_object will make it slightly shorter. I also made it recursive. def

Factory Girl - Why are Records being continually created?

╄→гoц情女王★ 提交于 2019-12-23 01:45:42
问题 I'm new to factory girl. What I'm trying to do is create 2 users, which belong to a group, joined by the permission model. Here's what I have. When I run this one rspec, it creates more than 2 users, 4+. Why? thanks factories.rb: require 'factory_girl' Factory.define :user do |f| f.sequence(:fname) { |n| "fname#{n}" } f.sequence(:lname) { |n| "lname#{n}" } f.sequence(:email) { |n| "email#{n}@google.com" } f.password "password" f.password_confirmation { |u| u.password } f.invitation_code

How to write an image to filesystem in rails 3.1.1?

落花浮王杯 提交于 2019-12-23 01:45:16
问题 I feel like I should be really close, but I can't get this to work just quite right. I'm sending an image using filetransfer from my phonegap app to my rails app (3.1.1 ruby 1.8.7 btw). I want to write the file to the filesystem. This is the params hash: Parameters: {"file"=>#<ActionDispatch::Http::UploadedFile:0x7f1e2baa94e8 @tempfile=#<File:/tmp/RackMultipart20120607-5707-owzii5-0>, @headers="Content-Disposition: form-data; name=\"file\"; filename=\"image.jpg\"\r\nContent-Type: image/jpeg\r

Creating a module for raising class-specific errors

六眼飞鱼酱① 提交于 2019-12-23 01:41:38
问题 In my rails projects, I often use this sort of behavior in my classes and models: class Whatever class WhateverError < StandardError; end def initialize(params={}) raise WhateverError.new("Bad params: #{params}") if condition # actual class code to follow end end The trouble is, this is both hugely repetitive and fairly verbose. I'd love it if I could just do this whenever I need to raise a class-specific error: class ErrorRaiser include ClassErrors def initialize(params={}) error("Bad params

Uploading multiple files in rails 3

非 Y 不嫁゛ 提交于 2019-12-23 01:41:21
问题 I'm working on uploading multiple files but I'm not passing the params correctly between the form and the controller - not quite sure what I'm missing. Form is pretty simple: <%= form_tag({:controller => :admins, :action => :upload_json}, {:multipart => true, :method => :post, :html => { :class => "form-horizontal"}}) do %> <fieldset> <legend>Data</legend> <ol class="field-list"> <li> <label for="data-file">JSON File</label> <%= file_field_tag 'jsonfileupload', multiple: true %> </li> </ol>

Using Rails/AR to generate this complex query

心不动则不痛 提交于 2019-12-23 01:40:11
问题 In trying to solve a grouping and ordering problem (original question here: "Complex" grouping and indexing in rails?), I got a SQL query that will fetch the right records the right way. My question now is: how do I generate this SQL query using Rails/AR synthax? The SQL-query is as follows: SELECT u.id as owner_id, u.name as owner_name, t.id, t.due_date FROM users u INNER JOIN tasks m ON u.id = m.owner_id INNER JOIN tasks t ON u.id = t.owner_id GROUP BY u.id, u.name, t.id, t.due_date ORDER

validate presence of url in html text

大城市里の小女人 提交于 2019-12-23 01:37:09
问题 I would like detect and filter if html text sent from a form, contains url or urls. For example, i send from a form this html: RESOURCES<br></u></b><a target="_blank" rel="nofollow" href="http://stackoverflow.com/users/778094/hyperrjas">http://stackoverflow.com/users/778094/hyperrjas</a> <br><a target="_blank" rel="nofollow" href="https://github.com/hyperrjas">https://github.com/hyperrjas</a> <br><a target="_blank" rel="nofollow" href="http://www.linkedin.com/pub/juan-ardila-serrano/11/2a7/62