Application helper to pass current client :id to collection form field? - ruby-on-rails

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))

Related

how to pass params via link_to in activeadmin

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

Rails 4 nested form issue

Hi I'm using the nested form plugin and trying to make it work for rails 4 instead of rails 3. Basically my model looks like this:
has_many :item, :dependent => :destroy
accepts_nested_attributes_for :item, :reject_if => lambda { |a| a[:item].blank? }, :allow_destroy => true
and my view looks like this:
<%= nested_form_for(#store) do |f| %>
<%= f.fields_for :item do |item_form| %>
<%= item_form.text_field :name %>
<%= item_form.link_to_remove "Remove this item" %>
<% end %>
<% end %>
this works (in terms of presentation - you can add and delete fields like you should be able to) but doesn't save the item names.
I tried this in my controller (these are the protected attributes/params):
def store_params
params.require(:store).permit(:name, :owner, :description,:url, :user, item_attributes)
end
but it still comes up with:
Unpermitted parameters: item_attributes
Thanks for all help!
You're going to have to permit the fields of item (like name) to be allowed as well.
So try this:
def store_params
params.require(:store).permit(:name, :owner, :description,:url, :user, item_attributes: [:name])
end
sometimes you have to specify the :id like this:
def store_params
params.require(:store).permit(:name, :owner, :description,:url, :user, item_attributes: [:id, :name])
end
in a similar case I had last week, not specifying the :id made Rails 4 create an new entity instead of updating the existing one.

Rails Activeadmin - custom association select box

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.

RAILS: Using simple_form with parent/child accounts

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 .

How to show select menu for two models?

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.

Resources