rails 上 ruby 未显示两个参数的值
value for two parameters not being shown ruby on rails
当我打开浏览器时,显示 'name' 和 'location' 的值,但不显示为 'start' 和 'end' (日期)输入的值.这是我的“表演”html 和控制器的代码。
show.html.erb:
<p>
<strong>Name:</strong>
<%= @trip.name %>
</p>
<p>
<strong>Location:</strong>
<%= @trip.location %>
</p>
<p>
<strong>Start Date:</strong>
<%= @trip.start %>
</p>
<p>
<strong>End Date:</strong>
<%= @trip.end %>
</p>
trips_controller.rb:
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
trip.rb:
class Trip < ActiveRecord::Base
end
schema.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
在我的 rails 控制台中:
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>
我认为你的 @trip.start
和 @trip.end
是 nil
,这就是你在浏览器中看不到它的原因。尝试这样做以确认:
<p>
<strong>Start Date:</strong>
<%= @trip.start.inspect %>
</p>
<p>
<strong>End Date:</strong>
<%= @trip.end.inspect %>
</p>
看看它现在是否显示 nil
s 的价值。
如果该值存在,它将在浏览器中显示该值。
当我打开浏览器时,显示 'name' 和 'location' 的值,但不显示为 'start' 和 'end' (日期)输入的值.这是我的“表演”html 和控制器的代码。
show.html.erb:
<p>
<strong>Name:</strong>
<%= @trip.name %>
</p>
<p>
<strong>Location:</strong>
<%= @trip.location %>
</p>
<p>
<strong>Start Date:</strong>
<%= @trip.start %>
</p>
<p>
<strong>End Date:</strong>
<%= @trip.end %>
</p>
trips_controller.rb:
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
trip.rb:
class Trip < ActiveRecord::Base
end
schema.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
在我的 rails 控制台中:
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>
我认为你的 @trip.start
和 @trip.end
是 nil
,这就是你在浏览器中看不到它的原因。尝试这样做以确认:
<p>
<strong>Start Date:</strong>
<%= @trip.start.inspect %>
</p>
<p>
<strong>End Date:</strong>
<%= @trip.end.inspect %>
</p>
看看它现在是否显示 nil
s 的价值。
如果该值存在,它将在浏览器中显示该值。