rails undefine method size nil class for - ruby-on-rails

i tried to have this in my navaigation which will show products in cart
<% if !#order_item.nil? && #order_item.errors.any? %>
<div class="alert alert-danger">
<ul>
<% #order_item.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<% if #order_items.size == 0%>
<p class="text-center">
There are no items in your shopping cart. Please <%= link_to "go back", root_path %> and add some items to your cart.
</p>
<% else %>
<% #order_items.each do |order_item| %>
<%= render 'carts/cart_row', product: order_item.product, order_item: order_item, show_total: true %>
<% end %>
<% end %>
after adding this to the navigation when i visit other pages it fail but when the cart page it works. how can i show this in every page?

You can use before_filter to call in every controller. Where order_items should be written in application controller in a method.

I thing your #order_items is return the collection type , So please check your #order_items object have data ?
by using this methods
.empty? or .nil?
it return the true or false , you get you result.

Related

create instance variable in erb template

Say I have 2 erb views: a list view and a show view, I want to render the show view inside the list view
list view
<% #vehicles.each do |vehicle| %>
<h2><%= vehicle.type %></h2>
<ul>
<% vehicle.accessories.each do |accessory| %>
<li><% accessory.title %></li>
<% end %>
</ul>
<% end %>
show view
<h2><%= #vehicle.type %></h2>
<ul>
<% #vehicle.accessories.each do |accessory| %>
<li><% accessory.title %></li>
<% end %>
</ul>
the issue is the show view takes an #vehicle instance variable, how can I pass that down from the parent if I was going to nest these and it still shows up as an instance variable, accessed with #vehicle? something like:
<% #vehicles.each do |vehicle| %>
<%= render "show" %>
<% end %>
I guess you might want to make that show view a partial. Something like:
<h2><%= vehicle.type %></h2>
<ul>
<% vehicle.accessories.each do |accessory| %>
<li><% accessory.title %></li>
<% end %>
</ul>
Then you can do something like:
<% #vehicles.each do |vehicle| %>
<%= render "path/to/show_partial", locals: {vehicle: vehicle} %>
<% end %>
Naturally, you'll want to:
use the real path/to/show_partial, whever that happens to be, and
do some eager loading so that you're not doing a whole bunch of N+1 queries.

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 %>

Rails 4 params not being sent to the controller

I have been battling with what is going to turn out to be a quick answer for someone...
I have view with nested partials. The index has a partial to the categories which has a partial to the subcategories.
The categories are looping as expected but for some reason the locals don't seem to be reaching the controller.
My code is as follows:
index.html.erb
<%= render partial: "categories/categories", object: #categories %>
_categories.html.erb
<% #categories.each do |category| %>
<% if category.category_parent == 0 %>
<li><%= category.category_name %> <i class="icons icon-right-dir"></i>
<%= render partial: "categories/subcategories", object: #subcategories, locals: {parent_id: category.category_id} %>
</li>
<% end %>
<% end %>
_subcategories.html.erb
<% if #parent != nil %>
<ul class="sidebar-dropdown">
<li>
<ul>
<% #subcategories.each do |subcategory| %>
<li><%= subcategory.category_name %> <i class="icons icon-right-dir"></i></li>
<% end %>
</ul>
</li>
</ul>
<% end %>
categories_controller.rb
class CategoriesController < ApplicationController
before_action :categories, :subcategories
def categories
#categories = Category.all
end
def subcategories
#parent = params[:parent_id]
#subcategories = Category.where(:category_parent => params[:parent_id])
end
end
#categories is an instance variables, and you shouldn't need to pass it from view to view. So the first render statment becomes:
<%= render "categories/categories" %>
(I'm more familiar leaving off partial:)
_categories.html.erb becomes this:
<% #categories.each do |category| %>
<% if category.category_parent == 0 %>
<li><%= category.category_name %> <i class="icons icon-right-dir"></i>
<%= render "categories/subcategories", category: category %>
<%# Note passing in category to have access to it as a local %>
</li>
<% end %>
<% end %>
_subcategories.html.erb
<% if category != nil %>
<ul class="sidebar-dropdown">
<li>
<ul>
<% category.subcategories.each do |subcategory| %>
<li><%= subcategory.category_name %> <i class="icons icon-right-dir"></i></li>
<% end %>
</ul>
</li>
</ul>
<% end %>
I don't think you should be looking up #subcategories in your controller. It looks like in def categories you're just getting all the categories and then looping through them in the view. As it is, this will be n+1, as every time it loops over a new category, it will have to do a db query to find its subcategories. You can avoid this with includes.
categories_contorller.rb
def categories
#categories = Category.all.includes(:subcategories)
end
This is what I think is simplest and clearest. But I also haven't played with object: too much. This post might help you if you want to stick to that pattern:

Control Structure in Rails Partial

I'm building a Rails app up from the Sample App featured in Michael Hartl's book. In order to display error messages on user signup, I'm using a partial in the shared directory - app/views/shared/_error_messages.html.erb:
<%if #fact %>
<% #data = #fact %>
<% elsif #user %>
<% #data = #user %>
<% end %>
<% if #data.errors.any? %>
<div id="error_explanation">
<div class="alert alert-error">
The form contains <%= pluralize(#data.errors.count, "error") %>.
</div>
<ul>
<% #data.errors.full_messages.each do |msg| %>
<li>* <%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
Originally, this partial just started with something like:
<% if #user.errors.any? %>
However, since I've decided to re-use this partial to show errors on other pages, I'm having to use different objects (#user, #fact) depending on which page I'm using it on. This is easily solved by adding that IF statement at the top,
<%if #fact %>
<% #data = #fact %>
<% elsif #user %>
<% #data = #user %>
<% end %>
-but this feels icky. Is there a controller somewhere I should be putting this kind of logic for shared partials?
You can pass local variables to partial instead:
<% if object.errors.any? %>
<div id="error_explanation">
<div class="alert alert-error">
The form contains <%= pluralize(object.errors.count, "error") %>.
</div>
<ul>
<% object.errors.full_messages.each do |msg| %>
<li>* <%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
And in your template, for example:
<%= render 'shared/error_messages', object: #user %>
Marek's answer is probably the best. However, here is an alternative which though not scalable, is closer to what you have done.
Just replace:
<%if #fact %>
<% #data = #fact %>
<% elsif #user %>
<% #data = #user %>
<% end %>
with:
<% #data = #fact||#user %>
You could use render to point to the partial from your controllers for users and fact. Check out the API documentation on rendering partials.

notices and errors on rails3

I read somewhere that rails 3 form helper does not have error messages embedded in it anymore. I am wondering how I am supposed to show flash messages when I set them up inside my controller or as an inline notice in redirect_to? How am I supposed to display them on my view? Is there helper for this?
For example if I have
def update
if #person.save
flash[:notice] = "Successfully saved!"
end
end
how do i show the notice on my view?
flash will still work as long as you display it in your layouts:
<div id="page">
<% if flash[:alert] %>
<p class="flash-error"><%= flash[:alert] %></p>
<% end %>
<% if flash[:notice] %>
<p class="flash-notice"><%= flash[:notice] %></p>
<% end %>
<%= yield %>
</div>
You can either display error messages manually or use the dynamic_form gem which gives you the old behavior.
You can still display flash messages in your view with this:
<%= flash[:notice] %>
But if you want to display for error messages:
#In your form
<%= form_for #foo do |f| %>
<%= render "shared/error_messages", :target => #foo %>
...
<% end %>
#shared/_error_messages.html.erb
<% if target.errors.any? %>
<div id="error_explanation">
<ul>
<% target.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

Resources