I have two related models, Bunny has_many BunnyData (which belongs_to Bunny). From the show page of a particular Bunny (in Active Admin), I want to create a link to make a related BunnyData. I've tried a few different ways, with no success, and am currently trying this:
sidebar :data, :only => :show do
link_to 'New Data', new_admin_bunny_datum(:bunny_id => bunny.id)
end
The link being generated ends up as something like:
.../admin/bunny_data/new?bunny_id=5
But when you go to that page, the dropdown for Bunny is set to the blank default as opposed to showing the name of Bunny with ID 5.
Thanks in advance.
Rails namespaces form fields to the data model, in this case BunnyData. For the form to be pre-filled, any fields provided must also include the namespace. As an example:
ActiveAdmin.register Post do
form do |f|
f.inputs "Post Details" do
f.input :user
f.input :title
f.input :content
end
f.actions
end
end
The fields can be pre-filled by passing a hash to the path helper.
link_to 'New Post', new_admin_post_path(:post => { :user_id => user.id })
Which would generate the following path and set the form field.
/admin/posts/new?post[user_id]=5
In the case of BunnyData, it might be slightly different due to the singular and plural forms of datum. But that can be verified by inspecting the generated HTML to find the name attribute of the inputs.
Related
I have an ActiveAdmin form to create a new ActiveRecord object, and the form is capable of also creating records for one of the "main" model's has_many associations, i.e.:
class Car < ApplicationModel
has_many :tires
end
class Tire < ApplicationModel
belongs_to :car
end
# ... in ActiveAdmin "cars.rb" file...
ActiveAdmin.register Car do
...
form do |f|
f.semantic_errors
f.inputs 'Car' do
f.input :color
f.input :doors, as: :number
f.inputs do
f.has_many :tires, allow_destroy: true, new_record: true do |t|
t.input :type
t.input :size
end
end
end
f.actions
end
end
There is also some controller logic not listed here that properly creates, edits, or destroys these records, including Cars and Tires. When the form renders (let's say I'm creating a new Car), there is a button at the bottom that says "Add New Tire". Clicking it renders a sub-form on the page with two fields: "Type" and "Size". I've set up the controllers to save both the Car and its associated Tires when I submit the form. That all works.
I'm struggling with an automated test to ensure that clicking the "Add New Tire" button causes the sub-form to render. I've added a breakpoint after clicking the button and inspected page.body to see if the sub-form exists, but it doesn't seem to be there - I only see what looks like a template of the sub-form in the button's (really an a element) code. This is the part of my test code that fails:
click_link 'New Car'
fill_in('Color', with: 'Blue')
fill_in('Doors', with: 2)
click_link('Add New Tire') # Succeeds
fill_in('Type', with: 'All-Terrain') # Fails, can't find element
I've tried other ways to match on the first sub-form field, like xpath and css selectors, but it just can't find the field, and like I mentioned above, I don't see an instance of the sub-form being rendered if I inspect the page with page.body. I'm stuck - can anyone help me figure out what's going on here?
I got a tip that adding js: true to the test definition could help, and as it turns out, it did:
RSpec.describe Car, js: true do
...
end
I'm trying to manually tell formtastic to check a few checkboxes. #some_array currently has an element called checked which exists for each member.
= f.input :cboxes, label: "CBoxes", as: :check_boxes,
collection: #some_array.map { |a| [a[:name], a[:id]] }
I've tried to set the input_html to { checked: 'checked' } (How to pre-check checkboxes in formtastic) but this checks all checkboxes, not just the select few that I want.
The contents of #some_array are coming via an API, and I can't change the database structure (Ruby on Rails + Formtastic: Not checking checkboxes for multiple checked answers)
Suggestions?
If you are editing an ActiveModel, you don't need to "manually select checkboxes".
Let's consider a simple example with a single User model which has fields username and roles. Roles field is a string column, which Rails serializes as an Array. It might also be has_many relation to other ActiveModel, but we assume it's an Array for simplicity.
User is defined in user.rb:
class User < ActiveRecord::Base
serialize :roles, Array
end
Now you can "assign manually" desired roles to User in your controller:
#user = User.new(username: 'dimakura', roles: ['admin', 'editor'])
and define form in your view:
<%= semantic_form_for #user do |f| %>
<%= f.input :username %>
<%= f.input :roles, as: :check_boxes, collection: ['owner', 'admin', 'editor', 'viewer'] %>
<% end %>
In given example only "admin" and "editor" roles will be pre-selected in form. The "owner" and "viewer" role won't be selected.
Update Official documentation states:
Formtastic, much like Rails, is very ActiveRecord-centric.
But actually it's not a big challenge to create ActiveRecord-compatible model yourself. An example of doing this can be found in this blog post.
I have 2 models, one is called Hotel and one is called HotelInfo.
When a user saves an Hotel resource in ActiveAdmin I want it to force the user to fill out the form for HotelInfo as well before it can be saved.
If possible, how can I do this?
Here is an example code that how to add two models together in active admin.
form do |f|
f.inputs "Hotel" do
f.input :name
end
f.inputs "Hotel Information", :for => [:hotel_info, f.object.hotel_info || HotelInfo.new] do |hotel_info_form|
hotel_info_form.input :telephone
end
f.buttons
end
You could use a fields_for helper to have both resources on the same form. Make sure your parent object has accepts_nested_attributes_for :hotelinfo set.
I have a Post model which I'm accessing through ActiveAdmin. It's also taggable using the acts_as_taggable_on gem. So the admin can add, edit or delete tags from a specific Post.
The normal way to add the tagging functionality for the resource in your admin panel is by doing this in admin/posts.rb:
ActiveAdmin.register Post do
form do |f|
f.inputs "Details", :multipart => true do
f.input :tag_list
# and the other irrelevant fields goes here
end
f.buttons
end
end
However, I want to have the tags selected from a multiple select form field and not being entered manually in a text field (like it is with the code above). So I've tried doing this:
f.input :tag_list, :as => :select,
:multiple => :true,
:collection => ActsAsTaggableOn::Tag.all
but it doesn't work as expected. This actually creates new tags with some integer values for names and assigns them to that Post. Someone told me that extra code is needed for this to work.
Any clues on how this is done? Here's my model just in case: http://pastie.org/3911123
Thanks in advance.
Instead of
:collection => ActsAsTaggableOn::Tag.all
try
:collection => ActsAsTaggableOn::Tag.pluck(:name)
Setting the collection to Tag.all is going to tag your posts with the tag's ID, since that's how tags are identified by default (that's where the integer values for names are coming from). map(&:name) tells the form builder to use the tag's name instead.
I'm working on a basic garden logging application which consists of gardens, plants and planted plants. Each user has one or many gardens, plants are master definitions, and a planted plant can be thought of as an instance of a plant in a specific user's garden.
In my routes.rb file I have the following:
map.resources :gardens do |gardens|
gardens.resources :planted_plants, :has_many => :plant_log_entries, :collection => { :filter => :post, :choose_garden => :post}
gardens.resources :garden_log_entries
end
map.resources :plants
This makes sense to me when retrieving a list of planted_plants in a user's garden, but I'd like to create a planted_plant record from the index of plant. The problem is, a user can have multiple gardens. How can I create a new form for a planted_plant that allows the user to specify which garden should be used?
The current route requires a garden_id - which makes sense for retrieval, but I'd like to supply that as a parameter for creation.
Thanks in advance for any help!
I would add another (non-nested) route, to allow access to the PlantedPlantsController#create action without specifying the garden_id in the URL:
map.resources :planted_plants, :only => :create
This will allow you to post your form to /planted_plants. As for the form itself, you'll probably need something like this:
<% form_for #planted_plant do |p| %>
<%=p.label "garden_id", "Garden" %>
<%=p.collection_select :garden_id, current_user.gardens, :id, :name %>
... other fields ...
<% end %>