0

Hi I am a newbie in rails. I am trying to render the data from the DB and display. I have been almost succeeded but It was not displaying the data as shown in the image below.

Companies List
Name    Place           
                  **VIEW**(Click of this is working and navigating nothing is displaying)

ADD New Company

And Here is my controllers code:

class CompaniesController < ApplicationController

def index
    @companies = Company.all


   end


  def show
    @companies = Company.find(params[:id])

  end


  def new
    @companies = Company.new
end

  def create
    @companies = Company.new(params[:Company])
    if @companies.save

      flash[:success] = "Welcome to skillable"
      redirect_to root_url
    else
      render 'new'
    end
  end



end

Here is my view code index.html.erb

<h1>Companies List</h1>

<table>
  <tr>
    <th>Name</th>
    <th>Place</th>
    <th></th>
    <th></th>
    <th></th>
  </tr>

<% @companies.each do |companies| %>
  <tr>
    <td><%= companies.name %></td>
    <td><%= companies.place %></td>
    <td><%= link_to 'VIEW', companies %></td>
        <% end %>
</table>

<br />

<%= link_to 'ADD New Company', new_company_path %>

So can anyone tell me what is my problem. why it is not showing. please I am unable to figure out my problem.

4
  • In the @companies.each loop you are not closing the row. Before the <% end %> tag you should put a </tr>. Could this be it? Commented Dec 29, 2013 at 14:17
  • No it is not the problem. Commented Dec 29, 2013 at 14:39
  • Are there any values in @companies? Commented Dec 29, 2013 at 15:00
  • Ya it getting the values from the DB. Commented Dec 29, 2013 at 17:20

2 Answers 2

1

First of all, in index page I would write:

<% @companies.each do |company| %>
  <tr>
    <td><%= company.name %></td>
    <td><%= company.place %></td>
    <td><%= link_to 'VIEW', company %></td>
  </tr>
<% end %>

this will show a list of companies. Next, a click on VIEW would take you for ex to: /companies/1 where 1 is the id of a company. That means you'll find company by id in show action of companies controller:

def show
  @company = Company.find(params[:id])
end

see how I call variable - @company, not @companies cause I assume in show.html.erb you have used @company variable (or if it's created by scaffold it's for sure @company), also this is the page for one company only and it would be more appropriate to call it with singular (company not companies).

Doing all of the above you will use in show page for ex:

<%= @company.name %>

P.S. 1 You have the same naming issue for new and create actions, I would do it like this:

 def new
   @company = Company.new
 end

this will let me use this variable in a form like:

<%= form_for @company do |f| %>
  ....
<% end %>

and:

def create
  @company = Company.new(params[:company])
  if @company.save
    flash[:success] = "Welcome to skillable"
    redirect_to root_url
  else
    render 'new'
  end
end

P.S. 2

to check if you have something in db - open rails console:

your/project/path/ rails c

x.x.x-pxxx :001 > Company.first
  Company Load (18.2ms)  SELECT "companies".* FROM "companies" LIMIT 1
 => nil 

if result will be nil, means you have nothing in db, else - you have records inside.

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

Comments

0

Use Company.all.to_a in your controller index method.

Possible duplicate of this question: rails 4 equivalent of rails 3 model all

3 Comments

ya it is still the same
In your question it isn't clear where it is not displaying. In the index or in the show method? Please be more precise. If it's in the show method then your path variable in the view link is wrong. It should be something like company_path(id).
Are there values stored in the db table companies? Use pgphpadmin or sqlite manager to see your database table entries. If there are none, then nothing can be displayed.

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.