ruby-on-rails tutorials: Can't show a user's microposts - ruby-on-rails

As I was reading "Ruby on Rails 3 Tutorial", I was trying to figure out what I did wrong in chapter 11 as I couldn't have the test in listing 11.15 to pass.
So here I am, stuck in section 11.2.1. Since I couldn't get anywhere with it, I decided to ignore this and proceed further: to fill up 50 microposts for the first six users and then try to access a user's show page. I got the following error page:
NoMethodError in Users#show
Showing <my_path>/sample_app/app/views/users/show.html.erb where line #10 raised:
undefined method `model_name' for NilClass:Class
Extracted source (around line #10):
7: </h1>
8: <% unless #user.microposts.empty? %>
9: <table class="microposts" summary="User microposts">
10: <%= render #microposts %>
11: </table>
12: <%= will_paginate #microposts %>
13: <% end %>
With trial and errors, it looks like the instance variable #microposts is nil for some reason. It looked as if the method "UsersController.show" didn't do its job of creating that instance variable. However, I could verify that this method is executed since I could manage to acces a user's show page successfully provided that he doesn't have any micropost.
At this step, it's hard to give more hints. But I can tell that all the other tests are passing.

try replacing all your occurrences of #microposts with #user.microposts.
Is #microposts defined in your controller action? Or is #user defined in your controller action? Because it seems like #user.microposts is probably the same thing as #microposts (from this context) and unless you've declared a variable for an associated user.microposts object, you might be trying to access a nonexistent variable.

could it be #user.microposts? The thing that on line 8 it checks for the existence of?

Well, with these types of errors, there's a stupid mistake creeping somewhere.
Following the advice of the ruby.railstutorial.org site. I did compare my code against the one of the author. Guess what, in my version of UsersController, there was two definitions of the 'show' method! And that second instance, at the bottom of the file, didn't have any assignment to the #microposts instance variable.
Would I had to compile the thing, the bug would have been spotted. I guess that there must be a way to scan a rails site against a ruby syntax checker.
Anyway, thanks for the other people suggestions.

Related

Missing partial comments/_comment in rails

I'm using a tutorial to get a feel for ruby, I am very much a beginner. Before posting this I have spent a couple of hours trying to resolve this myself with no luck. Sorry in advance if my explanation isn't great:
So I am getting this error screen:
error message
I am following a tutorial to make a basic reddit style app and I am trying to add the comments functionality.
When you render a collection like render #comments, Rails will check the type name of the items in #comments (i.e. 'comment') then look for a partial under app/views/comments/_comment.html.erb by default (note the plural singular distinction between the partial name and the folder/collection name).
The following steps should resolve your issue:
Create a comments partial under:
app/views/comments/_comment.html.erb
Now, when you call render #comments, each item of your collection is passed to the partial as a local variable as the same name without the underscore:
In _comment.html.erb
<%# comment is defined because it matches the name of the partial %>
<%= comment.<some_attribute_on_comment> %>

This may be very simple but how come this code <%= #user.name %> doesn't work on all pages?

I get the following error after putting it on certain pages in Ruby on Rails:
undefined method `name' for nil:NilClass
It also says there is no method error in the page I am on. I know this may be a beginner question but it has been stumping me big time in my project. If you can give me some advice I'd be very happy! Thank you in advanced!
Fist of all: It's a common trap. We all fall into it from time to time.
When ever you read undefined method foobar for nil:NilClass you know that something is nil what shouldn't be nil. So you have to check why #user is nil. Maybe a bug in the controller.
If you want to handle both situations (nil AND User) than you can use this code:
<%= #user.name if #user %>

My Rails app doesn't like my attributes

I am new to Rails and Ruby and actually it's my first web language. This isn't my first project but it's the bigest so far and I am having trouble with a certain page (the "home" page).
The error is:
NoMethodError in Home#index undefined method `avatar' for nil:NilClass
Extracted source (around line #1):
1: <%= image_tag current_user.avatar.url(:thumb) %>
2: <ul>
3: <li><b>Name:</b><%= current_user.name %></li>
4: <li><b>Username:</b><%= current_user.username %></li>
So it doesn't like my attributes :( What should I do in this situation? Where should I look first? Is this a controller problem?
The problem is that current_user is returning nil.
Take a look at the error
NoMethodError in Home#index undefined method `avatar' for nil:NilClass
Ruby is saying that you are trying to execute an instance method avatar on the nil object. After looking at your code, you should be able to tell that you are calling avatar on the return value of current_user so you can deduce that current_user is returning nil instead of the user.
Per the comments:
To solve this you will need to first determine if there is a current_user. Then and only then do you try to display a users avatar or the users other attributes (name, username ...)
Some people do not like logic in the views, I tend to follow this practice.
To avoid this, you could: create a view helper that contains the logic. This helper may or may not display/return data about a user.
Something along the lines of if current_user ...
You are trying to get the avatar for the 'current_user'. The system is complaining because it doesn't know who or what current_user is.
Your current_user object is nil. The code causing the problem (probably form your application or sessions helper) where you are setting the current_user...is most likely where you'll find the root of the problem.

RoutingError on Ruby Rails?

Hey everyone, thanks for reading this.
Ihave the next issue: When I call my "New" template (generated by scaffold) I got the next error:
<h1>ActionController::RoutingError in Flujos_de_trabajo#new</h1>
Showing app/views/flujos_de_trabajo/new.html.erb where line #3 raised:
flujos_de_trabajo_url failed to generate from {:controller=>"flujos_de_trabajo", :action=>"show"} - you may have ambiguous routes, or you may need to supply additional parameters for this route. content_url has the following required parameters: ["flujos_de_trabajo", :id] - are they all satisfied?
Extracted source (around line #3):
1: <h1>New flujo_de_trabajo</h1><br/>
2: <br/>
3: <% form_for(#flujo_de_trabajo) do |f| %><br/>
4: <%= f.error_messages %><br/>
5: <br/>
6: <p><br/>
I have overlooked everything, and I don't know what the problem is. The code in the view and in the controller is the same as the generated. In fact, i deleted it, generated it againg, and nothing, the same problem. Can you help me?
Did you do this.
script/generate scaffold FlujosDeTrabajo
rake db:migrate
http://localhost:3000/flujos_de_trabajos/new
it is working for me
Rails is really bad for languages that aren't English. It's failing here because it thinks that "flujo_de_trabajo" is the singular version of "flujo_de_trabajo". You're going to have to set up some inflections telling Rails the correct singular version of this. Look at the examples in config/initializers/inflectors.rb.

Rendering view of controller in another controller in Rails

The same question has been asked many times, but I am unable to find a solution that works for me.
I have three models, posts, users, and picture (paperclip).
I use restful_authentication and set it up so that each user sees only his/her post in posts/index. I've also given him the ability to upload a profile picture.
My question is how do I take the picture/index and show it in post/index. I want to show a thumbnail on top of the post/index page, followed by the posts.
I've used partials, but I get the error
NoMethodError in Posts#index
Showing app/views/us/_picture.html.erb where line #1 raised:
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each
Extracted source (around line #1):
1: <% for u in #us %>
2: <tr>
3: <td><%= image_tag u.picture.url(:thumb) %></td>
4: <td><%=h u.name %></td>
I see what it's trying to do, how do I solve it so that it just shows the uploaded picture.
Thanks for your help.
The error is saying that you havent assigned the #us instance variable to the template, which you normally do in the controller.
The template compilation step is having issues at the first iteration step (iterating over the #us array), it hasnt even gotten to the point of trying to run the picture accessing/rendering step, which might or might not have problems of its own.
In your controller grab a set of users (with pictures) and assign it to #us, some basic pseudo-code:
class PostsController < ApplicationController
def index
#us = User.find(:limit => 10, :include => [:picture])
end
end

Resources