Rails Admin throws No route matches error - ruby-on-rails

I have this configuration:
question.rb
class Question
belongs_to :asker
belongs_to :expert
end
user.rb
class User
has_one :asker
has_one :expert
end
I've setup rails_admin and I'm not able to edit any question because of the error.
ActionController::UrlGenerationError at /question/764/edit
No route matches {:action=>"show", :controller=>"rails_admin/main", :id=>nil, :model_name=>"asker"}
I've tried adding a show method in the askers_controller.rb, but it still doesn't make any difference.
Thanks.

Do you have edit method in askers_controller ?
When you try to edit something it should have path like this edit_admin_news_letter GET /admin/news_letters/:id/edit

Related

Rails ActiveAdmin has_one and belongs_to causes 'Undefined Method for <PLURAL_RESOURCE>'

In Rails, I have a 'User' model and a 'Wallet' model. A 'User' has_one wallet and each 'Wallet' belongs_to a 'User'. I made a 'Show' page in ActiveAdmin to view a User's Wallet. However, going to that page returns this error:
undefined method `wallets' for #<User:0x007f...>
HOWEVER, when I update the User model to 'has_many :wallets' instead of ':has_one wallet', everything works. Here is the relevant code from my models and ActiveAdmin code:
Models:
class User < ActiveRecord::Base
has_one :wallet, dependent: :destroy
end
class Wallet < ActiveRecord::Base
belongs_to :user
end
ActiveAdmin:
ActiveAdmin.register Wallet do
belongs_to :user
actions :all, except: :destroy
show do
div do
'hello'
end
end
end
ActiveAdmin.register User do
actions :all, except: :destroy
permit_params
action_item :wallet, only: :show do
link_to('Wallet', admin_user_wallet_path(user, user.wallet.id))
end
index do...
end
Any ideas as to where I might have gone wrong?
Edit 1: updates to correct colon placement mistakes in description
Edit 2:
in response to:
Can you show your routes file? Also, can you give us the full traceback of the error message and give us the output of rake routes? I suspect that the reason it's complaining about wallets not being defined (even though you never call wallets in the above code) is that some routing is making assumptions about how the relationships look. – Glyoko 4 mins ago
My routes file contains no mention 'wallet' or 'wallets'.
My stack error more specifically looks like this:
activemodel (4.1.15) lib/active_model/attribute_methods.rb, line 435
Let me know if you need more than that.
Here's the related output from 'bin/rake routes':
admin_user_wallets GET /admin/users/:user_id/wallets(.:format) admin/wallets#index
POST /admin/users/:user_id/wallets(.:format) admin/wallets#create
new_admin_user_wallet GET /admin/users/:user_id/wallets/new(.:format) admin/wallets#new
edit_admin_user_wallet GET /admin/users/:user_id/wallets/:id/edit(.:format) admin/wallets#edit
admin_user_wallet GET
/admin/users/:user_id/wallets/:id(.:format) admin/wallets#show
admin_user_wallet PATCH /admin/users/:user_id/wallets/:id(.:format) admin/wallets#update
admin_user_wallet PUT /admin/users/:user_id/wallets/:id(.:format) admin/wallets#update
ActiveAdmin uses InheritedResources gem internally, the belongs_to method ends up inside InheritedResources.
Possible solution here
ActiveAdmin.register Wallet do
belongs_to :user, singleton: true
actions :all, except: :destroy
end
The option singleton: true makes the Wallet a singular resource for the User.
Probably, another option optional: true may be helpful if Wallet is not required for any User to present
Even though your routes may not explicitly reference wallet(s), there may be something there making assumptions about how records are related to each other.
Look at the output of rake routes, in particular:
admin_user_wallet GET
/admin/users/:user_id/wallets/:id(.:format) admin/wallets#show
When you call admin_user_wallet_path(user, user.wallet.id) it's matching the /admin/users/:user_id/wallets/:id(.:format) route. Notice how that expects both a user id and a wallet id in the path. This is a tip-off that something is off here, since if you have the user, there should be exactly one wallet associated with it. You shouldn't need to give both the user and wallet id.
Since the wallet resource is nested under users, the page where you view the user's wallet is actually more of an index page than a show. If the wallet were an independent resource, then you could have a path like /admin/wallets/:id and things would work out fine.
But since the wallet is a subresource of the user, you would ideally want a path like /admin/users/:user_id/wallet. There's no need to pass the wallet id, since you already have the user.
tl;dr: Try changing the shows to indexs and see where that gets you. e.g.
index do
div do
'hello'
end
end
# ...
action_item :wallet, only: :index do
link_to('Wallet', admin_user_wallets_path(user))
end
Okay.. So I had this same exact issue. I had a belongs_to a parent where the parent had only a has_one to the child model..... Nothing seemed to work so I decided to fake it. I am not sure if this is the best way to do this but it worked. In the parent model, add a method:
class User < ActiveRecord::Base
has_one :wallet
def wallets
Wallet.where(user_id: id)
end
end
The above code is a hotfix until I can find some other way to implement what I need.

Nil class error for association

I have a model Tutorial and has_many association Tutorialcategory where tutorials can be connected to many tutorial categories. Tutorials model and controller are working fine but categories are not. I am able to add categories but can not edit them, I am getting 'undefined method `model_name' for NilClass:Class' error while accessing edit page for tutorialcategory.
I have defined route as follows
resources :tutorialcategories do
end
following is my model definition
class Tutorial < ActiveRecord::Base
has_many :tutorialcategories
If you can add categories but not edit them then you must load a #tutorial_category variable in your edit action, something like this:
def edit
#tutorial_category = TutorialCategory.find params[:id]
end

Why does rails create nested resource routes with the same parameters in multiple places?

I have a model parent that has many children, and the children belongs to a parent
class Parent < ActiveRecord::Base
has_many :children
end
class Child < ActiveRecord::Base
belongs_to :parent
end
I have nested the routes like this
resources :parents do
resources :children
end
When I run the rake routes command, I get this:
parent_children GET /parents/:parent_id/children(.:format) children#index
POST /parents/:parent_id/children(.:format) children#create
new_parent_child GET /parents/:parent_id/children/new(.:format) children#new
edit_parent_child GET /parents/:parent_id/children/:id/edit(.:format) children#edit
parent_child GET /parents/:parent_id/children/:id(.:format) children#show
PATCH /parents/:parent_id/children/:id(.:format) children#update
PUT /parents/:parent_id/children/:id(.:format) children#update
DELETE /parents/:parent_id/children/:id(.:format) children#destroy
parents GET /parents(.:format) parents#index
POST /parents(.:format) parents#create
new_parent GET /parents/new(.:format) parents#new
edit_parent GET /parents/:id/edit(.:format) parents#edit
parent GET /parents/:id(.:format) parents#show
PATCH /parents/:id(.:format) parents#update
PUT /parents/:id(.:format) parents#update
DELETE /parents/:id(.:format) parents#destroy
Notice that the params[:id] is different in the parents_controller than in the childrens_controller. Is there a good reason for this? Should it not just be params[:parent_id] and params[:child_id]?
Currently I can't link from one controller to another, without raising an exception:
No route matches {:action=>"new", :controller=>"parents", :id=>"3"} missing required keys: [:parent_id]
I do understand this error message, and I have found a not-very-pretty-hack to get around it. But how would you solve this? Am I completely missing something here?
EDIT:
The link is from parent#show to children#new and looks like this:
link_to 'new child', new_parent_child_path
<%= link_to 'new child', new_parent_child_path(#parent) %>
where #parent is the parent object.
is that not working for you?
I'm not sure how, but I think you are passing :id instead of :parent_id somehow.

Generated scaffold's view links are not corresponding correctly to the routes map

I have a problem with generated view paths. My routes.rb looks like following
Project::Application.routes.draw do
resources :project_templates do
resources :awards
end
...
project_template.rb like this
class ProjectTemplate < ActiveRecord::Base
belongs_to :user
has_many :awards #...
attr_accessible :user_id #...
...
award.rb like
class Award < ActiveRecord::Base
belongs_to :project_template
attr_accessible :tier #..
...
And generated view links are like this: awards_path
This way app does not work and I need to replace all with project_template_awards_path
I don't know why generator did this without project_template prefix but I ask for you to help me find a way to get around this. Maybe there is some generator command that will add up the missing suffixes to paths? I have to do the same with another class requirement.rb and there are views for that too so I hope there is some magic command for solving my issue.
rake routes | grep awards gives following output:
project_template_awards GET /project_templates/:project_template_id/awards(.:format) awards#index
POST /project_templates/:project_template_id/awards(.:format) awards#create
new_project_template_award GET /project_templates/:project_template_id/awards/new(.:format) awards#new
edit_project_template_award GET /project_templates/:project_template_id/awards/:id/edit(.:format) awards#edit
project_template_award GET /project_templates/:project_template_id/awards/:id(.:format) awards#show
PUT /project_templates/:project_template_id/awards/:id(.:format) awards#update
DELETE /project_templates/:project_template_id/awards/:id(.:format) awards#destroy
Could you try typing $ rake routes in project directory and post there output? Does it still show awards_path for Awards resources?
PS. Controllers have nothing to do here, as you can create route resources without corresponding controllers in app/.
The routes generated are correct. If you want to have this helpers, you can either add this to your route.rb
Project::Application.routes.draw do
resources :project_templates
resources :awards
end
Or implement helpers like
def awards_path(award)
project_template_awards(awards.project_template, awards)
end
More info on ruby guides

undefined method `new' for nil:NilClass - what's wrong with has_one nested association in rails 3.1.0?

There are quote and test_plan in our app. The relationship is quote has_one test_plan and test_plan belongs_to a quote. In routes file it is:
resources :test_plans, :only => [:index]
resources :quotes do
resources :test_plans
end
The problem is that #quote.test_plan returns nil (instead of an object) and #quote.test_plan.new() causes error saying undefined method new(). Any thoughts about the problem? Thanks so much.
#test_plan = #quote.build_test_plan
creates a #test_plan object with foreign key set by #quote. This solves the problem.

Resources