5

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.

2
  • Well, you certainly don't want <%= @stars.each %>; is that a typo? Also, "#{tempstr}" when tempstr is already a string is a little redundant. If you log or pry in the controller does @stars really contain values? Commented Apr 10, 2012 at 22:53
  • @Dave Newton - For one, thanks for catching tempstr; I had originally meant to do the .where with rails syntax, which would have needed the evaluation. For 2, the = in there was a typo, and I'll fix that in the question, thanks again. For 3, using the logger.debug to output @stars.each do |star| star.ra end (where star.ra is one of the star variables) seems to have logged the entire contents of @stars, which include all the variables of 2 stars that the .where is supposed to find. I think that means it does contain values, though it could be getting it from somewhere else, I suppose? Commented Apr 10, 2012 at 23:45

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, you were absolutely correct! I had just figured it out myself not long before you posted, but thanks very much for answering and confirming! I had originally thought the Show page automatically used the Create method when initializing from there, but I was wrong. Such a simple mistake made me spend hours cooking my brain.
you answer helped me solve totally not related rails issue. you deserve uptove :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.