ActiveAdmin association doesn't update its attributes in save - ruby-on-rails

I can't get ActiveAdmin to save the associated model when the initial model is saved.
I have two models that look like this:
# app/models/account.rb
class Account < ActiveRecord::Base
has_one :endpoint, inverse_of :account, class_name: 'Abcd::Endpoint'
accepts_nested_attributes_for :endpoint
delegate :access_key, to: :endpoint
end
# app/models/abcd/endpoint.rb
class Abcd::Endpoint < ActiveRecord::Base
attr_accessible :account_id, :access_key
belongs_to :account
end
My ActiveAdmin file looks like:
# app/admin/account.rb
Activeadmin.register Account do
form do |f|
f.inputs do
f.input :name
end
f.inputs title: 'endpoints', for: [:endpoint. f.object.endpoint || Endpoint.new] do |nested_form|
nested_form.input :access_key,
label: 'Access Key',
as: :string
end
f.actions
end
show do |account|
row 'endpoint has access_key' do
account.access_key
end
end
end
When I click on "Update Account" the Account gets updated but the Endpoint
model doesn't get updated. It appears that the Endpoint attributes aren't
being sent to the Endpoint model.
Does anyone know how to get the Endpoint model to get updated with its
attributes or what I need to fix?

give it a try
form do |f|
f.inputs "Account" do
f.input :name
end
f.inputs do
f.has_one :endpoint, inverse_of :account, class_name: 'Abcd::Endpoint' do |nested_form|
nested_form.input :access_key,
nested_form.label('Access key')
end
end
f.actions
end

Related

Unpermitted parameters for active_admin Nested Form

I'm using Nested form in Active admin resource but value of nested attribute not save in model.
here is detail of my models -
class Exceed::Assessment < ActiveRecord::Base
has_many :assessment_infos, :class_name => "Exceed::AssessmentInfo", :dependent => :destroy
accepts_nested_attributes_for :assessment_infos, allow_destroy: true
end
next model -
class Exceed::AssessmentInfo < ActiveRecord::Base
belongs_to :assessment
end
and here is the active admin resource of Exceed::Assessment
ActiveAdmin.register Exceed::Assessment do
form do |f|
f.inputs "Exceed Details" do
f.input :exceed_id, :label => 'Exceed Type', :as => :select, :collection => Exceed.all.map{|s| ["#{s.exceed_type}", s.id]}
f.input :exceed_section_id, :label => 'section', :as => :select, :collection => ExceedSection.all.map{|s| ["#{s.section}", s.id]}
f.input :guideline, label: "Section Guideline"
end
f.has_many :assessment_infos do |q|
q.input :information
end
f.actions
end
controller do
def permitted_params
params.permit exceed_assessment: [:exceed_id, :exceed_section_id, :guideline],
assessment_infos_attributes: [:information]
end
end
end
from my active admin form I fill details of exceed_assessment and nested form assessment_info.
Exceed_assessment details are successfully save in model but assessment_info not save.
when I check it on console it show error message -
Unpermitted parameters: assessment_infos_attributes
Unpermitted parameters: utf8, authenticity_token, commit
I did some silly mistakes, after correct them its working.
Firstly, in my Assessment_info model there is a column 'exceed_assessment_id'. I change it to 'assessment_id' because my model is belongs_to Assessment model.
Second I slightly change syntax of Permit params, like this -
controller do
def permitted_params
params.permit exceed_assessment: [:id, :exceed_id, :exceed_section_id, :guideline,
assessment_infos_attributes: [:assessment_id, :information],]
end
end

Rails Active Admin when I create my form it only displays the last field. Permit params wont accept :page field

I am having 2 problems.
1) formtastic will only show the last input field instead of all of them. In this case it will only display:
r.input :sort_order
2) I had to do some wierd wrap f.inputs around each field to get it to show up which i believe is the wrong way. But when i submit the form it says Unpermitted parameters: page. When I did clearly define page I dont know how else to get permit params accept this.
Here is my model
class Fact < ActiveRecord::Base
has_one :page, as: :pageable, dependent: :destroy
accepts_nested_attributes_for :page
end
The other model:
class Page < ActiveRecord::Base
belongs_to :pageable, polymorphic: true
end
My active admin:
ActiveAdmin.register Fact do
permit_params :id, page_attributes: [:type, :name, :description :sort_order ]
form do |f|
f.inputs "My Page", for: [:page, f.object.page || Page.new] do |r|
r.input :name
r.input :description
r.input :sort_order
end
end
end

How can I display nested records on activeadmin?

So far I have two models, user and profile
Class User < ActiveRecord::Base
has_one :profile
accepts_nested_attributes_for :profile
Class Profile < ActiveRecord::Base
belongs_to :user
On my active admin model
form do |f|
f.inputs "User" do
f.input :email
#code to get the profile data
end
f.action :submit
end
So I want to get the profile data on the user form I have tryed couple of things but I wasn't able to get them.
Something like
f.inputs 'Profile', :for => [:profile, f.object.profile || Profile.new] do |profile_form|
profile_form.input ...
...
end
inside the f.inputs "User" block should work. Just set up the attributes of Profile inside this block using profile_form.

uninitialized constant on nested resource form

I have three models, users, reports, and receipts. Users have many reports, and reports have many receipts.
Now, I have a form set up to create, or edit reports. And I need to nest another form to create and edit receipts. I followed the rails guide (section - building a multi modeled form) and edited my models, and have added the build line into my form view but Im getting that 'uninitialized constant' error.
Here are my models:
class Report < ActiveRecord::Base
belongs_to :user
has_many :receipts
attr_accessible :cash_advance, :company, :description, :end_date, :mileage, :report_name,
:start_date, :receipts_attributes
validates_presence_of :company, :description, :end_date, :report_name#, :start_date
validates_uniqueness_of :report_name
accepts_nested_attributes_for :receipts, :allow_destroy => :true,
:reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }
end
class Receipts < ActiveRecord::Base
belongs_to :report
attr_accessible :account_code, :amount, :company_card, :date, :description, :lobbying_expense, :vendor
end
and my form:
<%# #report.receipts.build %>
<%= form_for([current_user,#report]) do |f| %>
...
<%= f.fields_for ([#report, #report.receipts.build ]) do |receipt| %>
...
<% end %>
<% end %>
my routes (which Im not sure if I should have edited, but I got the same error before I added the receipts resources)
resources :users do
resources :reports do
resources :receipts
end
end
I didnt edit the reports controller since the rails guide didnt show any mention of it, its its only:
def new
#report = current_user.reports.new
end
def edit
#report = current_user.reports.find(params[:id])
end
What am I doing wrong?
edit - I changed my form for the receipts so the form_for takes in [#report, #report.receipts.build] but now I get the error:
uninitialized constant Report::Receipt
How do I get this form to work?
UGH! I messed up when I generated the model and gave it a plural name instead of a singular name. This guy, right here, is a fool.

creating new association while creating the related record in active admin

I've these two models
class Case < ActiveRecord::Base
belongs_to :client, :class_name => 'User'
end
class User < ActiveRecord::Base
has_one :requested_case, :class_name => 'Case', :foreign_key => :requested_case_id
end
and I want to create adminstration Interface for Case model using Active Admin, so when I create new case I can create new client for it in the same time, so I wrote the following lines of codes in the app/admin/cases.rb file
ActiveAdmin.register Case do
form do |f|
f.inputs "Basic Details"
f.input :title
f.input :Description
end
f.inputs :name => "Client Details", :for => :client do |c|
c.input :name
c.input :mobile
end
f.buttons
end
end
so when I filed the inputs of client and click submit I got this error
ActiveRecord::AssociationTypeMismatch in Admin::CasesController#create
User(#-625154418) expected, got ActiveSupport::HashWithIndifferentAccess(#82665960)
so any help please what's missing here?
Just add to your app/admin/cases.rb file
controller do
def new
#case = Case.new
#case.build_client
end
end
and don't forget to add accepts_nested_attributes_for to your case model
accepts_nested_attributes_for :client

Resources