Currently working with Rails 5.0.7.2 and ruby 2.4.10p364 for learning purposes.
I'm still a newbie. I have an embedded index.html file using Postgres. It seems to me that it's just a typing error in the script itself, but I can't confirm. Everything I try to do, It keeps returning me the "undefined method `each' for nil:NilClass" error.
So far, I've tried to modify the embedding style, but it keeps giving the me Exception in Action Controller error thing just right after the header.
<h1>Portfolio Items</h1>
<% #porfolio_items.each do |portfolio_item| %>
<p><%= portfolio_item.title %></p>
<p><%= portfolio_item.subtitle %></p>
<p><%= portfolio_item.body %></p>
<%= image_tag portfolio_item.thumb_image %>
<% end %>
This is the controller.
class PortfoliosController < ApplicationController
def index
#portfolio_items = Portfolio.all
end
end
I've even tried to add a "<% if #portfolio_items %>" separate line of code, but to no avail.
Any help would be appreciated. Is this a version thing or is there something I'm not looking at?
That's because #porfolio_items is nil. You have a typo there, a missing "T", it's not the same the instance variable from the index action.
Related
So I am making an app to manage projects and its task,
using a tutorial I have made some basic stuff.
Now I want to add a timeline to my page, so I wrote this in my pr_list.rb (model file)
def timeline
#pr_lists.each do |pr_list|
pr_list.pr_items.each do |pr_item|
if pr_item.completed?
#timeline = "#{pr_item.descrpition}: {#{pr_item.completed_at}}"
end
end
end
end
And i called it in my pr_list/views/index page.
<h1><%= pr_list.timeline %></h1>
but I get
NameError (undefined local variable or method `pr_list' for #<ActionView::Base:0x00000000029fb8>
Did you mean? #pr_lists")
At this point even if I just code it like to output a string, it just does not work.
if I try any other things, it gives me a NoMethodError
while the rest of the methods work.
and the above code also works if I write it in HTML.ERB index file as an embedded ruby.
I am new to Ruby on Rails.
pr_list is local to the block defined in the controller so it is not accessible from the template.
You could iterate on #pr_lists from within the view file (see this question for reference):
<% #pr_lists.each do |pr_list| %>
<% pr_list.pr_item.each do |pr_item| %>
<% if pr_item.completed? %>
<h2><%= pr_item.description %>: <%= pr_item.completed_at %></h2>
<% end %>
<% end %>
<% end %>
I'm having trouble figuring out how to display an index.
In my organisation requests view folder, I have a file called index.html.erb.
In that file, I'm trying to list each organisation request. I've tried each of the following formulations:
<% OrganisationRequest.each do |OrgReq| %>
<% organisation_request.each do |OrgReq| %>
<% #organisation_request.each do |OrgReq| %>
<% #organisation_requests.each do |OrgReq| %>
In each case, I get an error that says:
formal argument cannot be a constant
I thought a constant meant something beginning with a capital letter. 3 of the above attempts don't begin with a capital letter.
It's also confusing to me since in my user index, I have <% User.each %> and I don't get an error message.
Can anyone see what's gone wrong? How do I ask for a list of objects?
If you have your data and view right, you should be able to fix with:
<% #organisation_requests.each do |org_req| %>
...
<% end %>
If we stick Rails conventions, we'd say that, you have a OrganisationRequests controller, has such content.
class OrganisationRequestsController < ApplicationController
...
def index
#your_local_variable = OrganisationRequest.find(...)
end
...
end
That is to say, you need to use, #your_local_variable inside view file.
<% #your_local_variable.each do |o| %>
....
<% end %>
If the variable inside index action is #organisation_requests, use that.
I've been following the JumpStartLab Blogger 2 Project and I'm stuck on what seems to be something simple. I'm at this point Just scroll up a page where it says
#article = Article.find(params[:id])
Basically I've followed everything to a tee (I think) and after refreshing the browser after clicking the link for one of the articles I get the following error:
ActiveRecord::RecordNotFound in ArticlesController#show
Couldn't find Article with 'id'=1
My Articles_controller.rb looks like this
class ArticlesController < ApplicationController
def index
#articles = Article.all
end
def show
#article = Article.find(params[:id])
end
end
My index.html.erb looks like this
<h1>All Articles</h1>
<ul id="articles">
<% #articles.each do |article| %>
<li>
<%= link_to article.title, article_path(article), class: 'article_title', id: "article_#{article.id}" %>
</li>
<% end %>
</ul>
<%= link_to "Create a New Article", new_article_path, class: "new_article" %>
And my show.html.erb looks like this.
<h1><%= #article.title %></h1>
<p><%= #article.body %></p>
<%= link_to "<< Back to Articles List", articles_path %>
Pretty stuck here and can't figure it out. Also I noticed another problem. At first I forgot the
#article = Article.find(params[:id])
and I was plagued with this error which I assume will come back after I figure out the first issue.
NoMethodError in Articles#show show.html.erb where line #1 raised:
undefined method `title' for nil:NilClass
I've looked up similar questions but they all seem to be deeper into the program than I am and were talking about private methods which I don't think I'm dealing with yet. I posted both problems because I'm not sure if they are related or not. Any advice would help me out as this is really slowing me down. Thanks.
Based on the code you're showing, that shouldn't be able to happen, if the link is present on your index page, then your show method should be able to find it.
Maybe your index page is out of date, or using some cache that is no longer valid. Try reloading the page, or restarting your server (and if you do this, be sure to run spring stop so you get a full restart), I would also run rake tmp:clear to clear the cache, just in case.
I got the same error in a udemy course, i had not added a plural form of articles while redirecting the path. I had typed
article_path(article)
instead of
articles_path(article)
I hope this helps someone.
Unable to render category name in the index view, when passing
<%= post.category.name %>
to the
<% #posts.each do |post| %>
Error:
undefined method `name' for nil:NilClass
However, when passing
<%= post.category %>
I get
#<Category:0x007ff5c2c20b68>
Within individual Show actions
<%= #post.category.id %>
works perfectly. What can be the problem?
Thanks
I think that for atleast 1 post, the category is nil
You can avoid the error by making this change
<%= post.category.name if post.category %>
or
<%= post.category.try :name %>
Look at the SELECT commands that are being invoked in each case, you can see it in your terminal where you run rails s
I suspect that for some reason, in the index controller, the categories information is not being retrieved together with the posts
If you don't know what is wrong with the SELECT commands, post both cased here together with the controller and model
I have a model called "ExpDemo" and want to use it from "MainController."
I setup the code like this:
main_controller.rb
def pre
#demo = ExpDemo.new
end
main/pre.html.erb
<% form_for(#demo) do |f| %>
...
<% end %>
Until here, I experienced 'path' error.
undefined method `exp_demos_path'
So, I added following to routes.rb and the error message gone.
resources :exp_demos
Now, the form is not showing up in the HTML page.
I think the routing setup is the problem, but I'm not sure how to fix it.
Please help me to resolve this issue.
you forgot an = for the form_for
<%= form_for(#demo) do |f| %>
Solution:
Change <% form_for to <%= form_for
Explanation:
<% %> Executes ruby code that doesn't output results
<%= %> Executes
ruby code and outputs HTML
In other words, your form is created (which is why there is no error shown) but not printed as HTML.