Ruby Index和Show视图未显示任何数据

Ruby Index and Show views are showing no data

我是Ruby的新手,并且正在Nitrous中开发自己的应用程序。

当我在new.html.erb上填写表单并单击保存时,将加载显示视图。该页面将按预期方式显示HTML标头,但模型中没有数据字段。我也尝试过索引视图,但是我认为没有数据被保存。

我已经建立了一个名为收入的模型,并使用正确的表成功运行了rake db:migrate。我对问题是什么感到困惑。

感谢任何想法。

这是我的控制者:incomes_controller.rb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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

我在new.html.erb中的表单是:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<%= 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><center>[wp_ad_camp_2]</center></p><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" %>
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

<% end %>

我的show.html.erb文件是:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<p>
  First name:
  <%= @income.first_name %>
</p>

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

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

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

我的模型income.rb是:

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

开发日志输出。...

由IncomesController#create处理为HTML
参数:{" utf8" =>" a?""," authenticity_token" =>" D4F40TAMcVsb7RbdUZwxXfsgGn1u / iD7R DWvAwrtJ1q83mgequQbkyKPaheFjsLSZHlMBpDy H7Rm HdVeinc =" =""奥巴马","出生日期" =>" 1990年7月2日","收入1" =>" 12345","收入2" =>" 12345","收入3" =>","收入4" =>"," Income5" =>"," income6" =>"," income7" =>"," income8" =>"," income9" =>"," income10" =>"}," commit" =>"保存并继续"}
[1m [35m(0.1ms)[0m开始交易
[1m [36mSQL(0.5ms)[0m [1mINSERT INTO" incomes"(" created_at"," updated_at")值(?,?)[0m [["" created_at"," 2015-02-19 17:39:54.471034"],[" updated_at"," 2015-02-19 17:39:54.471034"]
[1m [35m(32.8ms)[0m提交事务
重定向到http://o-bot-laboratory-190030.euw1.nitrousbox.com/incomes/42

从2015-02-19 17:39:54 0000开始为88.215.13.57获取``/ incomes / 42''
由IncomesController#show处理为HTML
参数:{" id" =>" 42"}
[1m [36m收入负载(0.3ms)[0m [1mSELECT"收入"。*在"收入"中,"收入"。" id" =? LIMIT 1 [0m [[" id",42]]
布局/应用程序中的呈现收入/show.html.erb(0.8毫秒)
在94毫秒内完成200 OK(查看:75.6ms | ActiveRecord:0.3毫秒)

按要求的路线。...

1
2
3
4
Rails.application.routes.draw do
 resources :incomes
 root 'incomes#new'
end


感谢@RailsOuter和@jyrkim的帮助。我发现问题确实出在我的模型文件和迁移文件上。

我需要从模型中删除attr_accessor行。此后,我发现必须修改迁移文件才能添加列,而不是生成新的迁移。现在,我已经使用data列生成了新的迁移,我的数据正在显示!

再次感谢您非常感谢您提供的帮助新手的信息!