My FactoryGirl created objects are not persisted? - ruby-on-rails-3

I am running the following feature:
Scenario: viewing existing images
Given I am on the images page
And 4 images already exist
Then I should see a table containg those 4 images
And have the option to show images
And have the option to delete images
with these steps defined:
Given /^I am on the images page$/ do
visit(images_path)
end
Given /^(\d+) images already exist$/ do |count|
count.to_i.times {
FactoryGirl.build(:image).save!
}
end
Then /^I should see a table containg those (\d+) images$/ do |count|
page.all('table#imagesTable tr').count.should == count
end
The final step, counting the rows in the table, fails miserably. It can only find one row, which I assume is the header row. These are the tests for the index page, which I have manual confirmed it works. Why aren't my FactoryGirl created objects picked up my controller?
The index method of the controller:
def index
#images = Image.all
end

Swap the order of the 'I am on the images page' and '4 images already exist' givens.
The index action is being called the view is rendered before the creation of the images, so they they won't be picked up.
Also, I don't know if you know this already, but instead of
FactoryGirl.build(:image).save!
you can do
FactoryGirl.create(:image)

Related

Graph call Error When Getting User Page Information (Ruby on Rails)

I'm having some trouble making some graph calls with Koala.
Graph calls:
#graph_data = #api.get_object("me/books")
#account_data = #api.get_connections("me","accounts")
I have a do loop in my callback.html.erb that shows a table which displays the name of the user's favorite books.
(had to remove the html tags)
#graph.data.each do |data|
data["name"]
end
This code is fine, i get a table with all the book names.
The problem occurs when i want to display the #account_data object, this is supposed to hold the information of all the pages the user is an admin of.
I am expecting a table with the names of all the pages the user is subscribed to.
#account_data should have exactly two array elements since i am admin for two pages.
My understanding is that i should be getting an object that looks like this:
[{field1=>val1,field2=>val2,....}, {field1=>val1,field2=>val2,....}]
I tried using the Graph call #api.get_object("me/accounts") but same problem - blank screen.
Thanks in advance.

Rails Select Box form helper for multiple booleans

Is there a way to use a select box in a rails form for multiple booleans? Let's say I have three weather conditions: Clear, Cloudy, Rainy that are each boolean. Can I put them in one select box titled "Weather", and when one of them is picked that one becomes 'true'?
To me, I see this as two different actions.
1) The user making a selection from a selection_box helper on the form. That variable gets set to the resource :current_weather and stored in the database.
2) After submit button is clicked then is more logic processed in the controller or through a class method. Let's say it was in the 'update' portion of CRUD in a weather tracker.
def update
#tracker = Tracker.find(params[:id])
if #tracker.current_weather == "Clear"
#do this
end
end
Maybe this will give you some ideas. Good luck!

Load object from model on every page?

I'm looking to load a single (chosen randomly) object from a single table in my database on every page of my rails app.
For example, a quotes table which has several quotes in the table, and I just want one on every page load.
What's the best way to accomplish this? Obviously copy & pasting the query into each controller isn't the right way to go about this.
I wouldn't use before_filter for this, there is no need to access database on redirecting actions and other "not rendered" actions. Instead I would use helper_function, and I would call it in the layout, as you need to position it anyways.
def random_quote
#quote ||= "Select random quote here"
end
helper_method :random_quote
Your method of selecting a quote is up to you. You just need to access random_quote as a normal helper method in the layout. This only access one quote per action, only if the layout is rendered.
This kind of stuff typically goes into a before_filter in the ApplicationController :
before_filter :get_random_quote
#This code is executed before every action of your app
def get_random_quote
random_id = ...#Generate a random ID ...
#random_quote = Quote.find(random_id)
end
Then in your views, just refer to #random_quote. Done!
Edit : on second thought, Matzi solution seems smarter. The request will only get called when you actually output something. Nothing's wasted.
Assuming PostgreSQL:
before_filter :get_quote
def get_quote
#quote = Quote.order('RANDOM()').limit(1)
end

How implement the Back Link to the page from which comes from

I have an application with associations and will pagination the pages.
The index page from the main object "cat_list" shows links to the association "data_lists". The index page has also pagination with "will_paginate"
I show for example page=3 "/cat_lists?page=3"
I click the link of a "data_lists" for example "/cat_lists/8984/data_lists"
This index page shows a list of data_lists with Edit, Destroy and a New link.
And a Back Link to the cat_lists index page now "/cat_lists"
What is the best practice to implement the features, that the Back Link now the page from which comes from?
I usually record the history in the session and then call it via redirect_to back (no colon)
def index
... do your stuff ...
record_history
end
protected
def record_history
session[:history] ||= []
session[:history].push request.url
session[:history] = session[:history].last(10) # limit the size to 10
end
def back
session[:history].pop
end
Note that this only works for GET requests.
If I understand you correctly link_to('Back', :back) is what you want.
I also use mosch's approach.
link_to('Back', :back) only uses the browsers 'back' functionality. Managing the history server side gives you more control (i.e. if you've come from a google search, guess what happens on :back).
Managing the history server side gives you the possibility to hide links that would take the user off your page. Further you can offer the user to browse multiple steps back - i.e. via dropdown.

Rails-3.1 nested routes creating same action and controller, basic lack of knowledge

Doing the following routes configuration:
resources :cadeiras do
resources :professores
end
resources :cadeiras do
resources :fichas
end
resources :fichas do
resources :exercicios
end
will generate me 2 different links to the same controller and action, running rake routes ill get something like:
fichas GET /fichas(.:format) {:action=>"index", :controller=>"fichas"}
cadeira_fichas GET /cadeiras/:cadeira_id/fichas(.:format) {:action=>"index", :controller=>"fichas"}
The first action will reference all the 'fichas' while the second on is referencing only 'fichas' from 'cadeiras' how is it possible to distinguish the two actions?
I would like to avoid three level nesting problems as described here :http://weblog.jamisbuck.org/2007/2/5/nesting-resources
Thank you for your time
If I understand your question correctly, the answer is "you don't distinguish them" :
The exact same action is executed from the controller, rendering the exact same view. The difference is the collection of 'fichas' that get sent to the view:
- in the first case, all fichas are available in the view
- in the second case, only the 'fichas' related to the 'cadeira' are available in the view (e.g. /cadeira/1/fichas will display only the 'fichas' related to the 'cadeira' with id 1)
To determine which records to show (e.g.) in an index view, you can do something like this:
unless cadeira_id = params[:cadeira_id]
#fichas = Ficha.all
else
#fichas = Cadeira.find(cadeira_id).fichas
end
The rest is up to the view: it should render fichas the same way, you just chose which records are actually made available to it.