Why do I get an undefined method error in Ruby on Rails app?

KDP

Thanks for any help you can offer. I'm trying to figure out why I'm getting the undefined method error described below. I'm new to Ruby on Rails, but I thought that by creating the @waypoint, I could call methods for it (like @waypoint.waypointaddress). My models use a belongs_to and has many (Newsavedmaps can have many waypoints.) I appreciate your feedback on what I'm doing wrong.

Error Refers to if [email protected]?

    NoMethodError (undefined method `waypointaddress' for #<Array:NUMBERSHERE>):
    app/controllers/maptry_controller.rb:33:in `index'

maptry controller

    @newsavedmap = Newsavedmap.find(:first, :conditions => {:id => params[:newsavedmap_id]})
    @waypoint = Waypoint.find(:all, :conditions => {:newsavedmap_id => params[:newsavedmap_id]})
    @newsavedmap.id = params[:newsavedmap_id]
    @newsavedmap.name = Newsavedmap.find(:first, :conditions => {:id => params[:newsavedmap_id]}).name
    @newsavedmap.optimize = Newsavedmap.find(:first, :conditions => {:id => params[:newsavedmap_id]}).optimize

    if [email protected]_masterlocation_id.nil?       
    @start_i_name = Masterlocation.find(:first, :conditions => {:id => @newsavedmap.start_masterlocation_id}).i_name
    end
    if [email protected]?  
    @waypoint_i_name = Masterlocation.find(:first, :conditions => {:id => @waypoint.waypoint_masterlocation_id}).i_name
    end
    if [email protected]_masterlocation_id.nil?     
    @end_i_name = Masterlocation.find(:first, :conditions => {:id => @newsavedmap.end_masterlocation_id}).i_name
    end

    else
        @newsavedmap = Newsavedmap.new  
    end

EDIT 1

Here is my updated controller using code from Mu's comment. However, I'm getting an error of "undefined method `each'" for the maptry.html.erb view included below.

To clarify, there may be one or more waypoints that match the conditions. I'd like to return all of them and then do a for each in my maptry view.

Also, the .where(...) threw an undefined method error for where, so I used find. Maybe that's related to the problem? I'm in Rails 2.X.

Code from maptry.html.erb

    <% if params[:newsavedmap_id] %>
            <% for waypoint in @waypoint %>
          <option selected value="<%= @waypoint.waypointaddress %>"><%= @waypoint_inst_name %></option>
            <% end %>
          <% end %>

updated maptry controller

    if params[:newsavedmap_id]
    @newsavedmap = Newsavedmap.find(:first, :conditions => {:id => params[:newsavedmap_id]})
    @waypoint = Waypoint.find(:first, :conditions => {:newsavedmap_id => params[:newsavedmap_id]})
    @waypoint.waypointaddress = Waypoint.find(:first, :conditions => {:newsavedmap_id => params[:newsavedmap_id]}).waypointaddress
    @waypoint.waypoint_masterlocation_id = Waypoint.find(:first, :conditions => {:newsavedmap_id => params[:newsavedmap_id]}).waypoint_masterlocation_id
    @waypoint_inst_name = Masterlocation.find(:first, :conditions => {:id => @waypoint.waypoint_masterlocation_id}).inst_name
    @newsavedmap.id = params[:newsavedmap_id]
    @newsavedmap.name = Newsavedmap.find(:first, :conditions => {:id => params[:newsavedmap_id]}).name
    @newsavedmap.optimize = Newsavedmap.find(:first, :conditions => {:id => params[:newsavedmap_id]}).optimize

    if [email protected]_masterlocation_id.nil?       
    @start_inst_name = Masterlocation.find(:first, :conditions => {:id => @newsavedmap.start_masterlocation_id}).inst_name
    end
    if [email protected]_masterlocation_id.nil?     
    @end_inst_name = Masterlocation.find(:first, :conditions => {:id => @newsavedmap.end_masterlocation_id}).inst_name
    end

    else
    @newsavedmap = Newsavedmap.new  
    end
mu is too short

You're asking ActiveRecord to find all the waypoints that match your conditions:

@waypoint = Waypoint.find(:all, :conditions => {:newsavedmap_id => params[:newsavedmap_id]})
#-------------------------^^^^

That means that you'll get an Array in @waypoint and an Array (usually) won't have a waypointaddress method. Perhaps you meant to ask for the first one:

@waypoint = Waypoint.find(:first, :conditions => {:newsavedmap_id => params[:newsavedmap_id]})

or better:

@waypoint = Waypoint.where(:newsavedmap_id => params[:newsavedmap_id]).first

In general, you shouldn't be using find like that anymore; if you find yourself entering some :conditions in a find call, you should be saying .where(...) instead.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why do I get the error 'undefined method `each' for nil:NilClass' in Ruby on Rails?

Why do I get this "undefined method" error in a Rails model callback, passing the method as a symbol?

Why do I get the error "undefined method 'merge' for true:TrueCLass" when adding a checkbox in Rails

Why do I get this sqli3 error in Ruby on Rails?

Why do I get undefined method `mktmpdir' for Dir:Class in irb using Ruby 2.6.3?

Why do I get a 'constructor is undefined` error?

Why do I get property of undefined error?

Why do I get an undefined local variable or method error when using a constant, but not when using a method?

Why I get an Error when trying deploy Ruby on Rails app to Heroku?

Ruby on Rails - Seed undefined method error

Receiving undefined method error in ruby (no rails)

Ruby on Rails Error: undefined local variable or method

Why do I keep getting an undefined method error?

Why I get that error Call to undefined method Facebook\Facebook::api()?

Why do I get undefined method `default_scoped?'?

Why do I get "undefined local variable or method" in my code?

Why do I get an undefined constant error in Laravel?

Why do i get the - TypeError: undefined is not a function error?

Why do I get an error undefined reference to `getche'?

Why do I get an Undefined reference error when trying to compile?

Ruby on Rails, method undefined

Getting undefined method error in ruby on rails while the method is defined

Why do i get an an error on paint method when the parameters are correct?

Why do I get a compile error when calling start method?

Flutter: Why do I get an error when using the setState() method?

"undefined method" error ruby

Why do I get an error if the App is wrapped in Provider?

Why I get undefined method `each'?

Why do I get a "405 - Method Not Allowed" error when trying to open a .css file in my Spring Boot/Spring Security web app?