filepicker _field changes to link - ruby-on-rails

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.

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.

Rails Radio button is only allowing me to select only first option and not the other

What would be the cause of me only being able to select the first radio button, but not the second. Here is what my form looks like.
`
<%= form_for [#item, Calendar.new] do |f| %>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<%= f.text_field :start_date, readonly: true, value: Date.today, class: "form-control datepicker" %>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<%= f.text_field :end_date, readonly: true, value: Date.today, class: "form-control datepicker" %>
</div>
</div>
</div>
<div class="form-group">
<div class="btn-group" data-toggle="buttons-radio">
<%= f.collection_radio_buttons :status, Calendar.statuses, :first, :first, checked: Calendar.statuses.first do |b|
b.radio_button + b.label {b.text.split("_").join("").capitalize}
end %>
</div>
</div>
<div class="row new-pricing">
<div class="col-md-10">
<div class="form-group">
<div class="input-group">
<span class="input-group-addon">$</span>
<%= f.text_field :item_price, class: "form-control", value: #item.item_price, required: true %>
</div>
</div>
</div>
<div class="col-md-2">
<p style="margin-top: 10px"> Per Day </p>
</div>
</div>
<div class="no-pricing hide">
</div>
<div class="form-group">
<%= f.button "Save", type: :submit, class: "btn btn-success" %>
</div>
<% end %>`
Here is what the html looks like when I click on the "Not available" button. The checked always remains on the first value input button.
The html in the inspection window.
How can I go about fixing this. Sorry if this was an easy question.

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>

Returning the value from a select_tag as a parameter

I have a ruby form with a text box for start_date and one for end date. I need to add a dropdown for status. I added the select_tag and it populates correctly. My problem is how do I get the value that was selected?
Snippet from form:
<div class="modal-body">
<div class="control-group">
<label class="control-label">From</label>
<div class="controls">
<%= text_field_tag "purchase_requests_from_time", Date.today.beginning_of_month, :class => "text date_picker report_start_time_picker" %>
</div>
</div>
<div class="control-group">
<label class="control-label">To</label>
<div class="controls">
<%= text_field_tag "purchase_requests_to_time", Date.today.end_of_month, :class => "text date_picker report_start_time_picker" %>
</div>
</div>
<div class="control-group">
<label class="control-label">Status</label>
<div class="controls">
<%= select_tag :status, options_for_select(get_purchase_request_statuses) %>
</div>
</div>
<div class="control-group">
<label class="control-label">Status2</label>
<div class="controls">
<%= select_tag "status2", options_for_select(get_purchase_request_statuses) %>
</div>
</div>
</div>
Then when I try to access the params in the controller
from_time = params[:purchase_requests_from_time]
to_time = params[:purchase_requests_to_time]
status = params[:status]
status2 = params[:status2]
The time parameters show up fine, the status and status2 parameters are missing.
params = {ActiveSupport::HashWithIndifferentAccess} ActiveSupport::HashWithIndifferentAccess (4 elements)
'purchase_requests_from_time' = "Tue Mar 01, 2016"
'purchase_requests_to_time' = "Thu Mar 31, 2016"
'action' = "export"
'controller' = "purchase_requests"
Any idea what I am doing wrong?
I found my problem. I was missing the form_tag and was using a link_to rather than a submit_tag. This is the code that works
<div class="modal hide" id="download_purchase_requests">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h3>Download Purchase Requests</h3>
</div>
<%= form_tag({:action => :export, :controller => :purchase_requests}, :multipart => true) do %>
<div class="modal-body">
<div class="control-group">
<label class="control-label">From</label>
<div class="controls">
<%= text_field_tag "purchase_requests_from_time", Date.today.beginning_of_month, :class => "text date_picker report_start_time_picker" %>
</div>
</div>
<div class="control-group">
<label class="control-label">To</label>
<div class="controls">
<%= text_field_tag "purchase_requests_to_time", Date.today.end_of_month, :class => "text date_picker report_start_time_picker" %>
</div>
</div>
<div class="control-group">
<label class="control-label">Status</label>
<div class="controls">
<%= select_tag :purchase_request_status, options_for_select(get_purchase_request_statuses, :selected => params[:purchase_request_status]) %>
</div>
</div>
</div>
<div class="modal-footer">
Close
<%= submit_tag "Download", :class => "btn", :id => "download-btn", :onsubmit => "$('#download-btn').attr('disabled', 'disabled');" %>
</div>
<% end %>
</div>

Pass variable to Controller when clear button is clicked 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.

Resources