Note that there are some explanatory texts on larger screens.

plurals
  1. POI'm having some trouble with view in my Ruby on Rails project
    primarykey
    data
    text
    <p>I'm trying to create a ruby on rails for the network in my building. I want to set it up with switches having many ports and each port has a name, jack, and room.</p> <p>I'm getting the following error when trying to view a switch:</p> <pre><code>undefined method `port' for #&lt;Switch:0x2b49d7643c90&gt; </code></pre> <p>Extracted source (around line #2):</p> <pre><code>1: &lt;h1&gt;&lt;%= @switch.title %&gt;&lt;/h1&gt; 2: &lt;p&gt;&lt;strong&gt;Switch :&lt;/strong&gt; &lt;%= @switch.port.name %&gt;&lt;br /&gt; 3: &lt;/p&gt; 4: &lt;p&gt;&lt;%= @switch.description %&gt;&lt;/p&gt; 5: &lt;hr /&gt; </code></pre> <p>Here is my controller method:</p> <pre><code>class SwitchController &lt; ApplicationController def list @switches = Switch.find(:all) end def show @switch = Switch.find(params[:id]) end def new @switch = Switch.new end def create @switch = Switch.new(params[:switch]) if @switch.save redirect_to :action =&gt; 'list' else @ports = Port.find(:all) render :action =&gt; 'new' end end def edit @switch = Switch.find(params[:id]) @ports = Port.find(:all) end def update @switch = Switch.find(params[:id]) if @switch.update_attributes(params[:switch]) redirect_to :action =&gt; 'show', :id =&gt; @switch else @ports = Port.find(:all) render :action =&gt; 'edit' end end def delete Switch.find(params[:id]).destroy redirect_to :action =&gt; 'list' end def show_ports @port = Port.find(params[:id]) end </code></pre> <p>end</p> <p>Here are my models:</p> <pre><code>class Switch &lt; ActiveRecord::Base has_many :ports validates_uniqueness_of :title end class Port &lt; ActiveRecord::Base belongs_to :switch validates_presence_of :name validates_presence_of :jack validates_presence_of :room end </code></pre> <p>And here are my migrations:</p> <pre><code>class Switches &lt; ActiveRecord::Migration def self.up create_table :switches do |t| t.string :title t.text :description end end def self.down drop_table :switches end end class Ports &lt; ActiveRecord::Migration def self.up create_table :ports do |t| t.string :name t.string :jack t.string :room end Port.create :name =&gt; "1/0/1" end def self.down drop_table :ports end end </code></pre> <p>And finally, here is my show.html.erb</p> <pre><code>&lt;h1&gt;&lt;%= @switch.title %&gt;&lt;/h1&gt; &lt;p&gt;&lt;strong&gt;Switch :&lt;/strong&gt; &lt;%= @switch.port.name %&gt;&lt;br /&gt; &lt;/p&gt; &lt;p&gt;&lt;%= @switch.description %&gt;&lt;/p&gt; &lt;hr /&gt; &lt;%= link_to 'Back', {:action =&gt; 'list'} %&gt; </code></pre> <p>I know I'm missing some crucial code somewhere, thanks in advance for any help!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload