Check if Spree Cart is empty - ruby-on-rails

How can I check if the Spree cart is empty, so I can change the button if it isn't?
I thought there was a method line_items.count...
<% unless line_items.count > 0 %>
<%= link_to "<button>Empezar Pedido</button>".html_safe, "/shop" %>
<% else %>
<%= link_to "<button>Terminar Pedido</button>".html_safe, "/shop" %>
<% end %>
Thanks!

This is very simple I solved my this problem like this
In application_controller.rb
def load_cart
#order = current_order
end
In frontend in you case
<% unless #order.line_items.count > 0 %>
<%= link_to "<button>Empezar Pedido</button>".html_safe, "/shop" %>
<% else %>
<%= link_to "<button>Terminar Pedido</button>".html_safe, "/shop" %>
<% end %>
Thanks

Related

How to display related items

I want to display related experiences below the local experiences. I tried to do it using #experiences= Experience.excludes(house.experiences) but didnt work out. Can I get any suggestion how to do that.
<h3>Local experiences</h3>
<section class="list-box">
<% #house.experiences.each do |experience| %>
<p>
<%= experience.name %>
<% if logged_in? && current_user.role == "customer" %>
<%= link_to "Make Enquiry", new_message_path(receiver_id: experience.supplier.user) %>
<% else %>
<%= link_to "Make Enquiry", new_customer_path %>
<% end %>
</p>
<% end %>
</section>
<h3>Related experiences</h3>
<section class="list-box">
<% #experiences.each do |experience| %>
<p>
<%= experience.name %>
<% if logged_in? && current_user.role == "customer" %>
<%= link_to "Make Enquiry", new_message_path(receiver_id: experience.supplier.user) %>
<% else %>
<%= link_to "Make Enquiry", new_customer_path %>
<% end %>
</p>
<% end %>
</section>
There is no method called excludes in the rails model. There is only exclude?, which is a instance method of class String. Refer here.
For your issue, maybe something like below would work given that house_id is the foreign_key in your experience table.
#experiences = Experience.where.not(house_id: house.experiences)
You may refer here for more not conditions.

Allow unauthenticated users to access views that utilize current_user

I have lots of views that have elements that check if the current user isn't the same user that's being viewed, etc. For example, on the user show page there is a button that allows a user to follow another user, which doesn't appear if the user is looking at their own profile.
<% if current_user != #user %>
<div id="follow_form">
<% if current_user.following?(#user) %>
<%= render 'users/unfollow' %>
<% else %>
<%= render 'users/follow' %>
<% end %>
</div>
<% end %>
The problem arises that if you're not logged in, rails throws an error.
undefined method `id' for nil:NilClass
<% if current_user.id == #user.id %>
<%= link_to "Edit Profile", edit_user_registration_path(#user) %>
<% else %>
<%= link_to 'message', new_message_path(receiver_id: #user.id) %>
...
I don't want to have to force people to log in or sign up to view index or show pages. How can I get around this?
You should try that:
<% if user_signed_in? %>
<% if current_user != #user %>
<div id="follow_form">
<% if current_user.following?(#user) %>
<%= render 'users/unfollow' %>
<% else %>
<%= render 'users/follow' %>
<% end %>
</div>
<% end %>
<% end %>

How do I create this link_to conditional in a DRY way?

I want to do the following:
<% if current_user.has_role? :demo %>
<%= link_to profile_path(#selected_profile) do %>
<% else %>
<%= link_to profile_path(profile) do %>
<% end %>
What's throwing it off is the beginning of the block in the link_to, within the if statement.
So how do I achieve this without having to duplicate ALL of the code within this if block twice?
Edit 1
This is the error I get from the above code:
SyntaxError at /
syntax error, unexpected keyword_else, expecting keyword_end
'.freeze; else
^
You can do it like this:
<% chosen_profile = current_user.has_role?(:demo) ? #selected_profile : profile %>
<%= link_to profile_path(chosen_profile) %>
So this will not repeat your link_to tag which you need to do. As you have to redirect to the same path and just change the profile object then this will work. You may change the ternary to if else block if the line seems very long and not readable.
And as everyone mentioned that don't use a do after link_to until you need a block. So that will fix your error.
You can achieve this by defining method in you user.rb (Model)
def demo?
self.has_role?("demo")
end
Then you write in your view
<% if current_user.demo? %>
<%= link_to profile_path(#selected_profile) %>
<% else %>
<%= link_to profile_path(profile) %>
<% end %>
This may help you.
do should have end.
Here is the Ruby Doc reference for link_to
Here is more about do in Ruby
<% if current_user.has_role? :demo %>
<%= link_to profile_path(#selected_profile) do %>
selected profile
<% end %>
<% else %>
<%= link_to profile_path(profile) do %>
profile
<% end %>
<% end %>
You are getting error because of do, you are opening the block but not closing it, try this code
<% if current_user.has_role? :demo %>
<%= link_to 'Profile', profile_path(#selected_profile) %>
<% else %>
<%= link_to 'Profile', profile_path(profile) %>
<% end %>
or, you can do it in controller instead
#selected_profile = current_user.has_role?(:demo) ? #selected_profile : profile
and then in the view,
<%= link_to 'Profile', profile_path(#selected_profile) %>
Hope that helps!

Unable to get the value of a variable inside Rails erb tags

I want to understand the erb usage. In the below code I am unable to figure out how to get the value of (group.id) in the if clause using the erb tags.
This probably has a very basic solution but I am unable to get proper answers.
The below code gives me syntax error.
<% current_user.favorite_groups.to_a.each do |group| %>
<%= if (group.id).newfavorite_texts.exists?(id: text.id) %>
<%= group.name %>
<%= link_to # do something %>
<% else %>
<%= group.name %>
<%= link_to # do something else %>
<% end %>
<% end %>
Thanks in advance.
You should be get going with the below code
<% current_user.favorite_groups.to_a.each do |group| %>
<% if group.newfavorite_texts.exists?(id: text.id) %>
<%= group.name %>
<%= link_to # do something %>
<% else %>
<%= group.name %>
<%= link_to # do something else %>
<% end %>
<% end %>

Rails loop in html erb

How can I simplify the following lines:
<% if #campaign.previous_campaign.present? %>
<%= #campaign.previous_campaign.product_name %>
<% if #campaign.previous_campaign.previous_campaign.present? %>
<%= #campaign.previous_campaign.previous_campaign.product_name %>
<% end %>
<% end %>
I need to keep adding ".previous_campaign" until it is not present. So the next one in the above code would be:
<%= #campaign.previous_campaign.previous_campaign.previous_campaign.product_name %>
etc etc.
Something like this:
<% campaign = #campaign %>
<% while campaign.previous_campaign.present? %>
<% campaign = campaign.previous_campaign %>
<%= campaign.product_name %>
<% end %>
The code may need some debugging, but I guess the idea is clear
You could do something like this:
<% for c in #campaign do %>
<% if c.previous_campaign.present? %>
<%= c.previous_campaign.product_name %>
<% end %>
<% end %>

Resources