0

I generated the app using scaffolds as such:.

    rails generate scaffold server hostname:string
    rails generate scaffold template remote_template_id:integer remote_template_name:string server:belongs_to

In my rails controller, for the show method, I want to show all templates owned by the server.

When I run the show action I get the following error:

undefined method `remote_template_id' for #<Array:0x007f6b44dc3370>

Extracted source (around line #5):

2: 
3: <p>
4:   <b>Remote template:</b>
5:   <%= @template.remote_template_id %>
6: </p>
7: 

Here is the code in my template controller:

  def show

    server=Server.find(params[:server_id])
    @template=server.templates.all


    logger.info "TEMPLATE OBJECT: " + @template.inspect


    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @template }
    end
  end

As you can see above, I have some debugging output using the logger, and in the rails server console, the object does get correctly displayed. The object contains the correct data that I am trying to pass into the view.

  Server Load (0.2ms)  SELECT "servers".* FROM "servers" WHERE "servers"."id" = ? LIMIT 1  [["id", "1"]]
  Template Load (0.2ms)  SELECT "templates".* FROM "templates" WHERE "templates"."server_id" = 1
TEMPLATE OBJECT: [#<Template id: 1, remote_template_id: 12, remote_template_name: "another_debian_derivative", server_id: 1, created_at: nil, updated_at: nil>]
  Rendered templates/show.html.erb within layouts/application (0.9ms)
Completed 500 Internal Server Error in 6ms



    ActionView::Template::Error (undefined method `remote_template_id' for #<Array:0x007f6b44dc3370>):
        2: 
        3: <p>
        4:   <b>Remote template:</b>
        5:   <%= @template.remote_template_id %>
        6: </p>
        7: 
        8: <p>
      app/views/templates/show.html.erb:5:in `_app_views_templates_show_html_erb___2503859911707707300_70049345896300'
      app/controllers/templates_controller.rb:24:in `show'

The database is kosher, relationships are visible; I see server_id is populated into template table.

sqlite> .tables
schema_migrations  servers            templates        
sqlite> 
sqlite> .schema servers
CREATE TABLE "servers" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "hostname" varchar(255), "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL);
sqlite> select * from servers;
1|opennebula1|2014-01-28 11:03:02.393136|2014-01-28 11:03:02.393136
sqlite> 
sqlite> 
sqlite> 
sqlite> .schema templates
CREATE TABLE "templates" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "remote_template_id" integer, "remote_template_name" varchar(255), "server_id" integer, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL);
CREATE INDEX "index_templates_on_server_id" ON "templates" ("server_id");
sqlite> select * from templates;
1|12|another_debian_derivative|1||
sqlite> 

and finally, here is the view code that is erroring, I didnt change or add anything in it, it was generated by scaffold:

/app/views/templates$ cat show.html.erb 
<p id="notice"><%= notice %></p>

<p>
  <b>Remote template:</b>
  <%= @template.remote_template_id %>
</p>

<p>
  <b>Remote template name:</b>
  <%= @template.remote_template_name %>
</p>

<p>
  <b>Server:</b>
  <%= @template.server %>
</p>


<%= link_to 'Edit', edit_template_path(@template) %> |
<%= link_to 'Back', templates_path %>

Help in this matter would be much appreciated. Thank you.

4
  • 1
    @template=server.templates.all will return an array of the template objects. While in the show.html.erb you are trying to call remote_template_id on this array instead of template object. Commented Jan 28, 2014 at 12:05
  • Thanks Manoj Monga, that is correct. I assumed wrong in thinking that rails would have this automated. Commented Jan 28, 2014 at 15:29
  • Did it work for you then? Commented Jan 28, 2014 at 15:32
  • Yes Manoj, that was indeed the solution. Commented Jan 28, 2014 at 23:37

1 Answer 1

1

@template=server.templates.all will return an array of the template objects. While in the show.html.erb you are trying to call remote_template_id on this array instead of template object

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

Comments

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.