My is on Rails 7 with the Devise gem...
I'm implementing a favorite function with a heart svg and the acts_as_votabale gem and that is working as intended when the user is signed in. On my event index page I have:
<h2>Upcoming Events</h2>
<div class="event-list-wrapper">
<% #events.upcoming_events.each do |event| %>
<%= render "event", event: event %>
<% end %>
</div>
In my _event.html.erb I have (notice the user_signed_in Devise helper):
<% if user_signed_in? %>
<div class="heart-container">
<div id="<%= dom_id= event %>">
<%= render "events/favorite-link", event: event %>
</div>
</div>
<% end %>
<% if !user_signed_in? %>
<div class="heart-container">
<%= render "events/heart-signup", event: event %>
</div>
<% end %>
In the _heart-signup.html.erb I have:
<svg
xmlns="http://www.w3.org/2000/svg"
fill="#000000"
fill-opacity="0.5"
viewBox="0 0 24 24"
stroke-width="1.2"
stroke="#f2f2f2"
class="w-6 h-6">
<path stroke-linecap="round" stroke-linejoin="round" d="M21 8.25c0-2.485-2.099-4.5-4.688-4.5-1.935 0-3.597 1.126-4.312 2.733-.715-1.607-2.377-2.733-4.313-2.733C5.1 3.75 3 5.765 3 8.25c0 7.22 9 12 9 12s9-4.78 9-12z" />
</svg>
I was able able to confirm that the <%= render "events/heart-signup", event: event %> does render the image if I enter it directly on the event index page.
In the _favorite-link.html.erb I have:
<%= turbo_frame_tag "#{dom_id(event)}_likes" do %>
<%= link_to upvote_event_path(event), format: :turbo_stream, method: :patch, remote: true do %>
<% if current_user.voted_up_on? event %>
<%= link_to upvote_event_path(event), format: :turbo_stream, method: :patch do %>
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="#ff2929"
stroke-width="1.2"
stroke="#f2f2f2"
class="w-6 h-6">
<path d="M11.645 20.91l-.007-.003-.022-.012a15.247 15.247 0 01-.383-.218 25.18 25.18 0 01-4.244-3.17C4.688 15.36 2.25 12.174 2.25 8.25 2.25 5.322 4.714 3 7.688 3A5.5 5.5 0 0112 5.052 5.5 5.5 0 0116.313 3c2.973 0 5.437 2.322 5.437 5.25 0 3.925-2.438 7.111-4.739 9.256a25.175 25.175 0 01-4.244 3.17 15.247 15.247 0 01-.383.219l-.022.012-.007.004-.003.001a.752.752 0 01-.704 0l-.003-.001z" />
</svg>
<% end %>
<% else %>
<%= link_to upvote_event_path(event), format: :turbo_stream, method: :patch do %>
<svg
xmlns="http://www.w3.org/2000/svg"
fill="#000000"
fill-opacity="0.5"
viewBox="0 0 24 24"
data-user-logged-in="<%= user_signed_in? %>"
stroke-width="1.2"
stroke="#f2f2f2"
class="w-6 h-6">
<path stroke-linecap="round" stroke-linejoin="round" d="M21 8.25c0-2.485-2.099-4.5-4.688-4.5-1.935 0-3.597 1.126-4.312 2.733-.715-1.607-2.377-2.733-4.313-2.733C5.1 3.75 3 5.765 3 8.25c0 7.22 9 12 9 12s9-4.78 9-12z" />
</svg>
<% end %>
<% end %>
<% end %>
<% end %>
I tried using an else statement first within the same heart-container div before trying to try else with two completely different divs to then now trying two different if statements with user_signed_in? as I listed above, is there something I missing?
As I mentioned above, when I'm logged in, the favorite-link render works just fine, the issue I'm having is when a user is not signed in.
Any insights would be appreciated!
Related
I want to puts the result of this simple operation in my pack index view ( pack.quantity / pack.cigs.count).round(1)).
It works fine in local but does not work in heroku. In heroku I can see the result in some packs but not in all the packs.
I don't understand what is happening.
Thanks in advance.
<% #packs.each do |pack| %>
<li class="btn-green">
<%= pack.start_date.strftime("%b %e") %>
<% if pack.end_date %>
<%= pack.end_date.strftime("%b %e") %>
<%= "#{((pack.end_date - pack.start_date) / 60 / 60 / 24).round(1)} days" %>
<% else %>
<strong><%= 'not finished' %></strong>
<%= "#{((Time.zone.now - pack.start_date) / 60 / 60 / 24).round(1)} days" %>
<% end %>
<% if pack.cigs.count > 0 %>
<%= "#{pack.cigs.count} cigs" %>
<%= pack.quantity %>
<% if pack.quantity %>
<%= "#{(pack.quantity / pack.cigs.count).round(1)} g/cigs" %>
<% end %>
<% if pack.price %>
<%= "#{(pack.price / pack.cigs.count).round(1)} €/cigs" %>
<% end %>
<% end %>
Solved problem.
The problem was the operation with two integer with not integer result.
2 / 3 => 0
I've solved with to_f in the operation.
2.t_f / 3 => 0.6666666666
That's all.
Thanks.
I'm trying to split my records in half for display in my menu. The menu is two columns (col-md-4) but the methods I'm using with ODD number of records, puts the larger number on the wrong side (last_half) of my menu. What am I missing?
Menu
<div class="col-md-4">
<ul>
<li class="mega-menu-title">Products</li>
<% first_half(#menu_products).each do |product| %>
<li>
<%= link_to product_path(product) do %>
<span class="text-yellow"><%= product.name %></span> <%= product.subtitle %>
<% end %>
</li>
<% end %>
</ul>
</div>
<div class="col-md-4">
<ul>
<li class="mega-menu-title"> </li>
<% last_half(#menu_products).each do |product| %>
<li>
<%= link_to product_path(product) do %>
<span class="text-yellow"><%= product.name %></span> <%= product.subtitle %>
<% end %>
</li>
<% end %>
</ul>
</div>
<div class="col-md-4">
<!--- non-related code in last column in menu --->
</div>
Application Helper
def first_half(list)
list[0...(list.length / 2)]
end
def last_half(list)
list[(list.length / 2)...list.length]
end
You can use the following:
list.first((list.length/2).ceil) # will convert 1.5 to 2
And
list.last((list.length/2).floor) # will convert 1.5 to 1
The issue you had is that [7,8,9][3/2] returns 8, and the logic 3/2 (list.size / 2) was used in both first_half and last_half.
This is what I ended up doing to get it to work. I had to change the length to a float to_f, then I could get it to test in the console correctly.
def first_half(list)
list[0...(list.length.to_f / 2).ceil]
end
def last_half(list)
list[(list.length.to_f / 2).ceil...list.length]
end
Using .ceil on both methods then allowed the math to work.
Im having trouble rendering with jquery, I have a notification page which shows all notifications, then when i click on 1 for example it will show all the 1's and if i click 2 it will show all the 2's etc etc.
This is where all the notifications are rendered to start with. The locals work fine here.
<% #notifications.each do |x| %>
<div class="testing123">
<%= render 'notification', x: x %>
</div>
<% end %>
Then i click the link to change them
and here is the controller
def index
if params[:type]
#notifications = Notification.where(notification_type: params[:type])
else
#notifications = Notification.all
end
respond_to do |format|
format.html
format.js{
render 'notifications/jserb/select_notifications'
}
end
end
Which then arrives here:
$('.testing123').empty();
$("<%= escape_javascript(render #notifications, :locals => {:x => x}) %>").appendTo(".testing123");
But i keep getting this error
ActionView::Template::Error (undefined local variable or method x' for #<#<Class:0x007fae10882ed8>:0x007fae0f6234b8>):
EDIT-1: here is where i click the link:
<%= link_to "likes", notifications_path, remote: true %>
EDIT-2: here is the notification:
<div class="notification-body flerowspb" id="body<%=x.id%>">
<div class="flerowspb" style="width:80%">
<div class="notif-check flecolcen">
<%= check_box_tag "read_notif[]", value="#{x.id}", checked = false, options = {class: 'checked-id', id: "check-#{x.id}"} %>
</div>
<div class="notif-details ">
<div class="notif-time flerowspb">
<% if !x.viewed %>
<span class="glow" id="glow-<%=x.id%>"> New <%= x.notification_type.capitalize %></span>
<% else %>
<span><span class="text-grey"> New <%= x.notification_type.capitalize %></span></span>
<% end %>
<span class="text-grey font-small"> <%= time_ago_in_words(x.created_at) %> ago</span>
</div>
<div class="notif-body font-medium text-grey">
You have <%= x.status_count %> <%= x.title %> <br>
Click <span class="text-blue"><%= link_to "Here", entry_path(x.entry_id, notification_id: x.id) %> </span> to view it
</div>
</div>
</div>
<div class="notif-image">
<%= image_tag x.entry.image.small_thumb %>
</div>
</div>
EDIT-3: here is the full error that i get:
ActionView::Template::Error (undefined local variable or method `x' for #<#<Class:0x007fae0e6f32b8>:0x007fae11ac1838>):
1: <div class="notification-body flerowspb" id="body<%=x.id%>">
2: <div class="flerowspb" style="width:80%">
3: <div class="notif-check flecolcen">
4: <%= check_box_tag "read_notif[]", value="#{x.id}", checked = false, options = {class: 'checked-id', id: "check-#{x.id}"} %>
Any help will be appreciated
Issue with your code is after click on link it hits the notifications action and where you are getting #notifications as array of notifications, so after this in your js.erb you are rendering appending partial notification.html.erb with local variable x but here at your controller you are getting #notifications variable,
This should be like:
in notifications/_notification.html.erb
<div class="testing123">
<% notifications.each do |x| %>
<%#= render 'notification', x: x %>
#...
<% end %>
</div>
and in notification.js.erb
$('.testing123').empty();
$(".testing123").append("<%= escape_javascript(render 'notifications/notification', notifications: #notifications) %>");
I have an application with a list of majors and each one is tagged with categories using the acts-as-taggable-on gem. I have a page where you can explore majors by category. So, you see the categories and grouped under the category is a list of the majors.
My categories_controller.rb file:
def index
#creative = Major.tagged_with("creative arts")
#engineering = Major.tagged_with("engineering and technology")
#mechanics = Major.tagged_with("mechanics and construction")
#transportation = Major.tagged_with("transportation")
#science = Major.tagged_with("science")
#math = Major.tagged_with("math")
#resources = Major.tagged_with("natural resources")
#healthcare = Major.tagged_with("health care")
#social_sciences = Major.tagged_with("social sciences")
#education = Major.tagged_with("education")
#law = Major.tagged_with("law")
#management = Major.tagged_with("management and marketing")
#administrative = Major.tagged_with("administrative and clerical")
#services = Major.tagged_with("services")
#tags = Major.tag_counts
end
You can see the duplication. This is compounded on the view template.
Here's part of the index.html.erb page:
<!-- Creative Arts -->
<h2 class="major-categories-landing">Creative Arts</h2>
<% #creative.sample(10).each do |creative| %>
<%= link_to creative, class: 'span2 category-landing' do %>
<%= image_tag creative.image(:similar), class: 'img-polaroid', id: 'category-landing-list' %>
<p class="category-landing-list-name"><%= creative.name %></p>
<% end %>
<% end %>
<%= link_to "View all #{#creative.count} majors in this category.", category_path("creative arts"), class: "view-category-show-page" %>
<!-- Social Sciences -->
<h2 class="major-categories-landing">Social Sciences</h2>
<% #social_sciences.sample(10).each do |ss| %>
<%= link_to ss, class: 'span2 category-landing' do %>
<%= image_tag ss.image(:similar), class: 'img-polaroid', id: 'category-landing-list' %>
<p class="category-landing-list-name"><%= ss.name %></p>
<% end %>
<% end %>
<%= link_to "View all #{#social_sciences.count} majors in this category.", category_path("social sciences"), class: "view-category-show-page" %>
and so on for each category. I have tried #category = Major.tagged_with(params[:tag]) and many variations to that to no avail. This is my first time working with acts_as_taggable_on and although I've read the documentation over and over I can't quite figure this out.
I hope to extend this out throughout the application and so I want to figure it out now before I get a lot duplicate code. Thanks for sharing any ideas or suggestions!!
I am running a rails 3.2.11 app.
UPDATE
Here's how much better this is now:
My categories_controller.rb file:
def index
#major_categories = ["creative arts", "social sciences", "science", ....]
end
My index.html.erb page:
<% #major_categories.each do |c| %>
<!-- Title and blue strip -->
<div class="category-strip">
<div class="container">
<h2 class="major-categories-landing"><%= c %></h2>
</div>
</div>
<!-- Show Each Major in this Category -->
<div class="container">
<div class="row-fluid">
<% Major.tagged_with(c).order('RANDOM()').limit(10).each do |major| %>
<%= link_to major, class: 'span2 category-landing' do %>
<%= image_tag major.image(:similar), class: 'img-polaroid' %>
<p class="category-landing-list-name"><%= major.name %></p>
<% end %>
<% end %>
</div>
<!-- Link to View All Majors -->
<div class="row-fluid">
<div class="view-all-category">
<%= link_to "View all #{Major.tagged_with(c).count} majors in this category.", category_path(c), class: "view-category-show-page" %>
</div>
</div>
</div>
<% end %>
I would do something like this:
# in categories_controller.rb
def index
#categories = ["creative arts", "engineering and technology",
"mechanics and construction", ...]
end
# in index.html.erb
<%= render partial: "category", collection: #categories %>
# in _category.html.erb
<h2 class="major-categories-landing"><%= category.titleize %></h2>
<% Major.tagged_with(category).order('rand()').limit(10).each do |major| %>
<%= link_to major, class: 'span2 category-landing' do %>
<%= image_tag major.image(:similar), class: 'img-polaroid',
id: 'category-landing-list' %>
<p class="category-landing-list-name"><%= major.name %></p>
<% end %>
<% end %>
<%= link_to "View all #{Major.tagged_with(category).count} majors in this category.",
category_path(category), class: "view-category-show-page" %>
Btw: The link to each major is invalid html. A link (because a it is an inline element) should not contain a paragraph (because p is a box element). Furthermore each link for each category would have the same id, but ids must be unique in each html document.
I have a content tag that is creating jquery sortable output.
Some items I don't want sortable, so I've added the following to the jquery:
cancel: ".ui-state-disabled"
So, now I need to put "ui-state-disabled" into the li class.
Currently the code creating the li is this:
<% wostatus.workorders.each do |workorder| %>
<%= content_tag_for(:li, workorder) do %>
<div class="<%= workorder.type.maximo_no %> <%= workorder.priority %> ">
<a href="<%= workorder_path(workorder) %>">
<strong><%= workorder.wonum %></strong>
<%= workorder.description %>
</a>
</div>
<% end %>
<% end %>
The results in HTML are:
<li class="workorder" id="workorder_36">
<div class=" ">
<a href="/workorders/36">
<strong>13-39870</strong>
Added some text again
</a>
</div>
</li>
In the browser, if I edit the li class to include "ui-state-disabled" it works the way I want.
Now, how can I insert the "ui-state-disabled" into the li status if the workorder.wostatus.id = 232 ??
Thanks for your help!!
Make a helper:
def disabled_workorder_li(workorder)
{:class => "ui-state-disabled"} if workorder.id == 232
end
Then in the view:
<%= content_tag_for(:li, workorder, disabled_workorder_li(workorder) %>
Here's what it would look like if you skipped the helper and tried to do it all in the view:
<%= content_tag_for(:li, workorder,
(workorder.id == 232) ? {:class => "ui_state_disabled"} : nil %>
That looks terrible. Putting it into a helper will also make it easier to test.