当我进入浏览器时,会显示“名称”和“位置”的值,但不会显示为“开始”和“结束”(日期)输入的值。这是我的'show'html和控制器的代码。
show.html.erb:
class TripsController < ApplicationController
def show
@trip = Trip.find(params[:id])
end
def new
end
def create
#render plain: params[:trip].inspect
@trip = Trip.new(trip_params)
@trip.save
redirect_to @trip
end
private
def trip_params
params.require(:trip).permit(:name, :location, :start, :end)
end
end
trips_controller.rb:
class Trip < ActiveRecord::Base
end
trip.rb:
ActiveRecord::Schema.define(version: 20151016023421) do
create_table "trips", force: :cascade do |t|
t.string "location"
t.string "name"
t.date "start"
t.date "end"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
end
end
schema.rb:
2.2.3 :013 > Trip.first
Trip Load (0.5ms) SELECT "trips".* FROM "trips" ORDER BY "trips"."id" ASC LIMIT 1
=> #<Trip id: 1, location: "we", name: "a", start: nil, end: nil, created_at: "2015-10-21 03:17:45", updated_at: "2015-10-21 03:17:45", user_id: nil>
2.2.3 :014 > Trip.create(name: 'test', location: 'test location', start: '2015-10-21', end: '2015-10-27')
(0.1ms) begin transaction
SQL (0.5ms) INSERT INTO "trips" ("name", "location", "start", "end", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["name", "test"], ["location", "test location"], ["start", "2015-10-21"], ["end", "2015-10-27"], ["created_at", "2015-10-21 04:44:18.436966"], ["updated_at", "2015-10-21 04:44:18.436966"]]
(22.7ms) commit transaction
=> #<Trip id: 11, location: "test location", name: "test", start: "2015-10- 21", end: "2015-10-27", created_at: "2015-10-21 04:44:18", updated_at: "2015-10- 21 04:44:18", user_id: nil>
在我的rails控制台中:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>school</title>
<link rel="stylesheet" href=
"http://cdn.rawgit.com/jtblin/angular-chart.js/master/dist/angular-chart.css" type=
"text/css" />
<script src="http://cdn.rawgit.com/nnnick/Chart.js/master/Chart.min.js" type=
"text/javascript">
</script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js"
type="text/javascript">
</script>
<script src=
"http://cdn.rawgit.com/jtblin/angular-chart.js/master/dist/angular-chart.js"
type="text/javascript">
</script>
<script type="text/javascript" src="js/script.js"></script>
</head>
<body ng-app="app">
<div ng-controller="LineCtrl">
<canvas id="line" class="chart chart-line" chart-data="data" chart-labels="labels"
chart-legend="true" chart-series="series" chart-click="onClick"></canvas>
</div>
</body>
</html>
答案 0 :(得分:0)
我认为您的@trip.start
和@trip.end
是nil
,这就是您在浏览器中看不到的原因。尝试这样做以确认:
<p>
<strong>Start Date:</strong>
<%= @trip.start.inspect %>
</p>
<p>
<strong>End Date:</strong>
<%= @trip.end.inspect %>
</p>
查看它现在是否显示nil
个值。
如果存在该值,它将在浏览器中显示该值。