No route matches {:action=>"show" ... missing required keys: [:id] - ruby-on-rails-4

Seeing many question related to this but none of them gives answer to my problem.
I have Rails api application without ActiveRecord support. It is easy to reproduce problem. Follow steps:
Create rails api site without ActiveRecord support
rails new test001 -O --api
Change folder to test001 and run:
rails g scaffold testapp id name
Create model file testapp.rb in app/models folder
class Testapp
include ActiveModel::Model
attr_accessor :id, :name, :created_at, :updated_at
def self.all
t1 = Testapp.new(:id =>111, name: "t111")
return [t1]
end
end
Start server
rails s
Using postman REST client create GET request
http://localhost:3000/testapps.json
It fails with error ActionView::Template::Error (No route matches {:action=>"show", :controller=>"testapps", :format=>:json, :id=>#<Testapp:0x00000005411518 #id=111, #name="t111">} missing required keys: [:id]):
I have dummy implementation for POST, PUT, GET 1 item and all works. Here is dummy implementation of GET 1 item (/testapps/x.json)
def self.find(p)
return Testapp.new(:id =>p, name: "t123")
end
What is the problem with GET all (/testapps.json)?

Found solution.
Problem is scaffold generated index.json.jbuilder
file:
json.array!(#testapps) do |testapp|
json.extract! testapp, :id, :id, :name
json.url testapp_url(testapp, format: :json) #REMOVE
end
It added line json.url testapp_url(testapp, format: :json) for no reason. json.extract! deserialized object already.
Removing line solved problem.
I still do not know why testapp_url(testapp, format:json) caused error. Checking Rails Routing document http://guides.rubyonrails.org/routing.html

Related

Routes in Rails SAML IdP

I'm trying to get this gem to work with Rails 4 application that will serve as a SAML identity provider.
The thing that is confusing me is the routes and the template I assume should be rendered. In the gem controller, there is this:
def new
render template: "saml_idp/idp/new"
end
My routes are just the basic setup from the example, which I assume should match the action in my custom controller that inherits from the gem controller.
I have this in my controller.
class SamlIdpController < SamlIdp::IdpController
def idp_authenticate(email, password)
true
end
def idp_make_saml_response(user)
encode_SAMLResponse("you#example.com")
end
end
And my routes.rb file:
get '/saml/auth' => 'saml_idp#new'
get '/saml/metadata' => 'saml_idp#show'
So, what am I missing here? There should be a view rendered, instead I'm getting No Route Matches errors. Thanks.
As per Doc, I think you missed including SamlIdp::IdpController module
please include SamlIdp::IdpController rather than excluding.
Hope, It will work.
The new update for saml_idp gem wants to include SamlIdp::Controller as a module. And the controller class can inherit from ApplicationController
In your case it will be:
class SamlIdpController < ApplicationController
include SamlIdp::Controller
end

Ruby on Rails App Testing with Rspec and Capybara

when i create a feature test for my application get the following error:
ActionController::RoutingError:
No route matches [GET] "/example"
My application uses subdomains and sub-applications(engines/modules) within these subdomains. Now when i set for Capybara the app_host or default_host through an feature_subdomain_helper like
Capybara.app_host = "example.lvh.me" or
Capybara.default_host = "example.lvh.me"
and into my rails_helper.rb i add the following code line
config.extend SubdomainHelpers, type: :feature
I get the same error. Now i think the configured subdomain are not considered by my feature test.
My Rspec Version is: 3.2
and Capybara Version is: 2.4.4
My sample feature test looks like:
require 'rails_helper'
feature 'Example Page' do
scenario 'visit example page' do
visit "/example"
expect(page).to have_content 'Example'
end
end
Have someone an idea what i do wrong?
Edit:
Mainapp routes:
constraints(Subdomain) do
mount Example::Engine => '/', as: 'example'
end
Engine routes:
Example::Engine.routes.draw do
scope '/example', nav_scope: 'example' do
end
end
The names of Capybara.default_host and Capybara.app_host are slightly misleading since they both need to be set as URLs to function properly
Capybara.default_host = "http://example.lvh.me"
If that doesn't fix your issue check rake routes and make sure the action you think is mounted at '/example' really is.

NoMethodError in Admin::Programs#new

I recently installed ActiveAdmin.
I have added one Model successfully as an Active Admin Resource, and subsequently went to localhost:3000/admin and created a couple test objects.
When I add a second Model which belongs_to the first I get the following error, when I navigate through the dashboard and I try to create a new object of this latter model:
NoMethodError in Admin::Programs#new
Showing /Users/df/.rvm/gems/ruby-2.1.1/bundler/gems/active_admin- 7a2a31067e99/app/views/active_admin/resource/new.html.arb where line #1 raised:
undefined method `sss_center_id' for #<Program id: nil, name: nil, created_at: nil, updated_at: nil>
Extracted source (around line #1):
1
insert_tag renderer_for(:new)
N.B. SssCenter is the model name of the parent Model which has_many Programs (the second model that throws the error)
When I run rake routes I see that I have the following path:
new_admin_program GET /admin/programs/new(.:format) admin/programs#new
which is the path that is being called with the action admin/programs#new. My question is: where do you define controller methods that are namespaced with ActiveAdmin? I tried going to app/admin/ but I don't think you do it there.
If Program belongs to SSS Center then what you are missing in your program model is a column for the foreign key to SSS Center
To create that column you can begin by creating a new migration
rails g migration AddSssCenterToProgram
and editing this code inside of your db/migrate/<name_of_migration>.rb
class AddSssCenterToProgram < ActiveRecord::Migration
def change
add_column :programs, :sss_center_id, :integer
end
end
run rake db:migrate
Now you should have an attribute for program called sss_center_id to which the primary key of a specific SSS Center will be stored. You shouldn't have anymore problem associating it with your first model.

Rails 4 and Active Admin: ActiveModel::ForbiddenAttributesError

The full error is the following:
ActiveModel::ForbiddenAttributesError in Admin::ProductsController#create
My product model only has a name and price. Why is commit a parameter? When I click the 'Create Product' button within the admin dashboard, this is the params output:
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"6/pCeklsaik4sYF5h8+WRPddkH7wJn9ZJHd6YLaaNuc=",
"product"=>{"name"=>"Black Shirt Male",
"price"=>"25"},
"commit"=>"Create Product"}
From what I've gathered reading other Stack Overflow posts, you need to use strong parameters in Rails 4 instead of attr_accessible, which was done for me when I scaffolded the product model. In my create action in the Products controller, I have:
#product = Product.new(product_params)
And product_params is defined as:
def product_params
params.require(:product).permit(:name, :price)
end
I didn't do anything fancy when I created the model, and in my Gemfile I'm using the following as the documentation suggested for Rails 4:
gem 'stripe', :git => 'https://github.com/stripe/stripe-ruby'
Why am I getting this error when I create a new product via the Active Admin dashboard? Any input on the matter is appreciated.
Alright figured it out. I'm not sure if this is the 'correct' way but the products are being created.
in app/admin/product.rb I did:
permit_params :list, :of, :attributes, :on, :model, :name, :price
where
permit_params :list, :of, :attributes, :on, :model
was initially commented out. So I just added :name and :price
Question already answered, but I'm adding this as a helpful resource to supplement this answer:
https://github.com/activeadmin/active_admin/blob/master/docs/2-resource-customization.md#setting-up-strong-parameters

undefined method `get_taxonomies' when using a custom theme extension in 1.3?

i use the following gemfile:
gem 'spree_gateway', :github => 'spree/spree_gateway'
gem 'spree_auth_devise', :github => 'spree/spree_auth_devise', :branch => 'edge'
gem 'spree_theme_v1', :path => '../spree_theme_v1'
when using the last "spree_theme_v1" gem i created using the command "spree extension theme_v1", i can't browse the login and the account route anylonger. instead i get the following error message:
NoMethodError in Spree/user_sessions#new - undefined method `get_taxonomies' for
the error comes from the file views/spree/shared/_main_nav_bar.html.erb which i overwrite in my extension. as soon as i remove the file it works again. any advice, why this isn't working? or do i have to overwrite another file?
thanks!
The problem is that the gem is not loading the products_helper.rb from spree: https://github.com/spree/spree/tree/master/core/app/helpers/spree
You can make it work adding helper 'spree/products' to the controller that is failing (something about devise in your case).
I 'll show you my redefined PagesController from spree_essential_cms, maybe it helps:
class Spree::PagesController < Spree::BaseController
helper 'spree/products'
def show
...
end
end
As noted here http://osdir.com/ml/spree-user/2013-01/msg00053.html you can also make a decorator in app/controllers and call it something like add_products_helper_decorator.rb, and then fill it with this content:
[Spree::UserPasswordsController, Spree::UserRegistrationsController, Spree::UserSessionsController].each do |klass|
klass.class_eval do
helper 'spree/products'
end
end
That should be enough to require that helper into the right places and make it available for you.