I have two Rails applications (lets call them APP-1 and APP-2), each of them has a dependancy on a third Rails application (APP-3).
I would like to be able to run the tests for APP-1 and APP-2 in parallel on my CI server. The problem is, both need to start up APP-3 and write to a DB via the APP-3. This causes conflicts and failures if the tests are run in parallel.
My idea for a solution is for APP-1 and APP-2 to each start their own instance of APP-3 and to have each instance point to a different DB. Is there a way to dynamically set the DB in the database.yml of APP-3 so that it connects to a different DB depending on which APP starts it up?
FYI. APP-1 and APP-2 currently start APP-3 via rake tasks.
I have a User object, that is related to a Post object via two different association paths:
Post --(has_many)-- comments --(belongs to)-- writer (of type User)
Post --(belongs to)-- writer (of type User)
Say the following hold:
user1.name == "Bill"
post1.comments[1].writer == user1
post1.writer == user1
Now when I retrieve the post1 and its comments from the database and I update post1.comments[1].writer like so:
post1.comments[1].writer.name = "John"
I would expect post1.writer to equal "John" too. But it doesn't! It still equals "Bill".
So there seems to be some caching going on, but the kind I would not expect. I would expect Rails to be clever enough to load exactly one instance of the user with name "Bill"; instead is appears to load two individual ones: one for each association path.
Can someone explain how this works exactly and how I am to handle these types of situations the "Rails way"?
In a standard app, I have this line in my production.rb, which creates endpoints for non-default precompiled assets:
config.assets.precompile += %w( mobile.css )
My rails engine is a standard Sinatra app. It has its own assets.
When on development, these assets are served fine, presumably the web requests are handled by rails and sprockets. On production I'm getting 404s on the assets, and think I have to manually tell sprockets to provide the files. How can this be done without tightly linking?
It isin't evident how to set up env-specific initializers for engines. Is this done? Not only, for example, is config/development.rb within the engine not loaded, but there's no way to get the application class itself without knowing its name, in order to modify configuration.
And even if there was, it seems that having any engine able to reconfigure the main app would be very bad idea.
So maybe its better to let assets handling be done by sinatra itself? Or another instance of sprockets for the engine? How do other engines handle this?
Hi,
I have the following index action:
class ExpensesController < ApplicationController
def index()
@expenses = Expense.all
end
end
I want to mock the call to all in a functional test. I am using flexmock and have written the following test:
require 'test_helper'
require 'flexmock'
require 'flexmock/test_unit'
class ExpensesControllerTest < ActionController::TestCase
test "should render index" do
flexmock(Expense).should_receive(:all).and_return([])
get :index
assert_response :success
assert_template :index
assert_equal [], assigns(:presentations)
end
end
The problem is the the last assertion fais with the following error message:
<[] expected but was nil
I am confused what I am doing wrong. Should this not work?
Cheers
Paul
Trying to create an atom feed in Rails 3. When i refresh my browser i see basic XML, not the Atom feed im looking for.
class PostsController < ApplicationController
# GET /posts
# GET /posts.xml
def index
@posts = Post.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @posts }
format.atom
end
end
index.atom.builder
atom_feed do |feed|
feed.title "twoconsortium feed"
@posts.each do |post|
feed.entry(post) do |entry|
entry.title post.title
entry.content post.text
end
end
end
localhost:3000/posts.atom looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
<id>tag:localhost,2005:/posts</id>
<link rel="alternate" type="text/html" href="http://localhost:3000"/>
<link rel="self" type="application/atom+xml" href="http://localhost:3000/posts.atom"/>
<title>my feed</title>
<entry>
<id>tag:localhost,2005:Post/1</id>
<published>2012-03-27T18:26:13Z</published>
<updated>2012-03-27T18:26:13Z</updated>
<link rel="alternate" type="text/html" href="http://localhost:3000/posts/1"/>
<title>First post</title>
<content>good stuff</content>
</entry>
<entry>
<id>tag:localhost,2005:Post/2</id>
<published>2012-03-27T19:51:18Z</published>
<updated>2012-03-27T19:51:18Z</updated>
<link rel="alternate" type="text/html" href="http://localhost:3000/posts/2"/>
<title>Second post</title>
<content>its that second post type stuff</content>
</entry>
</feed>
In almost every app that I build I create some kind of user log table to log various activities that my actual USERS (not visitors, but someone with an account) perform on the site. This is primarily used for customer service issues to allow me to pull up a record of the pages and actions that a user has visited.
The downside to this is the size of the UserLogs table. It gets immense. I'm not sure if it is common practice or not for others to log INDIVIDUAL (not aggregate like Google Analytics) user behavior to a database, but if it is I'm wondering if any form of a SaaS exists to help offload this task? I essentially need a RESTful API that lets me store and retrieve individual user activity quickly and securely.
Anyone know of any or am I the only one who has this issue?
I have a model that has an arbitrary number of children entities. For simplicity lets call the entities Orders and Items. I would like to have a create Orders form where I input the order information, as well as add as many items as I want. If I click the "Add another item" button, a new set of form elements will be added to input the new data, amounts, etc..
I could hack this out in pure javascript, but I'm pretty sure there has to be a more magical, railsish way to do it, maybe with a partial view or something. I'm just a little too new to rails to know what it is.
What is the best way to dynamically add the new form elements, and then to access them in the create controller?
I'm trying to implement single-column regionalization for a Rails application and I'm running into some major headaches with a complex SQL need. For this system, a region can be represented by a country code (e.g. us) a continent code that is uppercase (e.g. NA) or it can be NULL indicating the "default" information. I need to group these items by some relevant information such as a foreign key (we'll call it external_id).
Given a country and its continent, I need to be able to select only the most specific region available. So if records exist with the country code, I select them. If, not I want a records with the continent code. If not that, I want records with a NULL code so I can receive the default values.
So far I've figured that I may be able to use a generated CASE statement to get an arbitrary sort order. Something like this:
SELECT *, CASE region
WHEN 'us' THEN 1
WHEN 'NA' THEN 2
ELSE 3
END AS region_sort
FROM my_table
WHERE region IN ('us','NA') OR region IS NULL
GROUP BY external_id
ORDER BY region_sort
The problem is that without an aggregate function the actual data returned by the GROUP BY for a given row seems to be untameable. How can I massage this query to make it return only the first record of the region_sort ordered groups?
Hi, I am trying to get all users that are updated maximum 90 seconds ago:
User.find(:all, :include => { :core => :image },
:conditions => ["updated_at > ?", Time.now - 90.seconds] )
But it doesn't work.
why?
how can i do?
thanks
Hello!
I want to allow the users of a web app that I'm building to write their own CSS in order to customize their profile page.
However I am aware of this opening up for many security risks, i e background: url('javascript:alert("Got your cookies! " + document.cookies').
Hence I am looking for a solution to sanitize the CSS while still allowing as much CSS functionality as possible for my users.
So my questions if anyone anyone knows of a gem or a plugin to handles this? I've googled my brains out already so any tips would be really appreciated!
I need to have multiple submit buttons.
I have a form which creates an instance of Contact_Call.
One button creates it as normal.
The other button creates it but needs to have a different :attribute value from the default, and it also needs to set the attribute on a different, but related model used in the controller.
How do I do that? I can't change the route, so is there a way to send a different variable that gets picked up by [:params]?
And if I do then, what do I do in the controller, set up a case statement?
I am developing an application that resides on a public host but whose source I must keep in a Git repository behind a corporate firewall. I'm getting very tired of the slowness of deploying via scp (copying the whole repository and shipping it over SSH on each deploy) and would like to have the remote host simply do a git pull to update. The problem is that the firewall prohibits incoming SSH connections.
Would it be possible for me to set up an SSH tunnel from my computer to the deployment computer and use my repository as the source for the git pull? After all, git is distributed, so my copy is just as valid a repository as the central one. If this is possible, what would the tunnel command and the Capistrano configuration be?
I think the tunnel will look something like
ssh -R something:deployserver.com:something [email protected]
When signing up for an account on one of my apps, we need to store the time zone is in. We're using the time zone selector, which is fine, but I'd like to set the default value to something that it likely the user's current time zone.
Is there an easy way, either on the server or using JavaScript, to set the time zone selector to the time zone the user is currently in?
I'm using Nokogiri to parse pepXML files from different peptide search engines. I have two pepXML files, both of which appear, inasmuch as I can tell, to be of correct format, and puts Nokogiri::XML(IO.read(file)) will output the whole XML file for both files.
The problem is, doc.xpath("any valid xpath") will parse the tag from one of the files, but not the other. No errors are given, so I have no idea why it won't parse. Anyone know of any reasons why Nokogiri wouldn't parse something out?
For uploading an image, the browse button is not properly aligned and also how can i change the colour of the browse button to red? I have used file field.
Hi, everyone: I am also open to just straight-up refactoring what I'm finding to be pretty repetitive, but to give a baseline of how it's working....
I have for every contact a Campaign, which has_many of three types of Models: Email, Call, and Letter.
When an Email (Call or Letter) has been executed for a specific contact, I have a Contact_Email(_or_Call_or_Letter) which belongs to both the Contact and the Model (Email_or_Call_or_Letter).
Each Contact_Email for example pairing has a :date_sent attribute. So does each Contact_Call and Contact_Letter.
How do I find the latest of all of them?
Here is the code I wrote that can find the latest Email and my finding retyping similar code for Call and Letter, but then stuck on how to do a .max on all of them:
def last_email(contact)
#get campaign the contact belongs to
@campaign = Campaign.find_by_id(contact.campaign_id)
@last_email = ContactEmail.find(:last,
:conditions => "contact_id = #{contact.id}",
:order => "date_sent DESC")
@last_call = ContactCall.find(:last,
:conditions => "contact_id = #{contact.id}",
:order => "date_sent DESC")
@last_letter = ContactLetter.find(:last,
:conditions => "contact_id = #{contact.id}",
:order => "date_sent DESC")
# how do I get the latest of all of these to display?
if @last_sent_email.nil?
return "no email sent"
else
return @last_sent_email.date_sent
end
end
Question 1: With what I have, how can I find effectively @last_event given I can find the last Email, last Call, and last Letter for every contact?
Question 2: How can I remove the repetitive code that I have to write for each Model?
According to rake routes, there's the same path for getting an index of objects as there is for creating a new object:
cars GET /cars(.:format) {:controller=>"plugs", :what=>"car", :action=>"index"}
POST /cars(.:format) {:controller=>"plugs", :what=>"car", :action=>"create"}
Obviously, the HTTP verb is what distinguishes between them. I want the "create" version of the cars_path method, not the "index" version. My question is what route method do you invoke to choose the one you want? I'm telling cucumber what path to generate with this:
when /the car plug preview page for "(.+)"/
cars_path(:action => :create, :method => :post)
...but it always chooses the "index" action, not "create". I've tried lots of combinations for the hash argument following cars_path and nothing changes it from choosing "index" instead of "create".
I'll get an error like this:
cars_url failed to generate from {:controller=>"plugs", :method=>:post,
:what=>"car", :action=>"create"}, expected: {:controller=>"plugs", :what=>"car",
:action=>"index"}, diff: {:method=>:post, :action=>"index"}
(ActionController::RoutingError)
This seems like a very simple question but I've had no luck googling for it, so could use some advice. Thanks.
when I try and run the "rake db:seed" command the rails console outputs "NoMethodError: undefined method `db' for #" not quite sure what going on. I'm using netbeans to build my rails project which is using the built-in JRuby 1.2 would that have anything to do with it?
Basically I want to strip the document of words between blockquotes. I'm a regular expression newb and even after using rubular, I'm no closer to the answer.
Any help is appreciated.
I'm writing a Sinatra app which needs to render different layouts based on whether the user is using an iPhone or a regular browser. I can detect the browser type using Rack-Mobile-Detect but I'm not sure of the best way to tell Sinatra which layout to use.
Also, I have a feeling that how I choose to do this may also break page caching. Is that true?
Example code:
require 'sinatra/base'
require 'haml'
require 'rack/mobile-detect'
class Orca < Sinatra::Base
use Rack::MobileDetect
helpers do
def choose_layout
if request.env['X_MOBILE_DEVICE'] == :iPhone
# use iPhone layout
else
# use normal layout
end
end
end
before do
# should I use a before filter?
choose_layout()
end
get '/' do
haml :home # with proper layout
end
end #Class Orca
I have a data model in which I would like to have an item that has a description that can be edited. I would like to also keep track of all edits to the item. I am running into issues with my current strategy, which is:
class Item < ActiveRecord::Base
has_one :current_edit,
:class_name => "Edit",
:foreign_key => "current_edit_id"
has_many :edits
end
class Edit < ActiveRecord::Base
belongs_to :item
end
Can the Item have multiple associations to the same class like this?
I was thinking that I should switch to keeping track of the edit version in the Edit object and then just sorting the has_many relationship base on this version.
I want to use selenium test to cover my rails project ! but i just find little documents on selenium test . I want someone to give me some documents for selenium test of all types !like website ,pdf ,text etc. you can sent them to my gmail [email protected] Thank you ,and best regards!
I'm using Twitter Bootstrap modal featurs and loading data from remote locations. I'm providing the remote url for a set of thumbnails with the hope that once the thumbnail is clicked, the appropriate data (a large version of the image) is displayed. I'm using the html declarative style to define the remote urls and all the features of the modal.
What I find is that Twitter bootstrap modal loads first remote url then does not display subsequent remote data, (although a request to the proper url is made in Chrome) but displays first loaded data always. How do I get it to show the proper data?
View:
#gallery-navigation
%ul
- @profile.background_images.each do |image|
%li
= link_to image_tag(image.background_image.url(:thumb)), remote_image_path(image.id), :role => "button", :data => {:toggle => "modal", :target => "#image-modal", :remote => remote_image_path(image.id)}, :id => "image-modal"
/ Modal
#image-modal.modal.hide.fade(role="dialog" aria-hidden="true" data-backdrop="true")
.modal-body
Controller:
def remote_image
@image = current_user.profile.background_images.find(params[:image_id])
respond_to do |format|
format.html {
render :partial => "remote_image", :locals => { :image => @image }
}
end
end