Ruby on Rails allow multiple clicks for link_to - ruby

I'm new to Ruby on Rails and have found myself stumped.
I am trying to create a link_to item in my view that will not be disabled after being clicked once. It's a page listing people's names and their attendance. The names are colored red if absent and green if present and I'd like the user to be able to toggle the attendance more than once for each person.
Here is the code for my view and controller (note I am using websockets here as well to update the coloring of the button after being clicked):
index.html.erb:
<div id="reader-attendance-<%= member.id %>">
<% if member.present == true %>
<%= link_to member.last_name.upcase, attendance_path(:id => member.id, :present => false), remote: true, class: "btn btn-outline-success btn-14" %>
<% else %>
<%= link_to member.last_name.upcase, attendance_path(:id => member.id, :present => true), remote: true, class: "btn btn-outline-danger btn-14" %>
<% end %>
</div>
controller.rb:
def attendance
member = Member.find(params[:id])
if(params[:present] == 'true')
member.present = true
else
member.present = false
end
member.save
vote_member = VoteMember.where(member_name: member.display_name)[0]
if vote_member.vote_type == 'Absent'
vote_member.vote_type = ''
vote_member.save
end
ActionCable.server.broadcast(
'attendance_channel',
{
member_id: member.id,
present: params[:present],
vote_member_id: vote_member.id,
total_abs: Member.where(present: false).count(),
member_name: member.last_name.upcase
}
)
end
Solutions I have tried:
I've read that you can add an option to config so that the disable option is turned off as follows:
Rails.application.configure do
config.action_view.automatically_disable_submit_tag = false
end
The it seems like this is not supported, however, for the link_to tag and is only supported for button_to
I added the content in #1 to config and then I've tried the button_to option as follows with no success. I have a feeling it's the way that I've passed the parameters in here:
<%=button_to member.last_name.upcase ,:action => :attendance, :id => member.id, :present => false, remote: true, class: "btn btn-outline-success btn-14" %>
I am working with the following versions:
Rails 6.1.4.1
Ruby 3.0.1
Thanks in advance! Please let me know if other info would be helpful.

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

Ruby on Rails Like Button with Image no text

I'm using the acts_as_votable gem to add a Like button to my posts. I followed the instructions I found in the following StackOverflow question:
Like button Ajax in Ruby on Rails
The code works really well but I'd rather use an an image than the text link. Basically I'm trying to use an existing design template that uses the following code based on like or dislike:
Like
<span class="heart-icon" style="background-position: 0px -290px;" id="<%= #pride.id %>"></span>
Dislike
<span class="heart-icon" style="background-position: 0px -256px;" data-id="<%= #pride.id %>"></span>
I tried using a do on the link_to and it works but also applies a text overlay that I believe is coming from the JS file. I've searched though a lot of the questions on here and I can't seem to find one that fits my situation. I'm hoping someone in the community will be able to help me out.
Here is how I currently have it setup:
My view looks like this:
<% if current_user.liked? #content %>
<%= link_to "Dislike", dislike_content_path(#content), class: 'vote', method: :put, remote: true, data: { toggle_text: 'Like', toggle_href: like_content_path(#content), id: #content.id } %>
<% else %>
<%= link_to "Like", like_content_path(#content), class: 'vote', method: :put, remote: true, data: { toggle_text: 'Dislike', toggle_href: dislike_content_path(#content), id: #content.id } %>
<% end %>
My CoffeeScript looks like this:
$(document).on 'ajax:success', 'a.vote', (status,data,xhr)->
# update counter
$(".votes-count[data-id=#{data.id}]").text data.count
# toggle links
$("a.vote[data-id=#{data.id}]").each ->
$a = $(this)
href = $a.attr 'href'
text = $a.text()
$a.text($a.data('toggle-text')).attr 'href', $a.data('toggle-href')
$a.data('toggle-text', text).data 'toggle-href', href
return
return
This is how I want my view configured but it keeps writing the Like/Dislike text over the image and I can't figure out where to change it.
<% if current_user.liked? #content %>
<%= link_to "Dislike", dislike_content_path(#content), class: 'vote', method: :put, remote: true, data: { toggle_text: 'Like', toggle_href: like_content_path(#content), id: #content.id } do %>
<span class="heart-icon" style="background-position: 0px -290px;" id="<%= #content.id %>"></span>
<% end %>
<% else %>
<%= link_to "Like", like_content_path(#content), class: 'vote', method: :put, remote: true, data: { toggle_text: 'Dislike', toggle_href: dislike_content_path(#content), id: #content.id } do %>
<span class="heart-icon" style="background-position: 0px -256px;" data-id="<%= #content.id %>"></span>
<% end %>
<% end %>
I found this awesome repository on GitHub that helped me out. Special thanks to the author.
https://github.com/shihabullal/soquestion6482354

only showing button to index action when there are objects listed in the index(in the database)

I have a button that links to the projects updates index action through all_project_updates_path i set in my routes. Here is my button code:
<%= button_tag type: "button", :class => "radius" do %>
<%= link_to 'Project Updates', all_project_updates_path(#project), :style => "color: white" %>
<% end %>
I want this button to only be visible when there are project updates in the database. Otherwise I want this button to dissapear. I tried:
<% if all_project_updates_path(#project) != nil %>
<%= button_tag type: "button", :class => "radius" do %>
<%= link_to 'Project Updates', all_project_updates_path(#project), :style => "color: white" %>
<% end %>
<% end %>
And also
<% if #updates != nil %>
<%= button_tag type: "button", :class => "radius" do %>
<%= link_to 'Project Updates', all_project_updates_path(#project), :style => "color: white" %>
<% end %>
<% end %>
but that doesn't seem to work. Looking for a simple explanation as I am a relative beginner with ruby.
This is the route:
get 'all_project_updates/:id' => 'project_updates#index', as: 'all_project_updates'
This is my projects controller(show action)
def show
#project = Project.find(params[:id])
#comments = Comment.all.where(:project_id => #project.id)
#updates = ProjectUpdate.all.where(:project_id => #project.id)
end
And this is my project updates controller index action:
def index
#projectUpdates = ProjectUpdate.where(:project_id => params[:id])
respond_to do |format|
format.html
end
end
You’re examining a path helper, all_project_updates_path, when you need to be querying a model object. The all_project_updates_path call is a helper to return a path for linking between web pages.
all_project_updates_path(#project) # => /all_project_updates/1
So you’re really asking:
'/all_project_updates/1'.nil? # => false
Because it’s just a string, it won’t be nil.
Instead, you should be examining the project_updates directly. I’m not sure how your models are related, but assuming that a Project has_many :project_updates, try this:
if #project.project_updates.any?
That will return true if a #project has any updates.
Beyond your immediate question, I would recommend considering whether nested resources are a better fit for this usage. You could declare your routes like this:
resources :projects do
resources :project_updates
end
Then you would get project_project_updates_path(#project) and no longer need your custom route that pretty much duplicates that functionality.
Try this:
<% if #updates.any? %>
<%= button_tag ... %>
<% end %>
#updates is an empty collection (ActiveRecord_Relation to be precise) of ProjectUpdate objects if no records were found, it's not nil.

prevent double click with disable_form, ruby on rails

I'm trying to prevent a form from being "double posted", when the user either clicks twice or hits submit twice.
I've seen a couple posts on this, but they haven't hit this issue per se. I can't seem to get the below to stop double-posts, and I'm getting the feeling it's related to the remote => true (using ajax to show the content on the page).
Below is my form:
<%= form_for([#posts, #comment], :remote => true) do |f| %>
<%= f.text_field :comment %>
<%= f.submit "Submit", class: "btn btn-large btn-primary", :style => 'display: none;', :disable_with => '' %>
<% end %>
Any recommendations would be great. Thank you!
Use the disable_with option
<%= submit_tag :submit, :id => 'submit_button', :value => "Create!", disable_with: "Creating..." %>
The other answer didn't work for me — I believe it was from the Rails 2 era. According to the docs, the disable_with attribute should be added within a data attribute, like so:
<%= submit_tag "Complete sale", data: { disable_with: "Please wait..." } %>

Rails 3 & Ajax date_select onchange => submit

I'd like my date to get saved anytime I change my date via ajax, is this possible? I tried
<%= form_for #dates,:remote => true do |f| %>
<%= f.date_select :booking_date,:onchange => "this.form.submit();" %>
<% end %>
but it does nothing, any good work arounds?
From the rails documentation:
date_select(object_name, method, options = {}, html_options = {})
Because onchange is an html option you need to provide an empty set of options to date_select, otherwise it assumes onchange is a date_select option.
You should change your call to date_select to look like this:
f.date_select :booking_date, {}, :onchange => "this.form.submit();"
Are you including this in your layout:
<%= javascript_include_tag :defaults %>
<%= csrf_meta_tag %>

Resources