gpt4 book ai didi

ruby-on-rails - 两个参数的值未显示 ruby​​ on rails

转载 作者:太空宇宙 更新时间:2023-11-03 16:45:45 25 4
gpt4 key购买 nike

当我转到浏览器时,会显示“名称”和“位置”的值,但不会显示为“开始”和“结束”(日期)输入的值。这是我的“显示”html 和 Controller 的代码。

显示.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

旅行.rb:

class Trip < ActiveRecord::Base
end

架构.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.endnil,这就是您在浏览器中看不到它的原因.尝试这样做以确认:

<p>
<strong>Start Date:</strong>
<%= @trip.start.inspect %>
</p>

<p>
<strong>End Date:</strong>
<%= @trip.end.inspect %>
</p>

看看它现在是否为它们的值显示 nil

如果该值存在,它将在浏览器中显示该值。

关于ruby-on-rails - 两个参数的值未显示 ruby​​ on rails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33250468/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com