Ruby on Rails disabled field in form select not working - ruby

<%= f.select :minutes, options_for_select([['1 hour', 60], ['1 hour and a half', 90], ['2 hours', 120]][0..(#minutes_to_schedule/30-2)], 60), {}, {disabled: true} %>
I checked the Form Helper API but despite following suit my options were still not disabled, is there anything I'm overlooking? I looked at the other stacked posts and it doesn't seem like I'm doing anything wrong
<%= form_for(#session_date, html: {class: 'no_custom'}) do |f| %>
<%= f.hidden_field :student_id, value: #student.id %>
<%= hidden_field_tag 'from', schedule_student_path(id: #student.id) %>
<div id="appointment_display"></div>
<input id="appointment_at" type="datetime-local" name="appointment[at]" step="900" min= "some_time" max="max_time" required="required" placeholder="Use Chrome!" style="width: auto; display: none;"/>
<br/>
<div id="appointment_location_display"></div>
<br/>
<%= f.select :minutes, options_for_select(#appointment_time_options, 60) %>
<br/>
<%= f.collection_select(:location_id, Location.order('city, state, address'), :id, :name, {}, {class: 'dont_display'}) %>
<%= f.submit 'Create Appointment' %>
<br/>
<% end %>

disabled: true should be the 4th parameter
Check the definition below
options_from_collection_for_select(collection, value_method, text_method, selected = nil)
So, the correct way to do it ...
<%= f.select : minutes, options_for_select( ([['1 hour', 60], ['1 hour and a half', 90], ['2 hours', 120]]), {}, {}, { disabled: true } )%>

This looks correct.
When I copy/paste this:
<%= f.select : minutes, options_for_select([['1 hour', 60], ['1 hour and a half', 90], ['2 hours', 120]]), {}, { disabled: true }%>
Into a Rails app, the correct form element gets generated:
<select disabled="disabled" id="post_minutes" name="post[minutes]">
<option value="60">1 hour</option>
<option value="90">1 hour and a half</option>
<option value="120">2 hours</option>
</select>
and the element is disabled like it should be. Is there any JS manipulating this field? Would you mind providing your whole form so I can help you out?

Related

Rails sending multiple params through dropdown box

I have two dropdown boxes. One is for campaign size(small, medium, large) and the other one is for the theme selection.
I want to be able to send params dynamically through onchange event handler
current code looks like this
<div class="col-lg-12 col-padding-helper">
<%= form_tag product_builder_path, method: :get do %>
<div class="col-lg-5 col-padding-helper">
<%= label_tag 'Campaign Size' %></br>
<%= select_tag 'type', options_for_select(['Small (4 Products)', 'Medium (7 Products)', 'Large (10 Products)' ]), multiple: false, :include_blank => true, class: "form-control", data: { remote: true }, onchange: "this.form.submit();" %>
</div>
<% end %>
<%= form_tag(product_builder_path, method: :get, remote: true) do %>
<div class="col-lg-7 col-padding-helper">
<%= label_tag 'Theme' %></br>
<%= select_tag 'theme', options_for_select(['Default BWX Theme', 'Black Friday Theme']), multiple: false, :include_blank => true, class: "form-control", onchange: "this.form.submit();" %>
</div>
<% end %>
</div>
The workflow that I am thinking in my head is that once user selects the campaign size it will submit a form through ajax and have something like this as a url. which then I can display display right type of html template on the webpage.
/product_builder?type=small
and after when they select the theme is it possible to do something like this? and this will change the theme of the type that I have chosen above
/product_builder?type=small&theme=black
so basically it's adding params to current URL
I don't even know if this is possible, any help or guidance would be awesome
Thank you

How to save different values for the checkbox field labels, in ruby on rails?

I have a collection checkboxes in my form, like below.
<% ["cricket" ,"tennis", "not there in list"].each do |c| %>
<div class="col-md-4" >
<p><%= f.check_box :game, {:multiple => true, checked: #training.game&.include?(c), class: "reason-for-the-test"}, c, "" %> <%= c.capitalize.tr("_"," ") %></p>
</div>
<% end %>
Currently the label of the checkbox, and its value that is getting saved in the db, both are same. i want to save different values to the db for the labels. i tries like below. but its not working. can anyone help me with this.
<% [["cricket","cri"] ,["tennis","ten"], ["not there in list","na"]].each do |c| %>
<div class="col-md-4" >
<p><%= f.check_box :game, {:multiple => true, checked: #training.game&.include?(c), class: "reason-for-the-test"}, c, "" %> <%= c.capitalize.tr("_"," ") %></p>
</div>
<% end %>
# ⇓⇓⇓⇓⇓⇓⇓⇓⇓
<% [["cricket","cri"], ["tennis","ten"], ...].each do |name, val| %>
<div class="col-md-4" >
<p><%= f.check_box :game, {:multiple => true, checked: #training.game&.include?(val)}, name, "" %> <%= name %></p>
</div>
<% end %>

rails 5.1: nested form validation

I have a parent model called user and 2 nested models called award and certification. when I go to save a certification the model validation for award tells me that awards is blank, and when I got to save an award I get an error that says the validations for certification is blank. not sure what's going on.
here my user model
has_many :certifications, dependent: :destroy
has_many :awards, dependent: :destroy
# allows for nested attributes in the user views.
accepts_nested_attributes_for :work_histories, :qualifications, :certifications, :courses, :awards,
:languages, :patents, :publications, allow_destroy: true
here my award model
class Award < ApplicationRecord
belongs_to :user
validates :award_name, :award_granted_by, :award_date, :award_description, presence: true
end
and my certification model
class Certification < ApplicationRecord
belongs_to :user
validates :certification_name, :certification_authority, :certification_number, :certification_from,
:certification_to, presence: true
end
strong params in users_controller
def user_params
params.require(:user).permit( :first_name, :last_name, :email, :phone, :current_job_title, :password, :password_confirmation,
work_histories_attributes: [:user_id, :id, :_destroy, :job_title, :company, :description, :city, :state,:start_date, :end_date],
qualifications_attributes: [:user_id, :id, :_destroy, :education, :university, :university_major, :graduation_year, :currently_enrolled, :location],
awards_attributes: [:user_id, :id, :_destroy, :award_name, :award_granted_by, :award_date, :award_description],
certifications_attributes: [:user_id, :id, :_destroy, :certification_name, :certification_authority, :certification_number, :certification_from, :certification_to, :cert_never_expires],
courses_attributes: [:user_id, :id, :_destroy, :course_name, :course_number, :course_description],
languages_attributes: [:user_id, :id, :_destroy, :language, :language_proficiency],
patents_attributes: [:user_id, :id, :_destroy, :patent_title, :patent_office, :patent_number, :patent_status, :patent_date, :patent_description],
publications_attributes: [:user_id, :id, :_destroy, :publication_title, :publication_source, :publication_date, :publication_description] )
end
here's a part of the User form. I've got a button dropdown that has several options, when a user selects an option that particular div will be shown with a modal window.
<!-- one-to-many nested attributes -->
<%= form_for(#user) do |form| %>
<!-- a partial is rendered based on the user dropdown selection -->
<div id="award">
<%= render partial: "awards/new_award_modal", locals: { form: form } %>
</div>
<div id="cert">
<%= render partial: "certifications/new_certification_modal", locals: { form: form } %>
</div>
<%= form.submit 'Save', id: "submit-achievement", class: 'btn btn-primary form-control' %>
</div><!-- modal body -->
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
<% end %><!-- form_for -->
here is the partial for certifications
<div class="new_cert">
<%= form.fields_for :certifications, Certification.new do |cert_field| %>
<div class="field">
<%= cert_field.label :certification_name, 'Certification Title' %>
<%= cert_field.text_field :certification_name, class: "form-control" %>
</div>
<div class="field">
<%= cert_field.label :certification_authority %>
<%= cert_field.text_field :certification_authority, class: "form-control" %>
</div>
<div class="field">
<%= cert_field.label :certification_number %>
<%= cert_field.text_field :certification_number, class: "form-control" %>
</div>
<table>
<tr>
<td>
<div class="field">
<%= cert_field.label :certification_from, "From" %><br />
<%= cert_field.date_select :certification_from,{ :order => [:day, :month, :year],
:start_year => Date.current.year, :end_year => 1960,
:prompt => {day: 'Day', month: 'Month', year: 'Year'}, :discard_day => true }, class: 'form-control' %>
</div>
</td>
<td> </td>
<td>
<div class="field">
<%= cert_field.label :certification_to, "To" %><br />
<%= cert_field.date_select :certification_to, { :order => [:day, :month, :year],
:start_year => Date.current.year + (10), :end_year => 1960,
:prompt => {day: 'Day', month: 'Month', year: 'Year'}, :discard_day => true }, class: 'form-control' %>
</div>
</td>
</tr>
</table>
<div>
<%= cert_field.label :cert_never_expires, "This certification does not expire", class: "l" %>
<%= cert_field.check_box :cert_never_expires, class: 'cb' %>
</div>
<br />
<% end %>
</div><!-- ./new_cert -->
here's the awards partial
<div class="new_award">
<!-- nested attributes for award modal -->
<%= form.fields_for :awards, Award.new do |award_field| %>
<div class="field">
<%= award_field.label :award_name, 'Award or Honor Name' %>
<%= award_field.text_field :award_name, class: "form-control" %>
</div>
<div class="field">
<%= award_field.label :award_granted_by, "Granted by" %>
<%= award_field.text_field :award_granted_by, class: "form-control" %>
</div>
<div class="field form-inline">
<%= award_field.label :award_date, 'Date' %><br />
<%= award_field.date_select :award_date, { :order => [:day, :month, :year],
:start_year => Date.current.year, :end_year => 1960,
:prompt => {day: 'Day', month: 'Month', year: 'Year'},
:discard_day => true }, class: 'form-control ds' %>
</div>
<div class="field">
<%= award_field.label :award_description, 'Description' %>
<%= award_field.text_area :award_description, class: "form-control" %>
</div>
<% end %><!-- ./fields_for -->
</div>
so both awards and certifications can be saved to the database without any issue when there are no validations. however, when I just do a simple validation checking for presence of an attribute in the form that's when things get weird. The award model is saying that the certification form is empty, which it should be because I'm submitting the award form. And the opposite is also true, when I go to submit a certification the award validation is triggered saying the award form is blank.
It would be best to see the specific error you're getting, but I'd make sure that your form is setting the nested attributes appropriately, and that your user controller has nested attributes setup within the strong parameters.
There's documentation in the Rails guides: http://api.rubyonrails.org/classes/ActionController/StrongParameters.html

how to check the collection_radio_buttons selected for if condition? Rails

I have this form
<div class="form-group">
<%= f.label :status %>
<%= f.collection_radio_buttons(:status, options_for_status, :id, :description) do |b| %>
<div class="radio">
<%= b.label { b.radio_button + b.value} %>
</div>
<% end %>
</div>
if ??
#<% f.hidden_field :data_fim, :value => Date.today %>
When the user selects a specific radiobuton, he must execute hiden_field.
STATUS = {:Aguardando => 1, :'Em atendimento' => 2, :Finalizado => 3}
These are the options present for the user, I want to set set date_fim when the user select the radio
:Finalizado => 3
Would JS be my only option? Someone to help a noob in ruby?
This should work, assuming that jQuery is included in your assets.
<script type="text/javascript">
$(document).ready(function(){
$('.radio').click(function(){
$('#data_fim').val($(this).val());
});
});
</script>

Rails 3.1 Jquery Ajax CRUD Operations

I am working on Rails 3.1 Jquery Ajax demo app listed here https://github.com/samnang/ajax_rails31_demo. I am trying to add code to delete comments. I have modified comments_controller.rb and added a destroy action. I also added a link to the app/views/_comment.html.erb to include a Destroy link. I am able to get the comment to delete and the comment count is decremented by 1, but the comment partial does not update removing the entry. Only upon page refresh does the page display correctly. Here is my code
app/controllers/comments_controller.rb
class CommentsController < ApplicationController
respond_to :html, :js
def index
#comments = Comment.all
end
def create
#comment = Comment.new(params[:comment])
#comment.save
respond_with #comment, :location => comments_url
end
def destroy
#comment = Comment.find(params[:id])
#comment.destroy
respond_with #comment, :location => comments_url
end
end
app/views/comments/index.html.erb
<% title "Comments for Ajax in Rails 3.1" %>
<div id="comments_count"><%= comments_count %></div>
<div id="comments">
<%= render #comments %>
</div>
<h3>Add your comment:</h3>
<%= form_for Comment.new, :remote => true do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :content, "Comment" %><br />
<%= f.text_area :content, :rows => '12', :cols => 35 %>
</p>
<p><%= f.submit %></p>
<% end %>
app/views/comments/_comment.html.erb
<div class="comment">
<strong><%= comment.name %></strong>
<em><%= comment.created_at.strftime('%b %d, %Y at %I:%M %p') %></em>
<%= simple_format comment.content %>
<%= link_to "Destroy", comment,:remote => true, :confirm => "You Sure", :method => :delete %>
</div>
app/vews/comments/create.js.coffee
$('<%= escape_javascript(render(:partial => #comment))%>')
.appendTo('#comments')
.hide()
.fadeIn()
$('#new_comment')[0].reset()
$('#comments_count').html '<%= comments_count %>'
app/views/comments/destroy.js.coffee
$('#new_comment')[0].reset()
$('#comments_count').html '<%= comments_count %>'
The destroy jquery portion is where I believe I am messing up. How do I reload the partial to remove the deleted comment?
I have updated my example on Github to support destroy action. In your example you didn't remove dom element in your destroy.js.coffee. Check out full code on Github again or look at below snippets:
app/views/comments/_comment.html.erb
<div id=<%= dom_id(comment) %> class="comment">
<strong><%= comment.name %></strong>
<em>on <%= comment.created_at.strftime('%b %d, %Y at %I:%M %p') %></em>
<%= link_to "Remove", comment, :method => :delete, :remote => true %>
<%= simple_format comment.content %>
</div>
app/views/comments/destroy.js.coffee
$('#comments_count').html '<%= comments_count %>'
$('#<%= dom_id(#comment) %>')
.fadeOut ->
$(this).remove()

Resources