how to pass params via link_to in activeadmin - ruby-on-rails

I have two questionaires.rb like this:
ActiveAdmin.register Questionaire do
menu :label => proc{ I18n.t("menu.admin.questionaire") }, :priority => 25
# actions :all, :except => [:create, :destroy]
filter :role
filter :name
config.comments = false
scope :all
def new
create!
end
index do
column :id
column :name
column :role
column :description
column do |ques|
link_to t("activeadmin.questionaire.add_question"), new_admin_question_path
end
column do |ques|
link_to t("activeadmin.questionaire.show_question"), admin_questions_path
end
default_actions
end
form do |f|
f.inputs :questionaire do
f.input :id, :as => :hidden
f.input :name
f.input :role
f.input :description
end
f.actions
end
end
and one questionaire has_many questions, and questions belong_to questionaire.
and in the questions table,I add this:
t.references :questionaire
add_index :questions, :questionaire_id
But when I click the link:
link_to t("activeadmin.questionaire.add_question"), new_admin_question_path
the questionaire_id is blank!
How can I get the questionaire_id, and when click each link of each questionaire to add questions, these questions just belong to their questionaire?
edit:
the results of questions and questionaires after rake routes:
batch_action_admin_questionaires POST /admin/questionaires/batch_action(.:format) admin/questionaires#batch_action
admin_questionaires GET /admin/questionaires(.:format) admin/questionaires#index
POST /admin/questionaires(.:format) admin/questionaires#create
new_admin_questionaire GET /admin/questionaires/new(.:format) admin/questionaires#new
edit_admin_questionaire GET /admin/questionaires/:id/edit(.:format) admin/questionaires#edit
admin_questionaire GET /admin/questionaires/:id(.:format) admin/questionaires#show
PUT /admin/questionaires/:id(.:format) admin/questionaires#update
DELETE /admin/questionaires/:id(.:format) admin/questionaires#destroy
batch_action_admin_questions POST /admin/questions/batch_action(.:format) admin/questions#batch_action
admin_questions GET /admin/questions(.:format) admin/questions#index
POST /admin/questions(.:format) admin/questions#create
new_admin_question GET /admin/questions/new(.:format) admin/questions#new
edit_admin_question GET /admin/questions/:id/edit(.:format) admin/questions#edit
admin_question GET /admin/questions/:id(.:format) admin/questions#show
PUT /admin/questions/:id(.:format) admin/questions#update
DELETE /admin/questions/:id(.:format) admin/questions#destroy

Related

Ruby on Rails and ActiveAdmin: add all object all at once instead add one by one

I'm using Ruby on Rails 5 API app with Active Admin.
I have a model called Subscription with has_many courses (Course is also a model).
When I add courses to a subscription, I need to do it one by one, like in this picture:
What I want is to have an option to add all the courses to a subscription all at once, instead of one by one. I tried using checkbox for multiple selection but it didn't worked. This is the code:
ActiveAdmin.register Subscription do
# require 'lib/app_languages.rb'
permit_params :name, :seat_limit, :domain, :language, :organization, course_ids: [], user_ids: [], subscription_courses_attributes: [:id, :course_id, :_destroy, :_create, :_update]
config.sort_order = 'id_asc'
# Index
index do
id_column
column :name
column :seat_limit
column :domain
column :organization
column :language do |subscription|
AppLanguages.languages[subscription.language]
end
column "Unlocked courses", :courses
column :created_at
actions
end
# Show
show do
attributes_table do
row :name
row :language do |subscription|
AppLanguages.languages[subscription.language]
end
row :seat_limit
row :domain
row :organization
row :created_at
end
panel 'Unlocked Courses' do
table_for subscription.courses do
column :id
column "Title" do |course|
link_to course.title, admin_course_path(course)
end
column "language" do |course|
AppLanguages.languages[course.language]
end
column :author
end
end
panel 'Subscribed users' do
table_for subscription.users do
column :id
column "name" do |user|
link_to user.name, admin_user_path(user)
end
column :email
column "language preference" do |user|
AppLanguages.languages[user.language]
end
end
end
end
# Edit
form do |f|
f.semantic_errors *f.object.errors.keys
f.inputs 'Details' do
f.input :name
f.input :language, :as => :select, :collection => AppLanguages.languages_array
f.input :seat_limit
f.input :domain
f.input :organization
end
f.has_many :subscription_courses do |sub_c|
sub_c.inputs "Unlocked Courses" do
if !sub_c.object.nil?
sub_c.input :_destroy, as: :boolean, label: "Destroy?"
end
sub_c.input :course ### should add here option for add all
end
end
f.inputs 'Subscribed users:' do
f.input :users
end
f.actions
end
end
Using for the array the as: :select form input with multi-select should help. Something like this:
sub_c.input :items, as: :select,
collection: courses_collection,
multiple: true
You can try using check_boxes
sub_c.input :items, as: :check_boxes, collection: courses_collection

Pass current_admin_user.id into permit_params

In my application I have 2 models: AdminUser, which has_many :announcements, and Announcement, which belongs_to :admin_user. The Announcement table has admin_user_id column in a database.
In app/admin/announcement.rb I have:
ActiveAdmin.register Announcement do
permit_params :title, :body, :admin_user_id
controller do
def new
#announcement = Announcement.new
#announcement.admin_user_id = current_admin_user.id
end
end
form do |f|
f.inputs do
f.input :title
f.input :body, as: :ckeditor
end
f.actions
end
end
Why my controller doesn't work? When I create an announcement through activeadmin, the column admin_user_id is empty. How can I solve this issue?
Just added to a form: f.input :admin_user_id, as: :hidden, and now everything is working.

Application helper to pass current client :id to collection form field?

I have a rails app that has following models:
Users, clients and invoices, In this app the user has the ability to create an invoice from the invoices index view and from the clients show view. Each Invoice is required to have the :client_id set on save. I am not calling client.find(params:id) in the invoices_controller new action because it cant be found when creating the invoice from the invoices index view.
What would be the best way to get the current client :id on client/id/invoice.new path? Should I pass client :id through in a helper method? Or can I do something cool in my new action or form that I am not seeing?
models
User
has_many :invoices
has_many :clients
Client
belongs_to :user
has_many :invoices
Invoice
belongs_to :client
belongs_to :user
In my invoices_controller new action I am just calling
def new
#invoice = Invoice.new
end
My new invoice _form
<%= f.input_field :client_id, :collection => Client.all, :label_method => :client_name, :value_method => :id, :include_blank => false, selected: #client_id, class: "form-control" %>
And my routes
resources :invoices, only: [:new, :create, :destroy, :index]
resources :clients do
resources :invoices, shallow: true
end
root 'invoices#index'
When I create a new invoice at clients/id/invoices/new, I want to set the default form field selector to be the current client's id from the URL?
You need to pass selected: value option right after you pass collection: [] to the input_field method like so:
<%= f.input_field :client_id, :collection => Client.all, :selected: #client_id, :label_method => :client_name, :value_method => :id, :include_blank => false, class: "form-control" %>
Edit:
You may need to pass #client variable in URL helper like following:
new_invoice_path(client_id: #client.id, back_path: client_path(#client))

updating two models from activeadmin

I have two models:
Model: Stores => Relavant fields: id, retailer_id
Model: Retailer => Relavant fields: name, id
The form I am trying to create is an activeadmin form which looks at the retailer name, then returns a list of stores that matches the name in check_boxes. What I want is the user the select the stores he wants associated with the retailer, and hit update, which will basically update the retailer_id in the stores model with the current retailer record.
If creating a new retailer, I want the checkboxes to show all stores where the retailer_id is blank.
In my retailer model, I have added
has_many :stores
accepts_nested_attributes_for :stores
in my stores model, I have added
belongs_to :retailer
here is what I currently have in my retailer activeadmin form
ActiveAdmin.register Retailer do
action_item do
link_to "View unassigned stores", "/admin/unassigned_stores"
end
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs do
f.input :name
f.input :description, :as => :text
f.input :photo, :label => "Retailer Logo", :as => :file, :hint => image_tag(retailer.photo.url)
f.input :retailer_id, :for => :Stores, :as => :check_boxes, :collection => Store.find(:all, :conditions => ["retailer_id is null and (name like ? or name_2 like ?) ", "%#{retailer.name}%", "%#{retailer.name}%"]), :label => "Assign Stores to retailer"
end
f.buttons
end
end
after a lot of hours searching around, came accross this little gem.
ActiveAdmin -- Show list of checkboxes for nested form instead of a form to add items
basically, i changed :retailer_id to :stores and added, :store_ids to attr:accessible and that allowed me to do what i was trying to do.

ActiveAdmin has_many relationship does not save new records?

I have the relation ContentRelease has_many pages. The following activeadmin registered model lets me edit properties of a page inside the content_release, but not create new pages for the content release. There is no error, I get a success message, but a new page is not saved. What am I doing wrong?
ActiveAdmin.register ContentRelease do
form do |f|
f.actions
f.inputs "Details" do
f.input :active_at
end
f.inputs "Pages", :class => 'pages-list' do
f.has_many :pages do |page|
page.inputs "Associated Look" do
page.input :title
page.input :content_release, :as => :select, :collection => ContentRelease.list
page.input :look, :as => :select, :collection => Look.current
page.input :share_url
end
end
end
f.inputs "Looks", :class => 'looks-list' do
f.has_many :looks do |look|
look.inputs do
look.input :title
end
end
end
f.actions
end

Resources