Pass variable to Controller when clear button is clicked rails - ruby-on-rails

<%= form_tag({controller: "cc_banker_assignments", action: "index"}, method: "get", class: "form-horizontal") do %>
<div class="col-md-4">
<span class="badge badge-error count-error"></span>
<div class="form-group">
<label for="" class="col-sm-4 control-label">Date of Assignment</label>
<div class="col-sm-7">
<%= text_field_tag "assignment_date", params[:assignment_date], class: "form-control", id: "date-range" %>
</div>
</div>
<div class="form-group" style="margin-bottom: 1.5em;">
<label for="" class="col-sm-4 control-label">Assigned To</label>
<div class="col-sm-7">
<%= select_tag "assigned_to", options_from_collection_for_select(#banker, :id, :name, params[:assigned_to]), class: 'form-control single-select', prompt: "All Bankers" %>
</div>
</div>
<div class="col-sm-8 pull-right" style="margin-top: -0.3em">
<%= submit_tag "Search", :class => "btn btn-default lender-search-btn btn-s-layout" %>
<%= button_tag "Clear", :type => 'reset', :class => "btn btn-primary btn-c-layout clear_button", :onClick => "check_for_cancel" %>
</div>
</div>
</div>
<% end %>
Ok so i have this form and i want to be able to pass a variable to my index method on my controller when i click on the button.
This is because i want my controller to know if the clear button has been clicked and the only way for me to do tht is to pass a variable to my controller index method.
my controller method
def index
#banker = Banker.where("locked_at is null and approved = true").order('name asc')
ap params
if !params[:get_status].nil?
params[:banker_status] = BankerAssignmentStatus.where("assignment_status =? ",params[:get_status]).pluck(:id)
end
search_banker_assignment params
render template: 'cc_banker_assignments/index'
end
How do i do this using rails

Are you trying to clear the form inputs by requesting index page? You can use link_to instead of button_tag to do that, or just javascript. If you really want to pass a variable to the controller, you can bind your button to a jQuery.ajax() call.

Related

multiple forms in one view rails

am new to stack overflow..hoping support from all f u
i have employee form with personal details,contact details,salary details etc,arranged,but they are different controllers.i want to get all data in one form in employee view.employee contacts and other forms are not getting saved...help out...
`
<%= form_for(#employee, :html => {class: 'form-horizontal add_allignment employee_form', role: 'form'}) do |f| %>
<div class="form-group row">
<%= f.label :first_name, class: 'col-sm-3 form-control-label' %>
<div class="col-sm-9">
<%= f.text_field :first_name, class: 'form-control' %>
<span class="error-block"><%= validation_error(#employee, :first_name) %></span>
</div>
</div>
<div class="form-group row">
<div class="col-sm-offset-0 col-sm-10">
<%= f.button "Submit", type: 'button', class: 'btn btn-primary btn-lg', onclick: 'submit_my_form(this)' %>
</div>
</div>
<% end %>
</div>
<div role="tabpanel" class="tab-pane in" id="nav-tabs-0-2">
<%= form_for(#employee.contact_components, :html => {class: 'form-horizontal add_allignment employee_form', role: 'form'}) do |e| %>
<div class="form-group row">
<%= e.label "P O Box", class: 'col-sm-3 form-control-label' %>
<div class="col-sm-9">
<%= e.text_field :po_box, class: 'form-control' %>
<span class="error-block"><%= validation_error(#employee, :po_box) %></span>
</div>
</div>
<div class="form-group row">
<%= e.label :permanent_address, class: 'col-sm-3 form-control-label' %>
<div class="col-sm-9">
<%= e.text_area :permanent_address, class: 'form-control' %>
</div>
</div>
<div class="form-group row">
<div class="col-sm-offset-0 col-sm-10">
<%= e.button "Submit", type: 'button', class: 'btn btn-primary btn-lg', onclick: 'submit_my_form(this)' %>
</div>
</div>
<% end %>
</div>
</div>`
What you want is a nested form. It allows you to update different model attributes from a single form. That doc link will walk you through each change you need to make to the model, controller and views.

undefined method `appointments_path' using nested resource

I have a nested resource appointments in profiles in route.rb.
resources :profiles, only: [:show, :update, :edit] do
resources :appointments
end
In my AppointmentsController.rb I write new method like this
def new
#appointment = Appointment.new
end
I have form for this new method in appointments/new.html.erb
<section class="login-page border-top-blue padding-v-10">
<section class="login-details-div bg-light-grey">
<%= form_for(#appointment, :html => { :class => 'form-horizontal login-form-container login-form-div' }) do |f| %>
<h1>Create New Appointments</h1>
<div class="form-div-send-for-inlinement-icon-with-text-field">
<span class="form-wrapper ">
<i class="fa fa-user login-icon-white login-icon-envelop "></i>
</span>
<div class="label-input-div input-width">
<label for="" class="login-label-div"></label>
<div class="form-wrapper"><%= f.text_area :description, class:'login-icon-white', autofocus: true %></div>
</div>
</div>
<div class="form-div-send-for-inlinement-icon-with-text-field">
<span class="form-wrapper ">
<i class="fa fa-envelope login-icon-white login-icon-envelop "></i>
</span>
<div class="label-input-div input-width">
<label for="" class="login-label-div">topic</label>
<div class="form-wrapper"><%= f.text_field :topic, class:'login-icon-white' %></div>
</div>
</div>
<div class="form-div-send-for-inlinement-icon-with-text-field">
<span class="form-wrapper ">
<i class="fa fa-envelope login-icon-white login-icon-envelop "></i>
</span>
<div class="label-input-div input-width">
<label for="" class="login-label-div">Appointment Date</label>
<div class="form-wrapper"><%= f.date_field :appointment_date, class:'login-icon-white' %></div>
</div>
</div>
<div class="form-div-send-for-inlinement-icon-with-text-field">
<span class="form-wrapper ">
<i class="fa fa-envelope login-icon-white login-icon-envelop "></i>
</span>
<div class="label-input-div input-width">
<label for="" class="login-label-div">class hours</label>
<div class="form-wrapper"><%= f.number_field :class_hours, class:'login-icon-white' %></div>
</div>
</div>
<div class="form-group">
<label for="" class="col-sm-offset-2 col-sm-10 col-xs-offset-2 col-xs-10 col-md-offset-1 col-md-11 login-btn-div">
<%= f.submit "submit", :class => "login-btn" %>
</label>
<label for="" class="col-sm-offset-2 col-sm-10 col-xs-offset-2 col-xs-10 col-md-offset-1 col-md-11 login-btn-div">
<%= link_to "Cancel", root_path(), :class => "login-btn" %>
</label>
</div>
<% end %>
</section>
</section>
I have a link tag in profiles/show.html.erb for callling this new method of AppointmentsController.rb .
<%= link_to "book a class", new_profile_appointment_path, class: 'btn-green2' %>
I don't understand why whenever I click this link I got link in url
http://localhost:3000/profiles/3/appointments/new
which is I need.But there is an error
undefined method 'appointments_path' for #<#<Class:0x007f24ccc16bf8>:0x007f24ccc154b0>
I could not figure out my fault here.Please help.
As you are using nested resources for creating every appointment you need a profile under which your appointment will be nested. So you need to pass profile_id under which your appointment will be created as your path new_profile_appointment_path is also saying.
<%= form_for([#profile, #appointment], :html => { :class => 'form-horizontal login-form-container login-form-div' }) do |f| %>
You also need to define #profile in your method.
def new
#profile = Profile.find(params[:profile_id])
#appointment = Appointment.new
end
Url in your form should be smth like this:
<%= form_for [#profile, #appointment] do |f| %>
...
<% end %>
form_for tries to use existing paths. You don't have an appointments_path but you do have an profile_appointments_path
To use that path, just pass both objects to the form_for
<%= form_for([#profile, #appointment], :html => { :class => 'form-horizontal login-form-container login-form-div' }) do |f| %>
However, this does mean you need to create an instance variable #profile in your new method...
def new
#profile = Profile.find(params[:profile_id])
#appointment = Appointment.new
end

Bootstrap modal nested model form view messed up

Here is what happens
Here is my views code
Form
<%= simple_form_for([:supplier, #fuel_price],remote: true, :html => {:class => 'form-vertical' }) do |f| %>
<%= f.simple_fields_for :fuel_products do |fuel_products_form| %>
<div class="field">
<%= render partial: 'fuel_products_fields', locals: {f: fuel_products_form} %>
</div>
<% end %>
<%= link_to_add_fields "Add more", f, :fuel_products %>
<div class="modal-footer">
<%= f.button :submit, "Update", class: "btn btn-success"%>
</div>
<%end%>
Fuel Products Field Partial
<div class="col-md-6">
<%= f.input :fuel, label: "Fuel Type", :collection => fuel_types, class: "form-control select",:selected => "87 RFG" %>
</div>
<div class="col-md-3">
<%= f.input :price, class: "form-control", placeholder: "$1.25" %>
</div>
<%= f.hidden_field :_destroy %>
<ul class="list-unstyled">
<li><%= link_to '#', class: "btn btn-danger btn-xs remove_fields" do %>
Remove
<%end%></li>
</ul>
Javascript code
ready = ->
$('#FuelmodelBody').on 'click', '.remove_fields', (event) ->
$(this).prev('input[type=hidden]').val('1')
$(this).closest('.field').hide()
event.preventDefault()
$('#FuelmodelBody').on 'click', '.add_fields', (event) ->
console.log('It is really happening ....')
time = new Date().getTime()
regexp = new RegExp($(this).data('id'), 'g')
$(this).before($(this).data('fields').replace(regexp, time))
event.preventDefault()
$(document).ready(ready)
$(document).on('page:load', ready)
Views Modal code
<div class="modal inmodal" id="newFuelPrice" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content animated bounceInRight">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">
<span aria-hidden="true">×</span>
<span class="sr-only">Close</span>
</button>
<i class="fa fa-usd modal-icon" style="color:#1ab394"></i>
<h4 class="modal-title">Add New Fuel Price</h4>
<span class="font-bold">Your contacts will get text message of your latest price</span>
</br>
<span class="font-bold">As (your fuel price + the formula).</span>
</div>
<div class="modal-body" id="FuelmodelBody">
<!-- form -->
</div>
</div>
</div>
</div>
I also have a helper function for link_to_add_fields
def link_to_add_fields(name, f, association)
# creates a new instance of the 'has_many' object
new_object = f.object.send(association).klass.new
# new_object = f.object.association.klass.new
# f.object.association.klass # => Document
# new_object = f.object.documents.build Document(user_id: f.object.id)
# gets the object id
id = new_object.object_id
# creates a new form for the association
fields = f.fields_for(association, new_object, child_index: id) do |builder|
render(association.to_s + '_fields', f: builder)
end
link_to(name, '#', class: 'add_fields', data: { id: id, fields: fields.delete("\n") })
end
Moving around code since morning and still could not figure out how to fix those buttons by aligning side by add, the add more will also be button in blue, i was just playing around and converted it to link. I am Using simple Form
If you want to align your "Remove" buttons with their corresponding Fuel type and Price fields, then you need to put them all in a single Bootstrap row like so:
<div class="row">
<div class="col-md-6">
<%= f.input :fuel, label: "Fuel Type", :collection => fuel_types, class: "form-control select",:selected => "87 RFG" %>
</div>
<div class="col-md-3">
<%= f.input :price, class: "form-control", placeholder: "$1.25" %>
</div>
<div class="col-md-3">
<%= f.hidden_field :_destroy %>
<ul class="list-unstyled">
<li><%= link_to '#', class: "btn btn-danger btn-xs remove_fields" do %>
Remove
<%end%></li>
</ul>
</div>
</div>
Also, you probably don't need that extra ul tag. It does not carry any additional meaning, since you have just one button in there. It is not a list.
<div class="row">
<div class="col-md-6">
<%= f.input :fuel, label: "Fuel Type", :collection => fuel_types, class: "form-control select",:selected => "87 RFG" %>
</div>
<div class="col-md-3">
<%= f.input :price, class: "form-control", placeholder: "$1.25" %>
</div>
<div class="col-md-3">
<%= f.hidden_field :_destroy %>
<%= link_to 'Remove', '#', class: "btn btn-danger btn-xs remove_fields" %>
</div>
</div>

filepicker _field changes to link

this is my code
<div class="form-group">
<%= user.label :img,"Profile Picture",:class => "col-sm-3 control-label" %>
<div class="col-sm-8">
<%= user.filepicker_field :img, accept: 'image/jpeg,image/gif,image/png' , class: 'btn btn-default btn-lg btn-warning center-block', :button_text => "Select Image"%>
</div>
</div>
and it sometimes when i am trying to reload the page the button text changes from "Select Image" to link !
How can i fix that ??
It looks like you assigning the blob.url to the image tag attribute.

Rails - getting the value out from the view form into a controller

In my form the user has to select a category for his post.
<div class="field form-group">
<%= f.text_field :category, class: "form-control" %>
</div>
(Currently I am using text-input instead of a drop-down list)
Posts belong to categories
#post = #category.posts.build(post_params)
However I can't understand how to get the category value out of that field.
I have tried passing a number, to find_by id, and string to find_by name.
#category = Category.find(params[:category]) #returns no Categoy with nil id
#category = Category.find_by(name: params[:category]) #returns no method error
any help would be appreciated
Edit:
Form code
<%= form_for [#company, #post], :html => { :class => "form-posts"} do |f| %>
<div class="field form-group">
<div class="input-group input-group-lg">
<span class="input-group-addon">$</span>
<%= f.text_field :text, class: "form-control", placeholder: "text", required: true %>
</div>
</div>
<div class="field form-group">
<div class="input-group">
<span class="input-group-addon">
<i class="glyphicon glyphicon-calendar"></i>
</span>
<%= f.text_field :date,
class: "form-control",
value: #today,
data: {behaviour: "datepicker"},
required: true %>
</div>
</div>
<div class="field form-group">
<%= f.text_field :comment, class: "form-control", placeholder: "Comment (optional)" %>
</div>
<div class="field form-group">
<%= f.text_field :category, class: "form-control" %>
</div>
<div class="actions"><%= f.submit 'Add', class: "btn btn-lg btn-primary" %></div>
<% end %>
Edit 2
controller:
form:
<%= f.text_field :category, class: "form-control" %>
def create
#company = current_user.companies.find(params[:company_id])
#category = #company.categories(params[:category])
#post = #category.posts.build(post_params)
debugger:
{"utf8"=>"✓", "authenticity_token"=>"...", "transaction"=>{"text"=>"lalala", "date"=>"11.11.2013", "comment"=>"", "category"=>"5"}, "commit"=>"Add post", "action"=>"create", "controller"=>"posts", "company_id"=>"2"}
undefined method `transactions'
Take a look at the development log.
Probably it will be something like params[:post][:category]
Get your category like this:
params[:post][:category]

Resources