leevigilstroy
leevigilstroy

Reputation: 604

Ruby Index and Show views are showing no data

I'm new to Ruby, and I'm developing my own app in Nitrous.

When I complete a form on my new.html.erb, and hit save, the show view loads. The page renders with HTML headers as expected, but no data fields from my model. I have also tried the index view, but there is no data being saved, I think.

I have generated a model called income, and run rake db:migrate successfully, wiht the right tables set up. I'm stumped as to what the problem is.

Any ideas gratefully received.

This is my controller: incomes_controller.rb

class IncomesController < ApplicationController 

  def index
  @incomes = Income.all
end

def new
  @income = Income.new
end

def create
 @income = Income.new(income_params)
 if @income.save
  redirect_to(:action => 'show', :id => @income.id)
 end
end

def show
 @income = Income.find(params[:id])
end


private

def income_params
 params.require(:income).permit(:first_name, :last_name, :date_of_birth, :income1, :income2, :income3, :income4, :income5, :income6, :income7, :income8, :income9, :income10)
 end
end

My form in new.html.erb, is:

<%= simple_form_for @income do |f| %>  
    <%= f.input :first_name, label: 'First Name' %> 
    <%= f.input :last_name, label: 'Last Name' %> 
    <%= f.input :date_of_birth, label: 'Date of Birth', hint: 'dd/mm/yyyy' %> 

<p>Income: Salary or Wages</p>
<%= f.input  :income1, label: 'Take home salary or Wage' %>
<%= f.input  :income2, label: 'Partner\'s salary or wage' %>
<%= f.input  :income3, label: 'Other income from salary or wages' %>

<p>Other Income</p>
<%= f.input  :income4, label: 'Maintanence or child support' %>
<%= f.input  :income5, label: 'Boarders or lodgers' %>
<%= f.input  :income6, label: 'Non-dependent contributions' %>
<%= f.input  :income7, label: 'Student loands or grants' %>
<%= f.input  :income7, label: 'Other' %>

<p>Benefits </p>
<%= f.input  :income8, label: 'Sample input' %>
<%= f.input  :income9, label: 'Sample input' %>
<%= f.input  :income10, label: 'More sample inputs' %>

<%= f.button :submit, "Save and Proceed" %>
<h3>You will have a chance to review your form before you submit it.  Once you click Save, you can come back and complete the rest of the form at a later date</h3>

<% end %>

And my show.html.erb file is:

<p>
  <strong>First name:</strong>
  <%= @income.first_name %>
</p>

<p>
  <strong>Last name:</strong>  
  <%= @income.last_name %>
</p>

<p>
  <strong>Date of Birth:</strong>  
  <%= @income.date_of_birth %>
</p>

<p>
  <strong>Take home salary or Wage:</strong>  
  <%= @income.income1 %>
</p>

My model income.rb is:

class Income < ActiveRecord::Base
attr_accessor :first_name, :last_name, :date_of_birth, :income1, :income2, :income3, :income4, :income5, :income6, :income7, :income8, :income9, :income10 
end

Development log output....

Processing by IncomesController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"D4F40TAMcVsb7RbdUZwxXfsgGn1u/iD7R+DWvAwrtJ1q83mgequQbkyKPaheFjsLSZHlMBpDy+H7Rm+HdVeTFw==", "income"=>{"first_name"=>"Barak", "last_name"=>"Obama", "date_of_birth"=>"02/07/1990", "income1"=>"12345", "income2"=>"12345", "income3"=>"", "income4"=>"", "income5"=>"", "income6"=>"", "income7"=>"", "income8"=>"", "income9"=>"", "income10"=>""}, "commit"=>"Save and Proceed"} [1m[35m (0.1ms)[0m begin transaction [1m[36mSQL (0.5ms)[0m [1mINSERT INTO "incomes" ("created_at", "updated_at") VALUES (?, ?)[0m [["created_at", "2015-02-19 17:39:54.471034"], ["updated_at", "2015-02-19 17:39:54.471034"]] [1m[35m (32.8ms)[0m commit transaction Redirected to http://o-bot-laboratory-190030.euw1.nitrousbox.com/incomes/42

Started GET "/incomes/42" for 88.215.13.57 at 2015-02-19 17:39:54 +0000 Processing by IncomesController#show as HTML Parameters: {"id"=>"42"} [1m[36mIncome Load (0.3ms)[0m [1mSELECT "incomes".* FROM "incomes" WHERE "incomes"."id" = ? LIMIT 1[0m [["id", 42]] Rendered incomes/show.html.erb within layouts/application (0.8ms) Completed 200 OK in 94ms (Views: 75.6ms | ActiveRecord: 0.3ms)

Routes as requested....

Rails.application.routes.draw do
 resources :incomes
 root 'incomes#new' 
end

Upvotes: 1

Views: 93

Answers (1)

leevigilstroy
leevigilstroy

Reputation: 604

Thanks to @RailsOuter and @jyrkim for your help. I found that the problem was indeed with my model file, and migration file.

I needed to remove the line attr_accessor from my model. I have since discovered that I must have modified my migration file to add columns, instead of generating a new migration. Now I have generated the new migration with the data columns, my data is showing!

Thanks again, very appreciative of your input to help out a newbie!

Upvotes: 0

Related Questions