Get from the link_to an array of values Ruby on Rails - ruby-on-rails

<i class="dropdown icon"></i>
<div class="menu">
<% #dogs.each do |dog| %>
<%= link_to dog[:name], params.merge(dog_id: dog[:id], page: nil), class: 'item' %>
<% end %>
</div>
</div>
I'm working on a project, I need to add to the params by clicking the id array with one key. Tried like this, but leads to unexpected results, I ask for help.
<i class="dropdown icon"></i>
<div class="menu">
<% #dogs.each do |dog| %>
<%= link_to dog[:name], params.merge(dog_id: params.push(dog[:id]), page: nil), class: 'item' %>
<% end %>
</div>
</div>
I understand what should look like this
<%= link_to dog[:name], params.merge(dog_id: [0, 1, 4, 6], page: nil), class: 'item' %>
But I can not realize. Thanks!

Related

Haiving a problem to Paginate an iteration with Kaminari

I'm trying to paginate a result of an iteration using Kaminari. The pagination is showing well but its not cutting it into another page. All is showing on the same page even when I click on page 2, it still the same result as page 1
I'm using rails 6 and ruby 2.5
in my controller i have this
def dashboard
user_gigs = current_user.gigs
#user_gigs_pager = Kaminari.paginate_array(user_gigs).page(params[:page]).per(4)
end
then in my view I call the paginate
<div class="card">
<div class="card-header-title is-centered">
<%= paginate #user_gigs_pager %>
</div>
</div>
this my full view code
<% current_user.gigs.each do |gig| %>
<%# <% user_gigs = current_user.gigs %>
<div class="column is-one-third">
<div class="card">
<div class="card-image">
<%= link_to edit_gig_path(gig) do %>
<figure class="image is-4by3">
<!-- Gig Cover Image Here -->
<%= image_tag gig_cover(gig) %>
</figure>
<% end %>
</div>
<div class="card-content p-t-5 p-b-5">
<p class="subtitle is-6 m-b-5"><%= link_to gig.title.truncate(20), gig_path(gig) %></p>
<span class="star-review"><i class="fa fa-star"></i>
<%= gig.average_rating %> <span class="star-review">(<%= gig.reviews.count %>)</span>
</span>
</div>
<footer class="card-footer">
<p class="deletegig">
<%= link_to destroy_gig_path(gig), method: :delete, data: { confirm: "are you sure"} do %>
<span class="icon has-text-danger">
<i class="far fa-trash-alt"></i>
</span>
<% end %>
</p>
<% basic_price = gig.pricings.find{ |p| p.pricing_type == 'basic' }%>
<a class="has-text-danger is-block card-footer-item has-text-right">
<% if !basic_price.nil? %>
<span class="small-title">Points</span> <i class="fab fa-bitcoin"></i>
<strong><%= basic_price.price %></strong>
<% else %>
<strong>
<span class="small-title has-text-dark">Pas de point</span>
</strong>
<% end %>
</a>
</footer>
</div>
</div>
<% end %>
</div>
</div>
</div>
<div class="card">
<div class="card-header-title is-centered">
<%= paginate #user_gigs_pager %>
</div>
</div>
Kaminari.paginate_array(user_gigs).page(params[:page]).per(4)
expects page number as argument of page and number of records per page as argument of per.
Your params must contain page number (i.e. 1 or 2 or 3 ...) in :page key.
The solution was to apply the pagination on the ActiveRecord association instead of the array.
So in my controller it should be
#user_gigs_pager = current_user.gigs.page(params[:page]).per(4)
then in the view
<% #user_gigs_pager.each do |gig| %>
....
<%= paginate #user_gigs_pager %>

Using ransack for Rails application material dropdown select in view

Just need to know how you add ransack code in rails view as I m taking array list as category items and passing into the bootstrap material
the code for rails
on top of starting line search path code
<%= search_form_for #search, url: events_path do |f| %>
dropdown code dropdown without ransack which gives output with correct dropdown list
<div class="col">
<div class="dropdown" id="dropdown">
<p class="selected" id="selected"><span>Filter By Category1</span> <i class="material-icons">keyboard_arrow_down</i></p>
<ul class="dropdown-list" id="dropdown-list">
<a href="/events">
<li>All Events </li>
<% #category_list.each do |cat| %>
<li class="selected" id="list"> <%= cat.name %></li>
<% end %>
</ul>
</div>
</div>
I m trying to add ransack this does not display my list
<div class="col">
<div class="dropdown" id="dropdown">
<%= f.select :category_name_eq, class: "selected" %>
<ul class="dropdown-list" id="dropdown-list">
<a href="/events">
<li>All Events </li>
<% #category_list.each do |cat| %>
<li class="selected" id="list"> <%= cat.name %></li>
<% end %>
</ul>
</div>
</div>
you can do with list like this below, in case you allow blank option you should add include_blank: true as sample below
<div class="row form-group">
<%= f.label "Filter By Category1 <i class="mi md-18">arrow_downward</i>".html_safe, :class => 'col-form-label col-sm-3' %>
<div class="col-sm-9">
<%= f.select :category_name_eq,
#category_list.all.map { |cat| [cat.name, cat.name] },
{ include_blank: true }, { class: 'form-control' } %>
</div>
</div>

How to put content under the first record rails 4

I am on Rails 4. I have two resources: subarticles and articles. Subarticles are nested in articles like so:
resources :articles do
resources :subarticles do
member do
put "like" => "subarticles#upvote"
put "dislike" => "subarticles#downvote"
end
end
end
On the articles show page, I display all of the subarticles. However, I would like to place some of the article attributes ( specifically pictures and videos) directly under the first subarticle.
I am having trouble finding any documentation on doing so.
Also note that I have subarticles ordered by number of votes. So if one gets voted higher than the other, it will go to the top. But I want to keep the same pictures and videos right under the first subarticle.
Here is a simplified version of my Article show page:
<div class="row">
<div class="col-md-9">
<div class="well" style="background:white; height:230px">
<%= #article.title.titleize %><br>
<%= social_share_button_tag %>
</div>
<% #subarticles.each do |subarticle| %>
<div class="well" style="background:white">
<%= subarticle.rating %><br>
<%= subarticle.whyrating %><br>
<div class="row" style="margin-top:40px;">
<div class="col-md-7 col-md-offset-1" style="margin-top:30px">
<%= link_to like_article_subarticle_path(#article, subarticle), class: "like", method: :put do %>
<button type="button" class="btn btn-success btn-lg" aria-label="Left Align" style="margin-right:5px">
<span class="glyphicon glyphicon-thumbs-up" aria-hidden="true"></span> Helpful
</button><span class="badge" style="margin-right:10px"><%= subarticle.get_upvotes.size %></span>
<% end %>
<%= link_to dislike_article_subarticle_path(#article, subarticle), class: "like", method: :put do %>
<button type="button" class="btn btn-danger btn-lg" aria-label="Left Align" style="margin-right:5px">
<span class="glyphicon glyphicon-thumbs-down" aria-hidden="true"></span> Unhelpful
</button><span class="badge"><%= subarticle.get_downvotes.size %></span>
<% end %>
</div>
</div>
</div>
<% end %>
<div class="well" style="background:white; text-align:center">
<h4>Pictures & Videos</h4><br>
<%= image_tag #article.image1(:medium) %><br>
<div class="embed-responsive embed-responsive-4by3">
<iframe class="embed-responsive-item" src="<%= #article.video1 %>"></iframe>
</div>
</div>
</div>
</div>
Because of the #subarticles.each do it will push pictures and videos all the way to the bottom. What is the best way to show just the first subarticle, then pictures & videos, then the rest of the subarticles?
Let me know if I need to show any other code.
<% #subarticles.each_with_index do |subarticle, i| %>
<div class="well" style="background:white">
....... // subarticle contents
</div>
<% if i==0 %>
// put articles content here
<% end %>
<% end %>
In that way after the first subarticle the if condition checks the loop index, as the loop index is currently 0, then it will first show the subarticle first content, then for index 0 it shows the article contents.
You can always use a flag for that:
<% first_one = false %>
<% #subarticles.each do |subarticle| %>
<div class="well" style="background:white">
...
</div>
<% unless first_one %>
// article contents here....
<% first_one = true %>
<% end %>
<% end %>

undefined method `stringify_keys' for "/accounts/15":String

I am trying to display a list of accounts, i am getting the above error in my link_to method. Below is my link_to method.
<div class="list-group">
<% #accounts.each do |account| %>
<%= link_to account.name, account_path( account.accountid ),
class: "list-group-item active" do %>
<span class="glyphicon glyphicon-chevron-right"></span>
<span class="badge">44</span>
<% end %>
<% end %>
</div>
If you're using the block form of link_to you can't have text content (the block is your text content). If you look at rails helpers. The syntax for block form looks like:
link_to(url, html_options = {}) do
# name
end
So you need to do something like:
<div class="list-group">
<% #accounts.each do |account| %>
<%= link_to account_path( account.accountid ), class: "list-group-item active" do %>
<%= account.name %>
<span class="glyphicon glyphicon-chevron-right"></span>
<span class="badge">44</span>
<% end %>
<% end %>
</div>

Update SVG using gagejs and rails

I'm experimenting with Raphael, SVG, and rails. I want the gage to dynamically update as a progress indicator of sorts as each task is crossed off via the completed button.
Something like tasks completed/total task created.
Attached is the view in the browser and below is the show page.
<h1 class="page-header"> Getting it up</h1>
<div class="well span9 offset2">
<h2 class="page-header"><%= #list.name %></h2>
<h2 class="lead"><%= #list.description %></h2>
<div class="pull-right" id="smallbuddy" style="width:340px; height:200px;"></div>
<h3>Still Not Finished</h3>
<ul>
<% #list.tasks.incomplete.each do |task| %>
<li style="list-style:none"><%= task.description %> <%= button_to "Completed", complete_task_path(#list.id,task.id), :class =>"btn-mini" %></li>
<% end %>
</ul>
<hr>
<h3>Finished</h3>
<ul id="completed">
<% #list.tasks.completed.each do |task| %>
<li style="list-style:none; opacity:.5; text-decoration:line-through;"><em><%= task.description %></em></li>
<% end %>
</ul>
<hr>
<%= form_for [#list, #list.tasks.new] do |form| %>
<p><%= form.text_field :description %><%= form.submit %></p>
<% end %>
</div>
<span class="btn affix"><%= link_to "Back to all Installs", lists_url %></span>
<div>
</div>
<script>
var g = new JustGage({
id: "smallbuddy",
value: 27,
min: 0,
max: 100,
title: "Until Complete"
});
</script>
Do I need to manipulate the Raphael Lib? or the justgagejs lib?
Still working my way through it but some veteran advice to point me in the correct direction on how to achieve this would be helpful. Thanks in advance.
You need your rails controller to respond to the a JS request, read a little into this: Having trouble rendering Javascript in Rails question.
That way you can update your gauge in the JS returned by your controller.

Resources