Linking to basket/cart in ruby on rails - ruby-on-rails

I have a menu in my header that has a show basket and a login button, each work when the code is placed in separately but not when both lines are in the file.
I'm using devise for the users.
Is there a better way to link to the current basket?
<li><%= link_to basket_path(#basket.id) do %>
<%= image_tag "/assets/viewBasket.png" %>
</li>
<% end %>
<% if signed_in? %>
<li><%= link_to edit_user_registration_path do%>
<%= image_tag"/assets/my_account.png" %></a></li>
<% end %>
<li><%= link_to destroy_user_session_path do%>
<%= image_tag"/assets/logout.png" %></li>
<%end%>
<% else %>
<li><%= link_to new_user_session_path do%>
<%= image_tag"/assets/loginRegisterBtn.png" %></li>
<% end%>
<% end %>
If I run on its own this works but not with the code after.
<li><%= link_to basket_path(#basket.id) do %>
<%= image_tag "/assets/viewBasket.png" %></li>
<% end %>
I think its to do with the way the current basket is set with the session id in the current_basket model.
module CurrentBasket
private
def set_basket
#basket = Basket.find(session[:basket_id])
rescue ActiveRecord::RecordNotFound
#basket = Basket.create
session[:basket_id] = #basket.id
end
end

The closing of <li> must be after the end of the link, like this:
<li>
<%= link_to basket_path(#basket.id) do %>
<%= image_tag "/assets/viewBasket.png" %>
<% end %>
</li>

I used the answer above which helped with one issue, however, I found that I had defined only the shop and index page. Removing this and it now works.
include CurrentBasket
before_action :set_basket, only: [:index, :shop]

Related

How to make links for index and show actions work in partial views

I am using a partial to render a nav bar from the layouts folder in all my pages, and within the nav bar I have links to actions such as index and show from a GradesController. I understand that in order for the links to work I must call them in that page's action in the controller as such:
def home
#grades = Grade.all
end
However as these links exist within a partial view which doesn't have a corresponding controller, how can I make these links work without getting a NoMethodError?
layouts/_navbar.html.erb
<div class="main_nav_color">
<div class="wrapper">
<header id="main_header" class="cf">
arabecademy
<nav>
<ul>
<li>
<%= link_to "#" do %>
Grade<span class="glyphicon glyphicon-chevron-down"></span>
<% end %>
<ul>
<% #grades.each do |grade| %>
<li><%= link_to "Grade" + grade.grade_number, grade_courses_path(grade, #course) %></li>
<% end %>
</ul>
</li>
<li>About</li>
<li>Contact</li>
<% if student_signed_in? %>
<li><%= link_to "Log Out", destroy_student_session_path, method: :delete %></li>
<li><%= link_to "Accout Settings", edit_student_registration_path %></li>
<% else %>
<li><%= link_to "Log in", new_student_session_path %></li>
<li><%= link_to "Sign Up", new_student_registration_path, :class => "button_sm button_orange" %></li>
<% end %>
</ul>
</nav>
</header>
</div>
</div>
To get #grades set all the time, you can add a before_filter in ApplicationController like this:
class ApplicationController
before_action :load_grades
def load_grades
#grades = Grade.all
end
end
This will make sure that every single controller action has #grades set in addition to whatever the action itself does. Your partial should be able to pick up #grades wherever you display it now.

Check if Spree Cart is empty

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

Accessing list of projects from accounts

Below is the code which gives me list of accounts.
<% #accounts.each do |account| %>
<li><%= link_to account.name %></li>
<% end %>
How to access the list of projects of my specific account.
Try this:
<% #accounts.each do |account| %>
<li><%= link_to account.name %></li>
<li><%= link_to account.projects %></li> #this will give you a collection of projects associated with that account
<% end %>
If you want a link_to for each individual project then you'll have to use another loop like this:
<% #accounts.each do |account| %>
<li><%= link_to account.name %></li>
<% account.projects.each do |project| %>
<li><%= link_to project %></li> #this will give you individual project associated with that account
<% end %>
<% end %>
Edit:
Incase you don't have any projects for an account you can do:
<% #accounts.each do |account| %>
<li><%= link_to account.name %></li>
<% if account.projects %>
<% account.projects.each do |project| %>
<li><%= link_to project %></li> #this will give you individual project associated with that account
<% end %>
<% else %> # add this else block to execute your code when there are on projects
<p> No projects associated with your account</p>
<% end %>
<% end %>

Unable to search queries from database in rails 3

I am working on rails to search the desired results. I have a table called coordinates, and I want a search form for that my model.rb
class Coordinates<ActiveRecord::Base
attr_accessible :city , :latitude, :longitude
end
coordinates_controller.rb
class CoordinatesController<ApplicationController
def show
#coordinates=Coordinates.where("city = ?", params[:search]).all
end
end
index.html.erb
<%= form_tag({controller: "coordinates", action: "show"}, method: "get") do %>
<%= label_tag(:coordinates,"search for:") %>
<%= text_field_tag(:show) %>
<%= submit_tag("search") %>
<% end %>
search.html.erb
<%= form_for #coordinates.search do |coordinates| %>
<ul>
<li><%= coordinates.latitude %></li>
<li><%= coordinates.longitude %></li>
<li><%= coordinates.longitude %></li>
</ul>
<% end %>
but I am getting this error:
undefined method `search' for []:Array
.. please help
To begin, try changing the form code in index.html.erb from:
<%= form_tag({controller: "coordinates", action: "show"}, method: "get") do %>
<%= label_tag(:coordinates,"search for:") %>
<%= text_field_tag(:show) %>
<%= submit_tag("search") %>
<% end %>
To:
<%= form_tag({controller: "coordinates", action: "show"}, method: "get") do %>
<%= label_tag(:search,"search for:") %>
<%= text_field_tag(:search) %>
<%= submit_tag("Search") %>
<% end %>
This will populate params[:search] with the value of the text field. Currently you are passing your query as params[:show].
Next, try changing your search results view to:
<% #coordinates.each do |coordinates| %>
<ul>
<li><%= coordinates.latitude %></li>
<li><%= coordinates.longitude %></li>
<li><%= coordinates.longitude %></li>
</ul>
<% end %>
This iterates over the #coordinates collection returned by your controller.
From the line
undefined method 'each' in nil:Nilclass
It seems that there are no coordinates for the city you looking for in your database
after the call
#coordinates=Coordinates.where("city = ?", params[:search]).all
check #coordinates.nil? and #coordinates.count to see if this is the case
And beside it is a better practice to check it before using it - so you can handle it in the way you want - in case that something went wrong in your DB and you don't want the user to get such errors on production

cannot display the view from the url parameter in ruby

I was trying to display a list of the blog with the same category which display on the url parameter. however when i click the link it still stay at the same page.
route
match '/microposts/:category', :to => 'microposts#category_list'
view
<h2>sidebar</h2>
<% #categories= Micropost.select("category").group("category")%>
<% unless #categories.nil? %>
<ul><% #categories.each do |category| %>
<li><%= link_to category.category, :controller =>"microposts", :category => category.category, :method => 'category_list' %></li>
<% end %>
</ul>
<% end %>
After click the link enter category_list view.
category_list.html.erb
<h2>Catergory</h2>
<% #microposts.each do |post|%>
<article>
<h4><%= link_to post.title, post %>| <%= post.created_at %></h4>
</article>
<% end %>
<%= will_paginate #microposts %>
microposts controller
def category_list
#micropost = Micropost.select("category").where(params[:category])
#title = "Category"
end
Ok, try this:
UPDATED:
microposts_controller.rb
def category_list
#microposts = Micropost.where('category = ?', params[:category])
#categories = Micropost.select('DISTINCT(category)').map(&:category).compact
...
end
routes.rb
match '/microposts/:category' => 'microposts#category_list', :as => :microposts_category_list
_sidebar.html.erb
<h2>sidebar</h2>
<% unless #categories.empty? %>
<ul>
<% #categories.each do |category| %>
<li><%= link_to category, microposts_category_list_path(category) %></li>
<% end %>
</ul>
<% end %>

Resources