Disable or hide a button when an object is empty Rails - ruby-on-rails

How can I disable a button once the user was able to send a request. I used the logic of checking if an object in empty or has value in the controller and if the object is empty the user can submit a request otherwise the button will be hidden or disabled.
Object in the Controller
#c_req = Notification.where("message LIKE ?", "%{friend}%")
View
<%= form_for :friend, url: friends_path, method: :post do |f| %>
<%= f.hidden_field :message, :value => "Accepted your friend request" %>
<%= f.hidden_field :date, :value => #date %>
<%= f.submit 'Submit', :disabled => #c_req.present? %>
<% end %>
I used :disabled => #c_req.present? to check if it has a value.

If disabled attribute is added in any input field then it will be disabled either it is disabled = false.
You want to enable button if object is empty.
<%= form_for :friend, url: friends_path, method: :post do |f| %>
<%= f.hidden_field :message, value: "Accepted your friend request" %>
<%= f.hidden_field :date, value: #date %>
<%= f.submit 'Submit',class: "submit_btn" %>
<% end %>
<script type="text/javascript">
$(document).ready(function(){
<% unless #c_req.empty? %>
$(".submit_btn").attr('disabled', true)
<% end %>
})
</script>
Above js code runs when page is loading, if the #c_req is empty then your submit button is enable else disable

Try to replace <%= f.submit 'Submit', :disabled => #c_req.present? %> with <%= f.submit 'Submit', :disabled => !#c_req.present? %>

You have twisted the logic. You want to disable the button if it does NOT exist.
I'd try changing to .any? or .empty?
And be nice you your future self and name the variable better.

#present? is used to check if the item is there. Disabling has to happen if the item is not there. For that we have #empty?
Please don't use things like c_req, be a little descriptive in variable names. Also follow a code style, some thing like this...
Update to your code...
<%= form_for :friend, url: friends_path, method: :post do |f| %>
<%= f.hidden_field :message, value: "Accepted your friend request" %>
<%= f.hidden_field :date, value: #date %>
<%= f.submit 'Submit', disabled: #c_req.empty? %>
<% end %>
If you are hiding it... Use a similar query to decide not to show the button or hide the button from css.
Ex:
<%= f.submit 'Submit', class: ('hidden' if #c_req.empty?) %>
I guess you get the idea!

Related

create record based on collection_select dropdown

I am trying to create playlist records based on the dropdown selection. I'm going to put this in a bootstrap divided dropdown once functional. For now I have a dropdown and a link. The link does the thing, creates a new playlist obj with the values from the video it was clicked on, everything is a PORO until added as a playlist obj, where the video is persisted to the DB as a video obj. What I am wanting to do is, if you want to add a video to as a playlist obj, that you see a list of associated playlist names. When you click on the name, it does the same thing as the link_to but takes the name parameter along with it to create the new record. Thanks for any help.
PS. if you have tips on implementing the bootstrap that'd be wicked helpful too :D
<%= form_tag :playlists, {action: 'create'} do %>
<%= collection_select :playlist, :name, current_user.playlists, :id, :name, class: 'dropdown-item' %>
<%= link_to 'Create Playlist', new_playlist_path(etag: video.etag, video_id: video.video_id, user_id: current_user.id,
img_high: video.img_high, img_default: video.img_default,
title: video.title, description: video.description), class: 'dropdown-item' %>
<% end %>
Basically, what I'm trying to do is...be able to do what the link_to is doing but also take along the playlist name
Bind to the onchange event of the the select and trigger it's form to submit.
<%= collection_select :playlist, :name, current_user.playlists, :id, :name, class: 'dropdown-item', onchange: "this.form.submit()" %>
Here's an example:
<form action="https://google.com/">
<select name="q" onchange="this.form.submit()">
<option value="abc">Option 1</option>
<option value="123">Option 2</option>
</select>
</form>
Was finally able to get the passing the params, I still have to use a button as couldn't get the onchange: to work yet.
<%= form_tag :playlists, {action: 'create'} do %>
<%= hidden_field_tag :etag, video.etag %>
<%= hidden_field_tag :video_id, video.video_id %>
<%= hidden_field_tag :img_high, video.img_high%>
<%= hidden_field_tag :img_default, video.img_default %>
<%= hidden_field_tag :title, video.title %>
<%= hidden_field_tag :published_at, video.published_at %>
<%= hidden_field_tag :description, video.description %>
<%= hidden_field_tag :user_id, current_user.id %>
<%= collection_select :playlist, :name, current_user.playlists, :name, :name, {class: 'dropdown-item', onsubmit: 'this.form.submit()'} %>
<%= submit_tag 'Add', method: 'post' %>
Wasn't aware I could use the hidden_field_tag there like that :P

Checkbox initial state when sharing form between new/edit views

I am trying to reuse code in a form between an edit and a new view. In the New view, I'd like the checkbox to be checked by default. In the edit view I'd like it to reflect the current state of the object. What's the best way to share this info? The code below is what I want for "new" but for edit I don't want it hard-coded to "checked: true".
<div class="row">
<%= form_for #producer_type do |f| %>
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
<%= f.label :enabled %>
<%= f.check_box :enabled, {checked: true} %>
<% end %>
</div>
You will need to pass an object from the controller to the form.
def new
#producer_type = ProducerType.new(enabled: true)
end
def edit
#producer_type = ProduceType.find(params[:id])
end
And use that in the form, Rails automatically checks it if enabled is true
<%= form_for #producer_type do |f| %>
<%= f.check_box :enabled %>
Replace true with current_page?(action: 'edit')
See ActionView::Helpers::UrlHelper#current_page? for more on the current_page methods.
And I don't think you need the curly braces because the hash is at the end of a parameter list.

How do I prevent certain form fields from showing up in the query string?

I have a date range selector for a report page which uses jQueryUI's datepicker. I have two fields for the start date and two fields for the end date:
<%= form_tag stats_path, :method => 'get', :id => "date-range" do %>
<%= hidden_field_tag :start_date %>
<%= text_field_tag :start_date_display %>
<%= hidden_field_tag :end_date %>
<%= text_field_tag :end_date_display %>
<%= submit_tag "Run it!" %>
<% end %>
Currently, as expected, all 4 fields are added to the query string as parameters, but I only want the hidden fields to be sent to the server. The text fields are just to display the dates in a user friendly format (e.g., May 5th) when the user selects a date.
Is there a way to exclude them?
I can't use :disabled => true, which would exclude them, because it makes the control unusable.
(I like having the date ranges in the query string so you can easily link to a report for a given range.)
Here's how I solved it using CoffeeScript:
$('#date-range').submit ->
$(this).find('input[id*="display"]').each ->
$(this).remove()
But an even better solution posed by #StuR is to set an empty value for the name attribute:
<%= form_tag stats_path, :method => 'get', :id => "date-range" do %>
<%= hidden_field_tag :start_date %>
<%= text_field_tag :start_date_display, '', :name => nil %>
<%= hidden_field_tag :end_date %>
<%= text_field_tag :end_date_display, '', :name => nil %>
<%= submit_tag "Run it!" %>
<% end %>

Rails 3.1 - Displaying form values on Edit?

I feel like this should be really really simple, but I'm completely stuck!
In a form I might have a field like:
<%= f.text_field :name, :class => "text" %>
On edit, this pulls back in the value submitted on create, that's fine. But I want to prevent certain fields from being edited. I know I can disable the field or hide it, but I'd like to be able to pull in the values to display and use in other ways. How do I access them?
In this case I've tried things like:
<%= f.track.name %>
<%= track.name %>
<%= #track.name %>
But none of the above work!
Any ideas folks?
EDIT: (I'm using nested forms for this)
<%= form_for(#release, :html => { :multipart => true }) do |f| %>
<h3>Upload Tracks for <%= #release.title %></h3>
<%= f.fields_for :tracks do |builder| %>
<%= render 'upload_track_fields', :f => builder %>
<% end %>
<%= f.submit "Upload Tracks", :class => "submit" %>
<% end %>
And the upload_track_fields that are rendered:
<%= f.text_field :position, :class => "text" %>
<%= f.text_field :name, :class => "text" %>
<%= f.text_field :isrc, :class => "text" %>
<%= f.text_field :version, :class => "text" %>
<%= f.file_field :track, :class => "text" %>
<%= f.hidden_field :primary_genre_id %>
<%= f.hidden_field :secondary_genre_id %>
<%= f.hidden_field :alt_primary_genre %>
<%= f.hidden_field :alt_secondary_genre %>
<%= f.hidden_field :asset_tier %>
<%= f.hidden_field :preview_start %>
<%= f.hidden_field :parental_advisory %>
<%= f.hidden_field :available_separately %>
<%= f.hidden_field :_destroy %>
I've hidden most of the fields to prevent editing, but still need to see some of the fields so they're left as text fields. I tried to disable them, but that stops any changes (specifically the file upload) working.
In short, i'd prefer to display most of the above as text rather than form fields.
In the main form:
<% index = 0 %>
<% f.fields_for :tracks do |builder| %>
<%= #release.tracks[index].name %>
<%= render 'upload_track_fields', :f => builder %>
<% index += 1 %>
<% end %>
In the nested form:
<%= f.text_field :position, :class => "text" %>
# Notice that there's no "name" attribute
<%= f.text_field :isrc, :class => "text" %>
<%= f.text_field :version, :class => "text" %>
<%= f.file_field :track, :class => "text" %>
What I did in the first snippet is dirty, but I never used fields_for so I don't know how to get the actual index. I looked in Google, but I didn't find a solution so far.
I can't try it right now, I'll do it when I'll be home.
I suggest using this while finding a way to get the index.
Good luck!
As those who have commented said, I'd assume the <%= #track.name %> should work, if you have #track = #release.track (for instance) in your edit method in the controller.
Instead of keeping track of the index you can access the associated objects through builder, it's actually a track
<% f.fields_for :tracks do |builder| %>
<%= builder.object.name %>
<%= render 'upload_track_fields', :f => builder %>
<% end %>

Submitting a Form from index page in Rails

I'm Rails newbie.
How to make a form which allows user to choose a language(en,fr etc) through Radio buttons in Home#Index View to Submit to Home#Language action ?
Thanks in advance
<%= form_tag language_path, :method => :post do %>
<%= label_tag :language_english, 'English' %>
<%= radio_button_tag :language, 'english' %>
<%= label_tag :language_french, 'French' %>
<%= radio_button_tag :language, 'french' %>
<%= submit_tag %>
<% end %>
Where language_path is the path defined in your routes.rb, such as
match "/home/language" => "home#language", :as => 'language'

Resources