Adding a drop down in rails and do particular action - ruby-on-rails

I am new to rails and this might me be a basic question. I checked on the internet but not able to find a simple example(might be my search is bad).
I need to create a form in my view and based the value selected and button clicking action i need to execute a particular action in my controller. I am able to create a drop down in my view using following lines.
= form_tag("/rtes", method: "get") do
= label_tag(:q, "Get Trip Type:")
= select_tag(:q, options_for_select({ "a" => "r4d_001", "b" => "r4d_002" })
<br>
= button_to( "Get Trip", :action => "rtes", :controller =>:q)
where rtes is my controller and i have mapped the value of the drop down values to the corresponding action names that needs to be executed when the button is clicked. This is my controller.
class RtesController < ApplicationController
##client=OptimusRtesServiceModel.new
def index
end
def r4d_001
result = ##client.r4t_001()
#driver_username = result.trip.loads[0].driver_details[0].driver_user_name
#driver_password = result.trip.loads[0].driver_details[0].driver_password
#trip_id = result.trip.trip_id
#carrier_username = result.carrier_details.carrier_user_name
#carrier_password = result.carrier_details.carrier_password
end
def r4d_002
result = ##client.r4t_002()
#driver_username = result.trip.loads[0].driver_details[0].driver_user_name
#driver_password = result.trip.loads[0].driver_details[0].driver_password
#trip_id = result.trip.trip_id
#carrier_username = result.carrier_details.carrier_user_name
#carrier_password = result.carrier_details.carrier_password
end
end
Now, if the first option in the drop down is selected and the button is clicked the "r4d_001" action in the controller needs to be executed. Your help is highly appreciated.

Rails is not like angular that you are trying to use :q variable to pass the value of selection to the button. You can create a drop-down menu instead of using select field to send them to different action for different option. Still if you want to use select field, then you need javascript to handle the front-end task here.

I have slightly modified my code to made it work. The idea is to get use the rails params and execute the corresponding action in my controller using case/when.
My view code:
= form_tag({:controller=>"r4d", :action=>"result"}, method: :get) do
= label_tag(:q, "Trip Type: ")
= select_tag(:q, options_for_select({"a" => "1", "b" => "2"})
= submit_tag("GetDetails")
My Contoller code:
class R4dController < ApplicationController
##client=ServiceModel.new
def r4d_result
result = case params[:q]
when "1"
##client.1
when "2"
##client.2
end
#value = result.value
end
end
By this way, i am able to pass the selected value of the drop down and execute the corresponding thing.

Related

Rails querying with an array

so in my controller i made a page which shows only the group_pages(the scaffold) in which the array #group_post_users contain the current user so:
def my_group
#group_pages = GroupPage.all
#group_pages.each do |group_page|
#group_page_users = group_page.page_users.split(',')
#group_page_users.push(group_page.user.email)
#mg_users = User.find_by_email(#group_page_users)
#gg_users = User.where(:email => #group_page_users)
group_page.super_uses = #gg_users
#g_pages = GroupPage.where(:super_uses => #selected_posts.map(&:id))
end
end
now i have an array of users for a group_page in #group_page_users and i only want to show the group pages which contain the current user inside the #group_page
basically i want something like this :
#g_pages = GroupPage.where(:super_uses => current_user.email)

Couldn't find Employee with id when im trying to add the one more time when enter the wrong data

I am facing the problem when I am trying to edit & add the new data. In this controller I am creating the new data of the relatives with employee_id, if I want to change the relative when I added the wrongly using this code I can delete but unable to add. When I am adding it is saying could't find the employee with id.
Can any one tell where I am wrong?
def employee_relations
if params[:relative]
#employee = Employee.find(employee_relative_params[:employee_id])
#employee_relatives = Relative.where(employee_id: #employee.id, relation_type_id: params[:relative][:relation_type_id])
#employee_relatives = Relative.create!(employee_relative_params.merge!("created_by" => current_user.id)) if ((#employee_relatives.empty? && params[:relative][:relation_type_id] != '3') || ( params[:relative][:relation_type_id] == '3'))
else
#employee = Employee.find(params[:employee_id])
#employee_relatives = Relative.where(employee_id: params[:employee_id].to_i)
end
#relation_types= Masters::RELATION_TYPE
respond_with(#employee_relatives)
end
for removing the data: this is for deletion it will work perfectly. if
I pass the params here like
employee_relative = Relative.find_by_id(params[:employee_relation_id][:employee_id])
it is
throwing the nil value
def remove_employee_relation
employee_relative = Relative.find_by_id(params[:employee_relation_id])
employee_relative.destroy if employee_relative
#relation_types= Masters::RELATION_TYPE
respond_with({:msg => "success"}, :location => nil)
end
You do not have a value for employee_id. From your post:
Parameters: {"utf8"=>"✓", "relative"=>{"relation_type_id"=>"3", "relation_name"=>"DADsdsd", "date_of_birth"=>"", "sex"=>"Male", "status"=>"Alive", "employee_id"=>""}, "commit"=>"Save"}
So your employee id is not being set wherever you are setting it(I am assuming in your form).

How to push a blank name and a name on to this array in Rails

I have an array of names. I want the first option to be blank, and the last option to be "Joe".
Here is the code:
def index
#case_managers = client.personnel_search_by_client(current_client.client_id, nil, groupMnemonic: 'reimbursement_whitelist')
#case_managers_drop_down = {}
#case_managers.each do |case_manager|
#case_managers_drop_down[case_manager.name] = case_manager.to_json
end
end
In my views I have:
= form_tag work_lists_path, :method=> 'put' do |f|
.fieldset.field-group.field-group-inline.pull-left
.field.field-text.field-required
%label= t('workflow.duplicate_claim_manager')
= select_tag('case[case_manager]', options_for_select(#case_managers_drop_down, #selected_case_manager))
This correctly gets all the names. I thought before the array I would do something like #case_managers_drop_down.push(" ") to get a blank option and then likewise for Joe. But this doesnt seem to be working. Any idea on how I can append to this array?
To get a blank option at first place in select_tag use include_blank boolean attribute like :
= select_tag('case[case_manager]', options_for_select(#case_managers_drop_down, #selected_case_manager), :include_blank => true)
OR use prompt like :
= select_tag('case[case_manager]', options_for_select(#case_managers_drop_down, #selected_case_manager), :prompt => "Please select")
For last option as joe do it like :
def index
#case_managers = client.personnel_search_by_client(current_client.client_id, nil, groupMnemonic: 'reimbursement_whitelist')
#case_managers_drop_down = {}
#case_managers.each do |case_manager|
#case_managers_drop_down[case_manager.name] = case_manager.to_json
end
#case_managers_drop_down["joe"] = ""
end
Hopefully it will work!

Database driven radio button ruby on rails

I am trying to update my conditions table with the radio button's value on and off
Here is the view
= form_tag('/admin/save',:action => 'update') do
= radio_button_tag("#{cols}_#{id}",1,checked = eval(check), options = {})
= radio_button_tag("#{cols}_#{id}",0,checked = eval(negcheck), options = {})
= submit_tag
Here is the controller
def updateCondition
params.each do |keys , value|
key ="#{keys}"
condition = key.split("_")[0]
hospitalid =key.split("_")[1]
if condition == "utf8" || condition == "authenticity" || condition == "commit"
next
end
Condition.find(hospitalid).update(:"#{condition}" => params["#{condition}_#{hospitalid}"])
end
render nothing: true
end
Params are :
{"utf8"=>"✓",
"authenticity_token"=>"",
"abc_10000"=>"1",
"commit"=>"Save changes",
"def_10000"=>"1",
}
Here is my question :
1) Why params is not showing all my radio button values ?
2) Any way to handle utf8, authentication_token, commit other than if statement ?
This may be why params not showing:
This looks strange to me: checked = eval(check) should it be checked: eval(check) or checked == eval(check)
Why are you setting options = {}? I think options should be passed in as a hash or not passed in at all.
Regarding #2,
if condition == "utf8" || condition == "authenticity" || condition == "commit"
next
end
Condition.find(hospitalid).update(:"#{condition}" => params["#{condition}_#{hospitalid}"])
You could extract that into a separate function:
def condition_valid?
condition == "utf8" || condition == "authenticity" || condition == "commit"
end
You're asking two questions here which is bad protocol for SO. I'll answer the second one.
Rather than try to filter out the parameters you don't want, which is very fragile, you could send through all the things you are trying to process, grouped into a single param. Eg, in your form, change the radio buttons to
= form_tag('/admin/save',:action => 'update') do
= radio_button_tag("conditions[#{id}][#{cols}]"}",1,checked = eval(check), options = {})
= radio_button_tag("conditions[#{id}][#{cols}]"}",0,checked = eval(negcheck), options = {})
= submit_tag
this will give you params like
{"utf8"=>"✓",
"authenticity_token"=>"",
:conditions => {1000 => {:abc => 1, :def => 1}},
"commit"=>"Save changes"
}
See how they are organised and separated? This means you don't have to muck about splitting them up again.
Now your controller code could be:
def updateCondition
params[:conditions].each do |id , attributes|
if condition = Condition.find_by_id(id)
condition.update_attributes(attributes)
end
end
render nothing: true
end
Note: you don't say what the value of "cols" is when you are building the form so i'm having to guess a bit. I think i just copied the way you are using it but shuffled it around a bit. There is almost certainly a cleaner way to build the form too.

Rails: Nested Forms and setting nested values

I have created a nested form. I have a model called contract and another called contract_details. In my contract controller I have the following code:
def new
#contract = Contract.new
#contract.customer_id = params[:customer_id]
7.times {
#contract.contract_details.build
#contract.contract_details.del_day = "Sun"
}
end
del_day is a column in my contract_details model but the following line of code is erring out:
#contract.contract_details.del_day = "Sun"
What am I doing wrong? How do I access a column in the nested model from the top controller. Meaning, how do I set the value of a column in the contract_details model when building each row in the contracts controller?
When I re-read, you would only be doing the same thing 7 times if it was an array, so suggest you try the following:
def new
#contract = Contract.new
#contract.customer_id = params[:customer_id]
7.times {
#new_contract_details = #contract.contract_details.build
#new_contract_details.del_day = "Sun"
}
end
The issue would be that at present when you call the following line, there is no indication which of the 'contract_details' objects you are trying to update.
#contract.contract_details.del_day = "Sun"

Resources