我是Ruby的新手,我正在Nitrous开发自己的应用程序。
当我在new.html.erb上填写表单并点击“保存”时,show视图会加载。该页面按预期呈现HTML标题,但我的模型没有数据字段。我也尝试了索引视图,但我认为没有保存数据。
我已经生成了一个名为income的模型,并且运行了rake db:migrate,并且设置了正确的表。我对这个问题感到困惑。
感激地收到任何想法。
这是我的控制器: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
我在new.html.erb中的表单是:
<%= 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 %>
我的show.html.erb文件是:
<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>
我的模型income.rb是:
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 as HTML 参数:{&#34; utf8&#34; =&gt;&#34;✓&#34;,&#34; authenticity_token&#34; =&gt;&#34; D4F40TAMcVsb7RbdUZwxXfsgGn1u / iD7R + DWvAwrtJ1q83mgequQbkyKPaheFjsLSZHlMBpDy + H7Rm + HdVeTFw == &#34;,&#34;收入&#34; =&gt; {&#34; first_name&#34; =&gt;&#34; Barak&#34;,&#34; last_name&#34; =&gt;&# 34;奥巴马&#34;,&#34; date_of_birth&#34; =&gt;&#34; 02/07 / 1990&#34;,&#34; income1&#34; =&gt;&#34; 12345&#34; ,&#34; income2&#34; =&gt;&#34; 12345&#34;,&#34; income3&#34; =&gt;&#34;&#34;,&#34; income4&#34; = &gt;&#34;&#34;,&#34; income5&#34; =&gt;&#34;&#34;,&#34; income6&#34; =&gt;&#34;&#34; ,&#34; income7&#34; =&gt;&#34;&#34;,&#34; income8&#34; =&gt;&#34;&#34;,&#34; income9&#34; = &gt;&#34;&#34;,&#34; income10&#34; =&gt;&#34;&#34;},&#34;提交&#34; =&gt;&#34;保存并继续& #34;} [1m [35m(0.1ms)[0m开始交易 [1m [36mSQL(0.5ms)[0m [1mINSERT INTO&#34;收入&#34; (&#34; created_at&#34;,&#34; updated_at&#34;)VALUES(?,?)[0m [[&#34; created_at&#34;,&#34; 2015-02-19 17:39 :54.471034&#34;],[&#34; updated_at&#34;,&#34; 2015-02-19 17:39:54.471034&#34;]] [1m [35m(32.8ms)[0m提交事务 重定向到http://o-bot-laboratory-190030.euw1.nitrousbox.com/incomes/42
开始GET&#34; / incomes / 42&#34;对于88.215.13.57在2015-02-19 17:39:54 +0000 由IncomesController处理#显示为HTML 参数:{&#34; id&#34; =&gt;&#34; 42&#34;} [1m [36mIncome Load(0.3ms)[0m [1mSELECT&#34;收入&#34;。* FROM&#34;收入&#34;在哪里&#34;收入&#34;。&#34; id&#34; =?限制1 [0m [[&#34; id&#34;,42]] 在布局/应用程序中呈现收入/ show.html.erb(0.8ms) 在94ms完成200 OK(浏览次数:75.6ms | ActiveRecord:0.3ms)
按要求路线....
Rails.application.routes.draw do
resources :incomes
root 'incomes#new'
end
答案 0 :(得分:0)
感谢@RailsOuter和@jyrkim的帮助。我发现问题确实存在于我的模型文件和迁移文件中。
我需要从模型中删除attr_accessor行。我发现我必须修改我的迁移文件以添加列,而不是生成新的迁移。现在我已经使用数据列生成了新的迁移,我的数据正在显示!
再次感谢,非常感谢您的帮助,帮助新手!