Say I got some lists and some tasks. Each list can have many tasks, a task belongs to a list. When I editing one of the tasks rails is showing the id of the corresponding list in the inputfield.
How is it possible to show the title of the list instead of the id?
You probably want to use a select input, something similar to this in your form code:
<%= f.select :list_id, List.all.collect { |l| [l.name, l.id] } %>
This will display the name of each List, but will actually assign the id to the task's list_id
Related
I'm writing an ActiveAdmin form for a record which takes an Array of Strings. Let's call them widgets. I'm using a multi select form like so:
ActiveAdmin.register Things do
permit_params widgets[]
form do |f|
f.inputs do
...
f.input :widgets,
as: :select,
collection: [],
multiple: true
end
end
end
With a bit of Javascript to help the user can enter their widgets and it will be delivered as an Array of Strings just fine. But when editing an existing record the existing record.widgets are not filled into the field, the widgets field is blank. I suspect what's happening is ActiveAdmin/Formtastic is interpreting this select as a has_many relationship with Widget, and the Strings as Widget IDs. ActiveAdmin is ignoring the failure.
How can I write an input to pass a plain Array of Strings into a model with ActiveAdmin? I'd like my parameters as params[:thing][:widgets] = ["foo", "bar"]
By providing collection: [], you are telling the form to always start with blank array.
You need to provide the actual Thing's widgets to the form. You'll need to modify the example to play well with the select box / javascript you wrote - just bear in mind that collection takes an array - collection: resource.widgets.split(',').
I have a Rails 3.2 project where:
Product has_many Descriptions
These Descriptions have a description_type and a language_code. Different types of Products can have different types and numbers of Descriptions, so that a Product of type 'Catalogwill have 4 different Descriptions, where a product of typeWholesale` will have only 2.
Upon the creation of a Product, an after_create method creates the different descriptions for the product, filling in only the description_type. The idea is that the user then goes in and fills in the different description values.
Now I am in the situation where I have to dynamically build some complex form with a different number of descriptions, then pass all those description values via the controller to some class method on either Product or Description to persist the data, and it just ends up as spaghetti everywhere.
I am convinced that I'm thinking of the problem wrong. I can't unthink it though, as this is one box I seem to put myself in over and over.
Is there some pattern that I can apply to the problem domain of nesting these child attributes in an existing form? I have to do it on Edit, because I don't know how many fields to create until the product_type is chosen. Again, I'm open to doing this completely differently.
I always run into this problem, and it ties me in knots. I am convinced that I am approachin
Just today I had to rebuild a Member form in our app, where a Member belongs to a User which has many PhoneNumbers.
The Member model accepts_nested_attributes_for :user and the User model accepts_nested_attributes_for :phone_numbers
Using the cocoon gem, I am able to do the following in my Member form:
# phone number fields in members/_form.html.haml
# if you do use this on a new (know you said edit but just in case) ... be sure to instantiate the association
# in my case: #member.user.phone_numbers.build
# u represents the User part of the form ... typically 'f'
= u.simple_fields_for :phone_numbers do |phone|
= render 'users/phone_number_fields', f: phone
.add-phone-link-wrapper.pull-right
= link_to_add_association 'Add Phone', u, :phone_numbers, class: 'btn btn-orange btn-mini add-remove-links', partial: 'users/phone_number_fields', render_options: { wrapper: 'bootstrap' }
# 'users/phone_number_fields' partial
.nested-fields
= f.input :phone
= f.input :phone_label, as: :select, collection: PhoneNumber::LABELS
= f.input :display_number, as: :boolean
= link_to_remove_association 'Remove Phone', f, class: 'btn btn-red btn-mini add-remove-links pull-right', render_options: { wrapper: 'bootstrap' }
Note: I use SimpleForm and cocoon has some differences in implementation depending on your form builder (all on their github page/wiki).
For our needs, this fit perfectly.
I am using a simpleform collection association input, as checkboxes in order to allow users to choose 1+ items from a list as part of an order. The list has a has_and_belongs_to_many association with the overall order. I want them to be able to do multiple of the same items, however. So I would want a small number input next to each checkbox. I can handle the javascript, I am just wondering how to do this with simpleform, if its even possible.
Thanks!
It is possible if you write a custom input for this specific task.
You would need to put it in # app/inputs/your_input.rb for Simple_form to automatically pick it up.
Then in the file :
class YourInput < SimpleForm::Inputs::Base
def input
# Your code here, but I just pasted the example that adds something after the existing field already as a reference.
"$ #{#builder.text_field(attribute_name, input_html_options)}".html_safe
end
end
and in the form :
f.input :money, as: :your
In case of checkbox, the method name will change to "check_boxes" from "input"
This is my first web app that i am developing and i have some design questions, i have a few book about RoR3 but i dont seem to find answers to my questions.
My application is based on Ruby on rails 3
I am not looking for detailed answers here, if you can just point me to a topic name that i could learn about that can answer my qustions, such as "names resources" , "hidden fields" .....
My questions:
1- How to send information between View A and controller B. Example, i am on the View for "Company" when i click create employee i am calling the "new view" for the employee so i am now on a different view, how can i pass to the new employee action the ID of the company? Since i am now on a different view ? i don’t want to use nested resources
What are the different ways to send information across different controllers/views
2- ruby URLs: i can view am item in my model via the URL: http://localhost:3000/Companies/1
I don’t want the url to contain the index of the item, each company has a name and i want to display this name in the url such as http://localhost:3000/Companies/myCompany
How can i change the url structure of rails?
For your first question, you can pass the parameters with the link (assuming you have employee and company variables accessible to your view):
Edit: this should work:
= link_to "create employee", :controller => "employees", :action => "new", :company_id => #company.id
And in the Employees controller:
def new
company_id = params[:company_id]
# check that company_id is not nil before doing stuff with it
end
I'm not sure why doing this ignores any extra parameters:
= link_to "create employee", new_employee_path, :company_id => #company.id
For your second question, this is what you're looking for.
Pass the company id as parameters at create employee link .
In the controller ,receive the params like
def new
company_id = params[:company_id]
....
end
For using some string instead of id , use gem 'friendly_id'
Make sure your reference string should be unique.
What I use rails form helpers to build a form, input elements in that form have id attributes so they can match with their labels. The problem is this id is the something like person_first_name and not person_first_name_#{person.id} meaning if I have more than one form of the same type on the same page unexpected things can happen.
A perfect example of this is using jquery-ui datepicker. I have a series of forms all containing a text_field element wrapped in a div with the class datepicker. I apply the datepicker like this (in document ready) $('.datepicker input').datepicker(options) and guess what, every one of these elements, although has a seemingly working datepicker (click on input, datepicker appears), although when a date is selected in any of these datepickers only the first element on the page (of that element type, ex. input id=published_on) gets updated with the value.
Any suggestions on getting rails to output more unique element id's or make datepicker not use the id attribute?
You can easily customize the id of your input.
text_field(:person, :first_name, :id => "person_first_name_#{person.id}")
One would think that if you set the ID of the form to say :id => "edit_person_#{person.id}" that this ID would cascade downwards into the label and field logic. From what I can see, there's too much auto-magic in the way of adding an eloquent custom solution.
# Haml
= form_for #model do |f|
.field
= f.label :field_name
= f.text_field :field_name
When using the above example, ids are rendered as expected.