Ruby on Rails ActiveScaffold: Showing {{model}} instead of model name? - ruby-on-rails

I'm using ActiveScaffold with Ruby on Rails and I'm loving it, however there is one weird thing. Whenever I hit "Edit" or "Create New" in my webapp's ActiveScaffold, it says "Create {{model}}" or "Update {{model}}" in the webapp rather than using the model's name. Why is this? I have an ads_controller.rb that includes this:
active_scaffold :ad do |config|
config.label = "Ads"
config.columns = [:name, :description, :imageUrl, :linkUrl, :apps, :created_at, :updated_at]
config.update.columns = [:name, :description, :imageUrl, :linkUrl, :apps]
config.create.columns = config.update.columns
list.sorting = {:created_at => 'DESC'}
columns[:imageUrl].label = "Image URL"
columns[:linkUrl].label = "Link URL"
end
And my routes.rb includes this:
map.namespace :admin do |admin|
admin.root :controller => 'admin_home', :action => 'index'
admin.resources :ads, :active_scaffold => true
end
Any thoughts on why I'm seeing "Create {{model}}" instead of "Create ad" ?

I just had this problem yesterday.
Looks like the correct syntax to perform interpolation on resources is using %{model} instead of {{model}}, despite some documentation claiming otherwise.
Check your resource files in config/locales.

If you have activescaffold installed as a plugin, you'll have to edit:
vendor/plugins/active_scaffold/lib/active_scaffold/locale/en.yml
and replace the {{model}}, etc with %{model}

Related

Active Admin: How to customize the main menu (at the top of the page)?

This is probably simple, but I haven't been able to find a solution.
In ActiveAdmin, I'd like to customize the behavior of the menu at the top of the page, particularly this link for the currently logged in user:
In all my searching, I haven't been able to find a way of altering the links in the top-right corner.
I'd like to change the text, where the link points to, and possible add links to this area of the menu.
Is there an easy way to do this?
You can modify config/initializers/active_admin.rb.
For ActiveAdmin 0.6.0, add these lines :
ActiveAdmin.setup do |config|
# Customize Up Right menu
config.namespace :admin do |admin|
admin.build_menu :utility_navigation do |menu|
menu.add :label => 'click me', # text of your link
:url => proc{ my_awesome_path }, # route of your link
:html_options => {:style => 'float:left;'}, # attributes added in the DOM of your link
:if => proc{ 1 < 2 } # condition to display the link
menu.add :label => proc{ display_name current_active_admin_user }, # email of the current admin user logged
:url => proc { my_other_awesome_path },
:html_options => {:style => 'float:left;'},
:id => 'current_user',
:if => proc{ current_active_admin_user? }
admin.add_logout_button_to_menu menu, 100, :style => 'float:left;' # logout link
end
end
end
For ActiveAdmin 0.4.2 and before, you have to add this code at the end of the same file :
ActiveAdmin.setup do |config|
# ...
end
module ActiveAdmin
module Views
class HeaderRenderer
alias_method :default_to_html, :to_html
def to_html
default_to_html.sub(current_admin_user.email, "<a style='text-decoration: underline;' href='#{my_awesome_path}'>#{current_admin_user.email}</b>").html_safe
end
end
end
end
Hope this helps :)
Just type the following in initializers file:
ActiveAdmin.setup do |config|
config.namespace :admin do |admin|
admin.build_menu :utility_navigation do |menu|
menu.add label: "ActiveAdmin.info", url: "http://www.activeadmin.info",
html_options: { target: :blank }
admin.add_current_user_to_menu menu
admin.add_logout_button_to_menu menu
end
end
end

Agile web development with rails ProductsControllerTest , meaning of :product symbol

I am new to ruby on rails and I have been reading the Agile web development with rails book.
I am working on Iteration B1: Validating, and I am really confused by the symbol :product while I am working on the test. The question is about the ":product => #update"
I really have no idea what does this mean and where the :product symbol come from. I know it is a hash, but which table does it hash to? what exactly does it do here? The code is as following. Thank you in advance.
require 'test_helper'
class ProductsControllerTest < ActionController::TestCase
setup do
#product = products(:one)
#update = {
:title => 'Lorem Ipsum',
:description => 'Wibbles are fun!',
:image_url => 'lorem.jpg',
:price => 19.95
}
end
test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:products)
end
test "should get new" do
get :new
assert_response :success
end
test "should create product" do
assert_difference('Product.count') do
***post :create, :product => #update***
end
assert_redirected_to product_path(assigns(:product))
end
# ...
test "should show product" do
get :show, :id => #product.to_param
assert_response :success
end
test "should get edit" do
get :edit, :id => #product.to_param
assert_response :success
end
test "should update product" do
put :update, :id => #product.to_param, :product => #update
assert_redirected_to product_path(assigns(:product))
end
# ...
test "should destroy product" do
assert_difference('Product.count', -1) do
delete :destroy, :id => #product.to_param
end
assert_redirected_to products_path
end
end
There is nothing magical about this construct. The #update is a variable name that references a hash. The hash is declared earlier in your test file.
#update = {
:title => 'Lorem Ipsum',
:description => 'Wibbles are fun!',
:image_url => 'lorem.jpg',
:price => 19.95
}
This hash contains new data that should be passed to the update action in the products controller. It's confusing because of the way the variable is named. A better name might help:
#product_attributes
The products#update action expects a hash containing updated data. That data is used to update the object.
The following line in your test...
post :update, :product => #update
Corresponds to this line you probably have in your products controller:
if #product.update_attributes(params[:product])
Notice params[:product]. It's basically saying "make a post request to products#update and pass it the #update hash as :product.
And that explains the symbol part. The :product symbol in your test is the name of the parameter that contains the product data, that the update action expects.
post :update, :product => #update
In theory you can call it whatever you want. But per convention it helps to call it the resource name.

Rspec test on Simple Form Associations

I am trying to run a request spec on a form (built with Simple Form). The form includes some select boxes that are generated using the association method and therefore database values for the model.
When running save_and_open_page it doesn't look like the select the values in the drop downs.
I have looked at Mocking and Stubbing but this is new to me and I'm still a little confused on the concept beyond basic usage.
Is there any way to generate the collection for the select box so Capybara can pick it up?
I'm using Rails 3.1, Simple Form, Capybara and FactoryGirl.
My code is...
challenge_spec
describe "New Challenges" do
before(:all) do
%w["Under 13", "13 - 16"].each do |item|
FactoryGirl.create(:age, :name => item)
end
end
it "should redirect to resources after submission" do
login_valid_user
visit new_challenge_path
#challenge = Factory.build(:challenge)
fill_in "challenge_name", :with => #challenge.name
fill_in "challenge_description", :with => #challenge.description
fill_in "challenge_description", :with => #challenge.description
select "30 mins", :from => "challenge_timescale"
save_and_open_page
select 1, :from => "challenge_age_id"
select #challenge.category, :from => "challenge_category_id"
click_button "save_button"
end
end
Controller
def new
#challenge = Challenge.new
respond_to do |format|
format.html # new.html.haml
format.json { render json: #challenge }
end
end
Form item
<%= f.association :age, :prompt => "Please select..." %>
Models
Challenge
class Challenge < ActiveRecord::Base
belongs_to :age
end
Age
class Age < ActiveRecord::Base
has_many :challenges
end
I strongly recommend creating fixtures for your tests.
This way you can manually create and manipulate the records needed for tests. It's not as efficient or elegant as using mocks, stubs and doubles but it reinforces understanding of the application and tests.

What are the right parameters for a rspec put request on my stub

I have a controller spec and I get following failed expectation:
Failure/Error: put :update, :id => login_user.id, :user => valid_attributes
#<User:0xbd030bc> received :update_attributes with unexpected arguments
expected: ({:name=>"changed name", :email=>"changed#mail.com", :password=>"secret", :password_confirmation=>"secret"})
got: ({"name"=>"Test user", "email"=>"user#test.com", "password"=>"secret", "password_confirmation"=>"secret"})
And for me it looks like I am passing in "name" => "Test User" and I am expecting :name => "test user"
my spec looks like this:
describe 'with valid parameters' do
it 'updates the user' do
login_user = User.create!(valid_attributes)
controller.stub(:current_user).and_return(login_user)
User.any_instance.
should_receive(:update_attributes).
with(valid_attributes.merge(:email => "changed#mail.com",:name=>"changed name"))
put :update, :id => login_user.id, :user => valid_attributes
end
end
and I have something like this for my valid attributes:
def valid_attributes
{
:name => "Test user",
:email=> "user#test.com",
:password => "secret",
:password_confirmation => "secret"
}
end
so what is wrong with my parameters any suggestions?
I am using Rails 3.0.5 with rspec 2.6.0...
The failure message is telling you exactly what's going on: any instance of User is expecting update_attributes with a hash including :email => "changed#mail.com", but it's getting :email => "user#test.com" because that's what's in valid_attributes. Similarly, it's expecting :name => "changed_name", but gets :name => "Test user" because that's what's in valid_attributes.
You can simplify this example and avoid this confusion. There is no need to use valid_attributes here because should_receive intercepts the update_attributes call anyhow. I usually do this like so:
controller.stub(:current_user).and_return(mock_model(User)) # no need for a real user here
User.any_instance.
should_receive(:update_attributes).
with({"these" => "params"})
put :update, :id => login_user.id, :user => {"these" => "params"}
This way the expected and actual values are right in the example and it makes clear that it doesn't really matter what they are: whatever hash is passed in as :user is passed directly to update_attributes.
Make sense?

How do I mix nested resources with multiple post values when testing Rails controllers?

I have a model called "Post" which is a nested resource under a model called "Project" and I'm trying to test the controller. The code works find in my browser, but I can't get it to work in the test. Here's the test
context "on POST to :edit" do
setup do
post( :edit,
:project_id => #project1.to_param,
:id => #post1.to_param,
:post => { :title => 'new title', :text => 'other text' }
)
end
should_assign_to :post
should_assign_to :project
should_respond_with :success
should "update post values" do
assert_equal 'other text', assigns['post'].text
end
Any idea how I'm screwing this up?
This was the result of me not understanding Rails' REST architecture -or- the post syntax. I should have been using PUT instead of POST, and the call should have looked like this:
context "on PUT to :update" do
setup do
put :update, {
:project_id => #project1.to_param,
:id => #post1.to_param,
:post => { :title => 'new title', :text => 'other text' }
}
end
should_assign_to :post
should_assign_to :project
should_respond_with :success
should "update post values" do
assert_equal 'new title', assigns['post'].title
assert_equal 'other text', assigns['post'].text
end
end
I had been using the wrong syntax because for some reason it was still handling my nested id's correctly.

Resources