Rendering view of controller in another controller in Rails - ruby-on-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

Related

NoMethodError in Articles#show ( undefined method `each' for nil:NilClass ) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
error
Am trying to render "latest articles" on my article blog sidebar and am getting the error above, the same code works fine for the article index page but shown errors in the show page.
latest articles partial
and my article controller below.
articles_controller
undefined method 'each' for nil:NilClass means that there is no method each that works with an object of class Nil. It works for the #index method because you are passing the variable #articles there. But in #show, you are passing a variable called #article. So you #articles variable in the show method does not exist therefore it has a default value nil. Your code in the index iterates over all articles, that is what each is doing. But in #show you want to show only one article so you should skip the each block, and use an article like #article.title
Found the solution, the code below worked for me.
<% Article.all.order("created_at Desc").limit(5).each do | article | %>
<%= article.title %>
<% end %>
Note: I ordered and limit the article to 5 at the same time and it's in my VIEW.

Ruby on rails- how to put a user id in a status update treehouse challenge

I am making a social network on the course that treehouse.com provide. It uses ruby on rails.
I am stuck at the minute because when I click post status I get this message. I'm trying to replace just any name with the user id. I have added all the correct things treehouse tell you to do but I get this error:
NoMethodError in Statuses#show
Showing /Users/lewisfrost/frostbook/app/views/statuses/show.html.erb where line #5 raised:
undefined method `first_name' for 13:Fixnum
Extracted source (around line #5):
5: <%= #status.user_id.first_name %>
Can someone help?
Thanks
#status.user_id is just a number. User #status.user instead.
<%= #status.user.first_name if #status.user %>
In your Model Statuses you have write Statuses - User Relation?
LIke
class Statuses
belongs_to :user
....
end
and now you can type
#status.user.first_name

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.

Extra instance variable in controller is nil in view?

I have been searching through Stack Overflow for a few hours now, but none of the related questions seem to apply to my issue.
I am new to Rails, with this being my first real project, and I may be confusing the MVC setup a little. I am attempting to assign the #stars instance variable while in an action of the searches_controller.rb:
def create
#search = Search.new(params[:search])
tempstr = searchstr(#search)
#stars = Star.where("tempstr", :limit => 100)
end
#search is created fine, being a complex search with varying parameters. tempstr is just a simple string container for the results of searchstr(#search), which is a quick method for converting the search parameters into a MySql-relevant string (which seems to be easier than trying to use the .where helper normally, in this case). I'm sure I can just put searchstr(#search) directly into the .where, but I split them up for now so I can inspect the elements as they pass through.
Anyways, the issue comes up when I try to call #stars in the show.html.erb view. Even with something as simple as this:
<% #stars.each do |star| %>
<%= display stuff %>
<% end %>
I get an error saying 'each' is not a method of nil:NilClass. So, I changed it to the following to see if #stars was nil:
<%= #stars.inspect %>
Sure enough, #stars is nil. However, when I add this line to my controller to check #stars there:
return render #stars.each
I see that the variable is filled with the correct star objects from the Star.where(), just as I had intended. A quick .inspect shows the variable is not nil, when in the controller.
So, I am unsure why the view is receiving it as nil if it has been defined in the controller just fine. I wouldn't be surprised if it was me misunderstanding how MVC works, though. The Star class was defined in the Star model, but maybe it is because I am trying to access it from the Searches controller, and thus it isn't initialized for the view?
Should I be going about doing this some other way? I attempted to use a local variable (using stars instead of #stars), but then the view says "Undefined local variable or method 'stars'".
Any help would be much appreciated, I have already wracked my brain for hours creating the complex search and parsing the star file data into the database, so I'm a bit burnt out. I can supply more information if requested, I'm not sure what else would be helpful in providing an answer.
You are setting #stars in the create method, but the view you are talking about is show.html.erb. Try setting #stars in the show method too. Something like this:
def show
#search = Search.find(params[:id])
tempstr = searchstr(#search)
#stars = Star.where("tempstr", :limit => 100)
end
If this does not help you, please show the rest of you controller actions, so we can help you better.

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

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.

Resources