Undefined method `errors' for nil:NilClass - Static Pages Rails - html

I try to add a link in the menu to a simple html page, but when I try to go to the link I get an error as in the title. I'm newbie in Rails and I can't understand this error, I try add one static html page. Where is the problem? Please help.
In menu
<li><%= link_to "test" , pages_test_path %></li>
Routes
Test::Application.routes.draw do
post "inquiry/create"
root :to => 'welcome#index'
resources :inquiries
get "pages/test"
end
Pages controller
class PagesController < ApplicationController
def show
end
end
Test html
<!DOCTYPE html>
<html>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
</body>
</html>
Error messages
<% if item.errors.any? %>
<div class="alert alert-danger fade in" id="error_messages">
<div id="error_explanation">
<ul class="list-unstyled">
<% item.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
</div><!--/alert-->
<% else %>
<div id="error_messages" class="hide"></div>
<% end %>

It seems item object is nil. Meaning this is not declared in your controller.
Declare the variable #item in your controller like below.
#item = Item.find(your_item_id)
or
item = Item.new(your_params)
#item = item.save
Then change your html like below.
<% if #item.errors.any? %>
<div class="alert alert-danger fade in" id="error_messages">
<div id="error_explanation">
<ul class="list-unstyled">
<% #item.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
</div><!--/alert-->
<% else %>
<div id="error_messages" class="hide"></div>
<% end %>

Related

Middleman Blog Pagination shows all articles

I have been trying to activate Pagination for my Middleman Blog, but for some reason, even though pagination itself seems to work (as signified by the buttons at the bottom, leading me until page three), all articles are shown on each individual page, instead of just ten
section in config.rb
activate :blog do |blog|
blog.name = "en"
blog.prefix = "blog"
blog.permalink = "/{year}/{month}/{day}/{title}.html"
blog.sources = "/posts/:year-:month-:day-:title.html"
blog.layout = "blog_layout"
blog.tag_template = "tag.html"
blog.paginate = true
blog.page_link = "page:num"
blog.per_page = 10
end
Blog Index.html
---
pageable: true
blog: en
priority: 1.0
change_frequency: weekly
description: Tbd.
title: Blog
---
<div class="page-content">
<div class="container">
<ul class="blog-post-list">
<% blog.articles.each do |article| %>
<li>
<a href="<%= article.url %>">
<div>
<img src="<%= article.data.image %>" class="blog-post-list__image" />
</div>
<div class="blog-post-list__post-preview">
<p class="blog-post-list__title"><%= article.title %></p>
<p><%= article.summary(140, '...') %></p>
<span><%= article.date.strftime('%B %e, %Y') %></span>
</div>
</a>
</li>
<% end %>
<ul class="paginate">
<% if paginate && num_pages > 1 %>
<% if prev_page %>
<p class="previous"><%= link_to 'Previous page', prev_page %></p>
<% end %>
<% end %>
<% if paginate %>
<% if next_page %>
<p class="next "><%= link_to 'Next page', next_page %></p>
<% end %>
<% end %>
</ul>
</ul>
</div>
</div>
Any hints what could be the issue here, that causes all articles to be displayed anyways would be highly appreciated! Thanks in advance :)
Is that not because you use blog.articles.each and should use page_articles instead?
I think you have to change blog.articles.each (the total number of articles) and use something like <% page_articles.each do |article| %> to get the number of articles you want on your page.

Rails Assigning a class in html erb works in one place, doesn't work in another

I have a view where I make 2 loops to output the information. One as a menu and another as data:
<div class="base-form side-menu">
<ul class="food-groups">
<% #food_categories.each do |category| %>
<li class="<%= "#{Food::CATEGORIES[category]}"%>" data-content=".<%= "#{Food::CATEGORIES[category]}-content"%>"> <%="#{category}"%> </li>
<% end %>
</ul>
</div>
<div class="base-form main-form">
<% #food_categories.each do |cat| %>
<%= "#{Food::CATEGORIES[cat]}-content" %>
<div class="<%= "#{Food::CATEGORIES[cat]}-content"%>">
<% #foods.where(category: "#{Food::CATEGORIES[cat]}").find_each do |food| %>
<p>
<%= "Name: #{food.name}, portion: #{food.portion}, calories: #{food.calories}" %>
</p>
<% end %>
</div>
<% end %>
</div>
First loop works fine, each element gets its class correctly. Second loop(with |cat|) works, but the div's don't get any classes assigned. I even copied the class="<%=...%>" to the 't' from the first loop and it still doesn't work! Although just putting the string on the page that I'm also passing as a class works fine. I don't get it!

Insert HTML with embedded Ruby

Is there a way that I can insert HTML tags with embedded Ruby?
I'm trying to loop over items in my database and need to put them in a bootstrap grid.
<% #div = "</div> <div class='row'> <div class='col-md-3'>" %>
<% #end = "</div>" %>
<div class="row">
<% #branches.each_with_index do |b, index| %>
<div class="col-md-3">
<div class="cardje transition">
<h2 class="transition"><%= b.description %></p>
<div class="cta-container transition">Import branch</div>
<div class="cardje_circle transition"></div>
</div>
</div>
<% if index+1%4 == 0 %>
<%= #div.html_safe %>
<% end %>
<% if index+1 == #branches.count %>
<% #end.html_safe %>
<% end %>
<% end %>
There must be a simpler way but I need this page to be done by tomorrow morning and I've been working way too long.
When I check the source code it's running only the loop and never implementing anything (so everything is on one row and overlapping).

Some HTML for values of Hash keys

I am building a panel with a partial, but I'd like to customise the inside of the panel with some html. Is there any way to write some HTML for a hash value?
I have a custom partial _panel_builder.html.erb that I takes as argument pclass, heading, body, etc., that I would like to use like this :
(The below syntax is bad, but I don't really understand how I could do something nice..)
<% #etudes.each_with_index do |etude, i| %>
<%= render 'shared/panel_builder',
pclass: panel_color_rotation(i),
heading: etude.name,
# For the body param, I'd like to be able to use some HTML with occasional <%=...%> for variables, like :
body: (%>
<p><%=etude.description %></p>
<ul>
<%etude.competences.each do |comp| %>
<li><strong><%= competence.name %></strong> : <%=competence.level %>
<br><small><%=competence.why %></small>
</li>
<% end %>
</ul>
<%).html_safe,
collapsable: true %>
<% end %>
EDIT : An idea of what my _panel_builder partial looks like :
<%
collapsable ||= false
pclass ||= "default"
footer ||= false
%>
<div class="panel panel-<%= pclass %> <%= if collapsable then "panel-collapsable " end %>">
<div class="panel-heading">
<%= heading %>
<% if collapsable %>
<span class="pull-right"><i class="glyphicon glyphicon-chevron-down"></i></span>
<% end %>
</div>
<div class="panel-body <%= if collapsable then "collapse" end %>">
<%= body %>
</div>
<% if footer %>
<div class="panel-footer <%= if collapsable then "collapse" end %>">
<%= footer %>
</div>
<% end %>
</div>
Okay so I was actually looking for the capture helper :
<% #etudes.each_with_index do |etude, i| %>
<%= render 'shared/panel_builder',
pclass: panel_color_rotation(i),
heading: etude.name,
body: capture do %>
<p><%=etude.description %></p>
<ul>
<%etude.competences.each do |comp| %>
<li><strong><%= competence.name %></strong> : <%=competence.level %>
<br><small><%=competence.why %></small>
</li>
<% end %>
</ul>
<% end %>,
collapsible: true %>
<% end %>
Note : in some cases, I also want to pass a complex HTML block as the header of footer, so I can't just have a helper that takes a block.
Note2 : My panel is a HTML template made for generic data. I cannot pass it a Ruby object

Writing HTML code with Rails

<% #ticket.conversations.each do |c| %>
<section class="messages">
<%="<li> #{c.the_message} </li>" %>
</section>
<%end%>
I am trying to have rails write the HTML code for me so the output would look something like this:
<li>MESSAGE1</li>
<li>MESSAGE2</li>
<li>Next message here...</li>
I am going to style every nth element to have a different style to show what speaker it belongs to. But currently is just outputs straight text and escapes the HTML. How do I stop this escape?
To output you need to use <%= as follows within your <section> block:
<%= "<li> #{c.the_message} </li>".html_safe %>
But currently is just outputs straight text and escapes the HTML
You can use the html_safe method. Please refer to the "Extensions to String" topic in this document: http://guides.rubyonrails.org/active_support_core_extensions.html
Another option you can use is the raw helper(as pointed out by Stefan) which calls the html_safe for you. e.g.
<%= raw "<li> #{c.the_message} </li>" %>
You can also style your list items this way:
<li><%= c.the_message %></li>
Just based upon preference.
Try it this way:
<% #ticket.conversations.each do |c| %>
<section class="messages">
<li><%= c.the_message %></li>
</section>
<% end %>
Or if you don't want to repeat <section> every time:
<section class="messages">
<% #ticket.conversations.each do |c| %>
<li><%= c.the_message %></li>
<% end %>
</section>