I am using simple_form on a project where I have the following associations
Company
has_many :users
has_many :projects
User
belongs_to :company
has_many :tasks
Project
belongs_to :company
has_many :tasks
Task
belongs_to :project
belongs_to :user
I am using a simple_form association input like so:
<%= f.association :user, :prompt => "Assign To...", :label_method => :first_name, :value_method => :id %>
But I need it to list ONLY the users who have the correct company_id (the same that the project belongs to). Is there a way to do this? I have done some googling and have come up with nothing so far.
I found an in-line way to handle it:
<%= f.association :user, :collection => User.where(:company_id => current_user.company_id), :prompt => "Assign To...", :label_method => :first_name, :value_method => :id %>
Only shows users who's company_id match that of the current user (which matches that of the project)
You should extract all the users , that belong to the company , and pass it to the list for assignment , is that right ? In your method (helper or model)
#company = Company.find(params[:id])
#labors = #company.users
It should return an array of instance objects , which you can use in the list .
Related
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))
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.
In my Rails application, I have the following model:
class Idea < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
has_many :ideas
end
I am creating ActiveAdmin CRUD for my Idea model with the custom form that looks something like that looks something like that:
form do |f|
f.inputs do
f.input :member
f.input :description
end
end
The requirement is to have the custom text for a content of the member association, i.e "#{last_name}, #{first_name}". Is it possible to customize my member select box to achieve it?
Any help will be appreciated.
Yes, that is possible. I assume you want to use a DropDown list box for members to select a user from User model.
form do |f|
f.inputs do
f.input :user_id, :label => 'Member', :as => :select, :collection => User.all.map{|u| ["#{u.last_name}, #{u.first_name}", u.id]}
f.input :description
end
end
For Active Admin You have to pass it as a collection of Hashes. Key in hash will be the text which you want to display and value will be the attribute id.
For Example:
f.input :user_id, :label => 'Member', :as => :select, :collection => User.all.map{|u| ["#{u.last_name}, #{u.first_name}", u.id]}.to_h
collection: [{name1: 1}, {name2: 2}, {name3: 3}]
Note: I have added to_h at end of the map which will convert a collection of arrays into a collection of hashes.
I'm using Simple Form and don't how you would show the values of 2 associations.
A Price can belong to a service or product but not both at the same time.
Price
# service_id, product_id
belongs_to :services # service.name
belongs_to :products # product.name
end
Instead having my simple form look like this:
<%= f.association :product, :input_html => { :class => "span5 } %>
<%= f.association :service, :input_html => { :class => "span5 } %>
I want to turn it into one field instead.
What is the way with simple_form_for?
What about with a regular form_for?
I think better way to do is to use polymorphic association.
class Price
belongs_to :pricable, polymorphic: true
end
class Product
has_one :price, as: :priceable
end
class Service
has_one :price, as: :priceable
end
Then, in your form, you can use:
<%= form_for [#priceable, Price.new]
where #priceable is a product or a service.
I've been trying to figure this one out for a while but still no luck. I have a company_relationships table that joins Companies and People, storing an extra field to describe the nature of the relationship called 'corp_credit_id'. I can get the forms working fine to add company_relationships for a Person, but I can't seem to figure out how to set that modifier field when doing so. Any ideas?
More about my project: People have many companies through company_relationships. With that extra field in there I am using it to group all of the specific relationships together. So I can group a person's Doctors, Contractors, etc.
My models:
Company.rb (abridged)
class Company < ActiveRecord::Base
include ApplicationHelper
has_many :company_relationships
has_many :people, :through => :company_relationships
Person.rb (abridged)
class Person < ActiveRecord::Base
include ApplicationHelper
has_many :company_relationships
has_many :companies, :through => :company_relationships
accepts_nested_attributes_for :company_relationships
company_relationship.rb
class CompanyRelationship < ActiveRecord::Base
attr_accessible :company_id, :person_id, :corp_credits_id
belongs_to :company
belongs_to :person
belongs_to :corp_credits
end
My form partial, using formtastic.
<% semantic_form_for #person do |f| %>
<%= f.error_messages %>
<% f.inputs do %>
...
<%= f.input :companies, :as => :check_boxes, :label => "Favorite Coffee Shops", :label_method => :name, :collection => Company.find(:all, :conditions => {:coffee_shop => 't'}, :order => "name ASC"), :required => false %>
So what I would like to do is something like :corp_credit_id => '1' in that input to assign that attribute for Coffee Shop. But formtastic doesn't appear to allow this assignment to happen.
Any ideas on how to do this?
Are you looking for something like
<% semantic_form_for #person do |form| %>
<% form.semantic_fields_for :company_relationships do |cr_f| %>
<%= cr_f.input :corp_credit_id %>
<% end %>
It is in the documentation