Rails 3 routing error with namespace and uncountable noun together - ruby-on-rails-3

Well this is my first time doing this. In routes.rb I have the following:
namespace :admin do
resources :news
end
And I have this after rake routes:
admin_news_index GET /admin/news(.:format) admin/news#index
POST /admin/news(.:format) admin/news#create
new_admin_news GET /admin/news/new(.:format) admin/news#new
edit_admin_news GET /admin/news/:id/edit(.:format) admin/news#edit
admin_news GET /admin/news/:id(.:format) admin/news#show
PUT /admin/news/:id(.:format) admin/news#update
DELETE /admin/news/:id(.:format) admin/news#destroy
As you can see the path to the action "new" is new_admin_news_path, unfortunately, when I visited that path, something pop up as follow:
No route matches {:action=>"show", :controller=>"admin/news", :id=>#<News id: nil, news_type_id: nil, title: nil, content: nil, views: nil, status: nil, start_date: nil, end_date: nil, created_at: nil, updated_at: nil, news_key: nil>}
I was thinking that the "news" is an uncountable noun and this might be the problem. So I change the config/initializers/inflections.rb to:
ActiveSupport::Inflector.inflections do |inflect|
inflect.uncountable %w( fish sheep news )
end
Obviously, it is not working...
What should I do? Any suggestions?

Related

One route from nested routes not working

I have a User model which has_many :portfolios and each Portfolio has_many :pictures. My routes look like this:
resources :users do
resources :portfolios do
resources :pictures
end
end
Then I try to create a new Portfolio like this:
<%= link_to "new portfolio...", new_user_portfolio_path(current_user) %>
With a form like this:
<%= form_for [current_user, #portfolio], html: { class: "form_settings" } do |f| %>
...
<% end %>
But when I click on "new portfolio..." my URL says /users/12/portfolios/new and I get this error:
No route matches {:action=>"show", :controller=>"portfolios", :user_id=>#<User id: 12, name: "michael", password_digest: "d787f56b080945c1ec0b3343cbf962ca427bb8ef", remember_token: "dL4nPlt0E5azUMemNIvkdg", admin: false, created_at: "2013-03-03 01:18:19", updated_at: "2013-03-03 19:56:28">, :id=>#<Portfolio id: nil, user_id: 12, name: nil, created_at: nil, updated_at: nil>}
I don't think the error is coming from the form to create the portfolio. There must be a link on the /portfolios/new page to add a picture or something like that, and this is what's failing (because the portfolio is a new record).
You need to hide this link when you're on the new page.

Testing controller new action with Rspec2 + Rails3

I have the following rspec2 test case for my controller new action
describe "GET #new" do
it "should assign new project to #project" do
project = Project.new
get :new
assigns(:project).should eq(project)
end
end
and I'm getting the following error
1) ProjectsController GET #new should assign new project to #project
Failure/Error: assigns(:project).should eq(project)
expected: #<Project id: nil, name: nil, company_id: nil, created_at: nil, updated_at: nil>
got: #<Project id: nil, name: nil, company_id: nil, created_at: nil, updated_at: nil>
(compared using ==)
Diff:#<Project:0x007f461c498270>.==(#<Project:0x007f461c801c90>) returned false even though the diff between #<Project:0x007f461c498270> and #<Project:0x007f461c801c90> is empty. Check the implementation of #<Project:0x007f461c498270>.==.
# ./spec/controllers/projects_controller_spec.rb:19:in `block (3 levels) in <top (required)>'
Finished in 1.21 seconds
13 examples, 1 failure, 10 pending
Failed examples:
rspec ./spec/controllers/projects_controller_spec.rb:16 # ProjectsController GET #new should assign new project to #project
and when I use == instead on eq, I'm getting the following error
1) ProjectsController GET #new should assign new project to #project
Failure/Error: assigns(:project).should == project
expected: #<Project id: nil, name: nil, company_id: nil, created_at: nil, updated_at: nil>
got: #<Project id: nil, name: nil, company_id: nil, created_at: nil, updated_at: nil> (using ==)
Diff:#<Project:0x007f461c4f5420>.==(#<Project:0x007f461c63b280>) returned false even though the diff between #<Project:0x007f461c4f5420> and #<Project:0x007f461c63b280> is empty. Check the implementation of #<Project:0x007f461c4f5420>.==.
what am I doing wrong here, thanks in advance
I'm on
Rails3
Rspec2
You are creating a new project before accessing the new action, this is unnecessary. Your controller actually does that work for you already. The problem you are facing is that you have two new projects created (in your case you have created Project:0x007f461c498270 first and then Project:0x007f461c801c90, they have the same attributes but are different projects). This test should pass:
describe "GET #new" do
it "assigns a new Project to #project" do
get :new
assigns(:project).should be_a_new(Project)
end
end

Rails 3: named parameter routes break link_to

I'm trying to create a Rails app that divides the users by groups and sub-groups and offers a customized site to each sub-group. I figured the easiest way to do that was to create scope with named parameters:
scope ":group/:subgroup/" do
devise_for :users
resources :users
end
"rake routes" then produces this:
users GET /:group/:subgroup/users(.:format) {:action=>"index", :controller=>"users"}
POST /:group/:subgroup/users(.:format) {:action=>"create", :controller=>"users"}
new_user GET /:group/:subgroup/users/new(.:format) {:action=>"new", :controller=>"users"}
edit_user GET /:group/:subgroup/users/:id/edit(.:format) {:action=>"edit", :controller=>"users"}
user GET /:group/:subgroup/users/:id(.:format) {:action=>"show", :controller=>"users"}
PUT /:group/:subgroup/users/:id(.:format) {:action=>"update", :controller=>"users"}
DELETE /:group/:subgroup/users/:id(.:format) {:action=>"destroy", :controller=>"users"}
It works perfectly--except when I to use link_to:
link_to current_user.name, current_user
Then it throws this error
ActionController::RoutingError in Users#index
Showing C:/Rails/MyApp/app/views/users/_my_list.html.haml where line #8 raised:
No route matches {:action=>"show", :controller=>"users", :group=>#<User id: 7, email: "username#domain.com", encrypted_password: "$2a$10$GdZeC0b4VaNxdsDXP...", password_salt: "$2a$sj$88gm0nttYE.7a4IHi.BNO", reset_password_token: nil, remember_token: nil, remember_created_at: nil, sign_in_count: 86, current_sign_in_at: "2011-08-05 17:18:19", last_sign_in_at: "2011-08-05 00:14:26", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", confirmation_token: nil, confirmed_at: "2010-12-09 23:08:54", confirmation_sent_at: "2010-12-09 23:08:36", failed_attempts: 0, unlock_token: nil, locked_at: nil, created_at: "2010-12-09 23:08:36", updated_at: "2011-08-05 17:18:19", name: "UserOne">}
Line #8, of course, is where I tried to use link_to. My guess is that link_to is ignoring the scope, which accounts for current_user getting stuffed into the :group parameter. Is there a way to keep this from happening, or am I going about this whole issue totally wrong?
Your resource :users is inside the scope, so you don't have the /users/:id route.
You could try this:
scope ":group/:subgroup/" do
devise_for :users
end
resources :users
I'm not sure if the scope has a shallow => true option (I think it does and you should probably use it).
Hope that helps
The problem is you can't reach an user without passing in a group and a subgroup. Reading your error it's possible to understand that rails is trying to convert your current_user to the group.

Rspec and Capybara object matching

Using Rails 3.0.5, RSpec 2 and Capybara 0.4.1.2 and I'am trying to write a controller spec for my SessionsController#new action.
it "assigns the empty session to a variable" do
get :new
assigns(:session).should == ActiveRecord::Base::Session.new
end
I'm using the ActiveRecord::Base namespace as it seems to clash with the Capybara Session class when I don't.
Here is the SessionsController:
class SessionsController < ApplicationController
def new
#session = Session.new
end
end
RSpec doesn't seem to understand these are the same objects. Here is what my test returns:
Failure/Error: assigns(:session).should == ActiveRecord::Base::Session.new
expected: #<Session id: nil, session_id: nil, data: nil, created_at: nil, updated_at: nil>
got: #<Session id: nil, session_id: nil, data: nil, created_at: nil, updated_at: nil> (using ==)
Diff:
# ./spec/controllers/sessions_controller_spec.rb:17:in `block (3 levels) in <top (required)>'
Any hints?
The problem is that if you do
ActiveRecord::Base::Session.new == ActiveRecord::Base::Session.new
You would get false, as both these objects have a separate object_id.
Try this:
assigns(:session).should be_an(ActiveRecord::Base::Session)

How to create a nested form in Rails 3 that works? ( I'm getting a routing error. )

I have a Client and ProposalRequest model that look like this:
class Client < ActiveRecord::Base
has_many :proposal_requests
accepts_nested_attributes_for :proposal_requests, :allow_destroy => true
end
class ProposalRequest < ActiveRecord::Base
belongs_to :client
end
In my my routes file, I included the nested routes, as usual.
resources :clients do
resources :proposal_requests
end
And this is my form so far:
-semantic_form_for [Client.new, ProposalRequest.new] do |f|
=f.inputs
=f.buttons
But after this, I'm stuck because of this error.
No route matches {:controller=>"proposal_requests", :client_id=>#<Client id: nil, name: nil, title: nil, organization: nil, street_address: nil, city: nil, state: nil, zip: nil, phone: nil, email: nil, status: "interested", how_you_heard: nil, created_at: nil, updated_at: nil>}
Can anyone help me puzzle out this error?
The problem is that your nested route is meant to add a new ProposalRequest to an existing Client. If you want to create a Client and a ProposalRequest at the same time, you need to just use new_client_path and semantic_form_for #client do |f|.
I would recommend you do the following in your clients_controller:
def new
#client = Client.find(params[:id])
#client.proposal_requests.build
end
And in your view:
semantic_form_for #client do |f|
= f.inputs # fields for client
= f.inputs :name => 'Proposal Request', :for => :proposal_requests do |pf|
= pf.input :some_proposal_request_attribute
= f.buttons
Hope this helps. Make sure to look at all the examples at https://github.com/justinfrench/formtastic and do some trial and error to get your form how you want it.