I've been using capybara for a while, but I'm new to sorcery. I have a very odd problem whereby if I run the specs without Capybara's :js = true functionality I can log in fine, but if I try to specify :js = true on a spec, username/password cannot be found.
Here's the authentication macro:
module AuthenticationMacros
def sign_in
user = FactoryGirl.create(:user)
user.activate!
visit new_sessions_path
fill_in 'Email Address', :with => user.email
fill_in 'Password', :with => 'foobar'
click_button 'Sign In'
user
end
end
Which is called in specs like this:
feature "project setup" do
include AuthenticationMacros
background do
sign_in
end
scenario "creating a project" do
"my spec here"
end
The above code works fine. However, IF I change the scenario spec from (in this case)
scenario "adding questions to a project" do
to
scenario "adding questions to a project", :js => true do
login fails with an 'incorrect username/password' combination. Literally the only change is that :js = true. I'm using the default capybara javascript driver. (Loads up Firefox)
Any ideas what could be going on here? I'm completely stumped. I'm using Capybara 2.0.1, Sorcery 0.7.13. There is no javascript on the sign in page and save_and_open_page before clicking 'sign in' confirms that the correct details are entered into the username/password fields. Any suggestions really appreciated - I'm at a loss.
I am learning how to write test cases using Rspec. I have a simple Post Comments Scaffold where a Post can have many Comments. I am testing this using Rspec. How should i go about checking for Post :has_many :comments. Should I stub Post.comments method and then check this with by returning a mock object of array of comment objects? Is testing for AR associations really required ?
I've recently convinced my management that our outdated version of Rails is slowing us down enough to warrant an upgrade. The approach we're taking is to start a fresh project with current technology rather than a painful upgrade. Our requirements for the project have changed and this will be much easier.
The biggest problem is actually that my knowledge of Rails is out of date. I've dealt only with Rails 1.2.4 while the rest of the world has moved on long ago. What topics have I missed by being buried in my work instead of keeping up with the current Rails fashion?
I'm hesitant to dig through blogs at random because I'm not sure how much has changed between the intervening versions of Rails. It's no use to learn Rails 2.1-2.3 specific stuff that is no longer useful for Rails 3.
I am looking for the easiest, simplest way to access web APIs that return either JSON or XML, with concurrent requests.
For example, I would like to call the twitter search API and return 5 pages of results at the same time (5 requests). The results should ideally be integrated and returned in one array of hashes.
I have about 15 APIs that I will be using, and already have code to access them individually (using simple a NET HTTP request) and parse them, but I need to make these requests concurrent in the easiest way possible. Additionally, any error handling for JSON/XML parsing is a bonus.
My rails app gets the following JSON feed from mixcloud and sticks the results into my index page
At the moment when I do this, the entire contents of my feed are displayed unformatted in one big blob of scary looking text (without the curly JSON brackets)
I only want to display specific values from the feed in the view.
From the feed in question lets say for simplicity that I just wanted to display all values with a key of "url"
In case I'm doing something wrong here's my code:
# podcast controller
def index
# I'm using a class method to get the feed
@feed = Podcast.feed
end
# podcast model
def self.feed
feed = JSON.parse(open("http://api.mixcloud.com/alivefrommaryhill/feed").read)
end
# index.html.haml
.feed
= @feed
I can't figure out how to style the results and display only certain items from the feed. Is my approach wrong?
I have the following form:
<% form_for(@account, :url => admin_accounts_path) do |f| %>
<%= f.error_messages %>
<%= render :partial => 'form', :locals => {:f => f} %>
<h2>Account Details</h2>
<% f.fields_for :customer do |customer_fields| %>
<p>
<%= customer_fields.label :company %><br />
<%= customer_fields.text_field :company %>
</p>
<p>
<%= customer_fields.label :first_name %><br />
<%= customer_fields.text_field :first_name %>
</p>
<p>
<%= customer_fields.label :last_name %><br />
<%= customer_fields.text_field :last_name %>
</p>
<p>
<%= customer_fields.label :phone %><br />
<%= customer_fields.text_field :phone %>
</p>
<% end %>
<p>
<%= f.submit 'Create' %>
</p>
<% end %>
As well as
attr_accessor :customer
And I have a before_create method for the account model which does not store the customer_fields, but instead uses them to submit data to an API.. The only thing I store are in the form partial..
The problem I'm running into is that when a validation error gets thrown, the page renders the new action (expected) but none of the non-column attributes within the Account Detail form will show?
Any ideas as to how I can change this code around a bit to make this work me??
This same solution may be the help I need for the edit form, I have a getter for the data which it asks the API for, but without place a :value = "asdf" within each text box, it doesn't populate the fields either..
Hi,
i don't understand this little thing:
Suppose, we have "Person" model
class Person < ActiveRecord::Base
end
Why Person.all works ?
Person.all.each { |p| do_something }
This syntax tells us, that we have Person class-object instanciated somewhere ?
Or is it some convention over configuration case ?
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>
I have form in my view like following
1 <div>
2 <% form_tag facebook_user_path do %>
3 <label>Use my photo and name from facebook?</label><br />
4 <%= check_box_tag 'use_name_and_photo', 'yes', true %>
5 <img src="<%= @user.pic %>" /><% @user.name %>
6
7 <%= submit_tag "Finish", :id => "use_name_and_photo_submit" %>
8 <% end %>
9 </div>
I have attached some JS handlers using Jquery to this form.
1 var fb = {
2 extendedPermissions: function () {
3 $("#use_name_and_photo_submit").click(function (event) {
4 FB.Connect.showPermissionDialog("email,read_stream,publish_stream", function (perms) {
5 if (!perms) {
6 alert("You have to grant facebook extended permissions to further browse the application.");
7 } else {
8 $("form").submit(function () {
9 $.post($(this).attr("action"), $(this).serialize(), null, "script");
10 });
11 }
12 });
13 event.preventDefault();
14 return false;
15 });
16 }
17 };
18
19 $(document).ready(function () {
20 fb.extendedPermissions();
21 });
What I want is that when the user clicks on the "Finish" button, he is prompted for the facebook permissions dialogue and when he gives the permissions, the form is submitted to FacebookUsersController.
Right now when I click the "Finish" button, facebook permissions dialogue is initiated but before I am prompted for the actual permission submission window, the browser freezes. Just like I have pressed Esc during the process. In fact status bar of the browser says "Stopped".
Any help is highly appreciated.
I'm writing a sinatra app and testing it with rspec and rack/test (as described on sinatrarb.com).
It's been great so far, until I moved some rather procedural code from my domain objects to
sinatra helpers.
Since then, I've been trying to figure out how to test these in isolation ?
I am trying to use some rails code withing a javascript and need to have that rails code be dynamically changed. Here's the line of code:
$(this).replaceWith("<%= escape_javascript(render(:partial => 'shared/products')) %>");
The 'shared/products' is the part I want to change based off information passed earlier in the javascript. How do I insert a value from javascript so that instead of 'shared/products' the products portion can be a variable? Hope this makes sense. I'm not the most experienced jQuery/javascript programmer, so any help is very much appreciated. Thanks in advance!
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!
Hi stackoverflow. I'm running into something strange. I'm using memcached with a caches_action setup. I'm doing this in 4 different controllers. In two of them, it works flawlessly (so far), though admittedly those two controllers are less complicated than the two in which it doesn't seem to work. I'm doing something like this:
caches_action :index, :expires_in => 6.hours, :cache_path => Proc.new {|controller| controller.send(:generate_cache_path) }, :layout => false, :if => Proc.new { |c| c.request.format.js? }
The intention behind the above is to cache some results that are dependent on the params. my :generate_cache_path method just takes into account some params and session vars and creates a unique key for memcached. I can see in memcached -vv that this is working.
What's weird is that I get my request from the rails app for a given key, and I see memcached (with -vv) get the request and send back the response. But then my action runs anyway, and a new value is then set for the same key, even when all the same params are given. I can watch it happen. In the controllers where everything is working, the request is made for the fragment, it gets it, and the action in the controller is halted, and the fragment is passed back.
These lines come from the exact same request:
Cached fragment hit: views/items/?page=1&rp=10&srtn=created_at&srto=DESC.js
And then:
Cached fragment miss: views/items/?page=1&rp=10&srtn=created_at&srto=DESC.js
I don't know what to make of it, or if I'm doing something stupid. Any help or ideas where I could start looking for trouble would be greatly appreciated.
If you use haml as rails view template, you can write portion of your page using markdown by using the ":markdown" filter.
Is is possible to do the same using erb?
Hi,
I'm trying to run a cucumber feature multiple times (i.e 500 times). Is there a way of doing this than me having to type in the same command everytime? I'm guessing this can be done using Rake? I'm not an expert in using rake or cucumber.
Will appreciate your help.
Thanks
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]
Hi Everyone,
I have three models that I want to interact with each other.
Kase, Person and and Company.
I have (I think) setup the relationships correctly:
class Kase < ActiveRecord::Base
#HAS ONE COMPANY
has_one :company
#HAS MANY PERSONS
has_many :persons
class Person < ActiveRecord::Base
belongs_to :company
class Company < ActiveRecord::Base
has_many :persons
def to_s; companyname; end
I have put the select field on the create new Kase view, and the create new Person view as follows:
<li>Company<span><%= f.select :company_id, Company.all %> </span></li>
All of the above successfully shows a drop down menu dynamically populated with the company names within Companies.
What I am trying to do is display the contact of the Company record within the kase and person show.html.erb.
For example, If I have a company called "Acme, Inc." and create a new Kase called "Random Case" and choose within the create new case page "Acme, Inc." from the companies drop down menu. I would then want to display "Acme, Inc" along with "Acme, Inc. Mobile" etc. on the "Random Case" show.html.erb.
I hope this makes sense to somebody!
Thanks,
Danny
hi All,
I am new with rails and i am using "devise" gem for authentication purposes.
At first i add a new user through default sign up page (E.g./users/sign_up)
Then, i made "sign_up" page only available to signed_in users by following instructions from
Devise before filter that prevents access to "new_user_registration_path" unless user is signed-in
Now, after sign in process when i try open sign up page it always directs me to root_path! How can i access sign up page?
My "roots.rb" file as follows:
Example::Application.routes.draw do
devise_for :users, :controllers => { :registrations => 'registrations'}
resources :companies
resources :orders
resources :customers
root :to => "welcome#index"
end
Thank you all!
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 have a User model in my app, which I would like to store basic user information, such as email address, first and last name, phone number, etc.
I also have many different types of users in my system, including sales agents, clients, guests, etc.
I would like to be able to use the same User model as a base for all the others, so that I don't have to include all the fields for all the related roles in one model, and can delegate as necessary (cutting down on duplicate database fields as well as providing easy mobility from changing one user of one type to another).
So, what I'd like is this:
User
-- first name
-- last name
-- email
--> is a "client", so
---- client field 1
---- client field 2
---- client field 3
User
-- first name
-- last name
-- email
--> is a "sales agent", so
---- sales agent field 1
---- sales agent field 2
---- sales agent field 3
and so on...
In addition, when a new user signs up, I want that new user to automatically be assigned the role of "client" (I'm talking about database fields here, not authorization, though I hope to eventually include this logic in my user authorization as well). I have a multi-step signup wizard I'm trying to build with wizardly. The first step is easy, since I'm simply calling the fields included in the base User model (such as first_name and email), but the second step is trickier since it should be calling in fields from the associated model (like--per my example above--the model client with fields client_field_1 or client_field_2, as if those fields were part of User).
Does that make sense? Let me know if that wasn't clear at all, and I'll try to explain it in a different way.
Can anyone help me with this? How would I do this?
I need to validate form on client-side and I was wondering if there is a rails plugin which will help me in this task (maybe it will build javascript needed to validate certain model)?
I have an app where there is always a current contest (defined by start_date and end_date datetime). I have the following code in the application_controller.rb as a before_filter.
def load_contest
@contest_last = Contest.last
@contest_last.present? ? @contest_leftover = (@contest_last.end_date.utc - Time.now.utc).to_i : @contest_leftover = 0
if @contest_last.nil?
Contest.create(:start_date => Time.now.utc, :end_date => Time.now.utc + 10.minutes)
elsif @contest_leftover < 0
@winner = Organization.order('votes_count DESC').first
@contest_last.update_attributes!(:organization_id => @winner.id, :winner_votes => @winner.votes_count) if @winner.present?
Organization.update_all(:votes_count => 0)
Contest.create(:start_date => @contest_last.end_date.utc, :end_date => Time.now.utc + 10.minutes)
end
end
My questions:
1) I would like to change the :end_date to something that signifies next Sunday at a certain time (eg. next Sunday at 8pm). Similarly, I could then set the :start_date to to the previous Sunday at a certain time. I saw that there is a sunday() class (http://api.rubyonrails.org/classes/Time.html#method-i-sunday), but not sure how to specify a certain time on that day.
2) For this situation of always wanting the current contest, is there a better way of loading it in the app? Would caching it be better and then reloading if a new one is created? Not sure how this would be done, but seems to be more efficient.
Thanks!
Hi,
I have a teacher model which has_many students. When I render a student (as json) I want to strip out the teacher_id property and replace it with the name of the teacher in my representation.
What is the best way to achieve this?
Cheers,
Chris
I have a model Entree which belongs to a model Vin, which itself belongs to a model Producteur.
On the form for Entree creation/edition, I want to allow the user to define the attributes for parent Vin and Producteur to create them, or retrieve them if they exist (retrieval based on user input).
For now I do the following in Entree new and edit actions:
@entree = Entree.new
@entree.vin = Vin.new
@entree.vin.producteur = Producteur.new
and use fields_for helper in the form,and that works. But I intend to have much more dependencies with more models, so I want to keep it DRY. I defined a after_initialize callback in Vin model which does the producteur initialization:
class Vin < ActiveRecord::Base
after_initialize :vin_setup
def vin_setup
producteur = Producteur.new
end
end
and remove the producteur.new from the controller. However, get an error on new action:
undefined method `model_name' for NilClass:Class
for the line in the form that says
<%= fields_for @entree.vin.producteur do |producteur| %>
I guess that means the after_initialize callback doesn't act as I expect it. Is there something I'm missing? Also, I get the same error if I define a after_initialize method in the Vin model instead of definiing a callback.