gpt4 book ai didi

ruby-on-rails - Rails 表单参数没有被拾取

转载 作者:行者123 更新时间:2023-12-04 05:56:36 29 4
gpt4 key购买 nike

我正在做一个小项目来熟悉 Rails。我有以下代码:

模型事件.rb

class Event < ApplicationRecord
validates :title, presence: true
validates :date, presence: true
has_and_belongs_to_many :users
end

查看 app/views/events/new.html.erb
<h1> Create event </h1>
<p> Create a new event here: </p>
<%= form_for @event do |e| %>
<%= e.label :title %>
<%= e.text_field :title %> <br />
<%= e.label :date %>
<%= e.text_field :date %> <br />
<%= e.submit %>
<% end %>

Controller
应用程序/ Controller /events_controller.rb
def create
@event = Event.new(event_params)
if @event.save
render html: "Event saved!"
else
render html: "Didn't work :("
end
end

private
def event_params
params.permit(:title, :date)
end

路线
  get "events"      => "events#index"
get "events/new" => "events#new"
post "events" => "events#create"

但是,每次我提交时,参数都为空:
Empty params

我没有使用资源 :events 而是手动进行练习。为什么没有获取 params ?

最佳答案

尝试这个:

params.require(:event).permit(:title, :date)

关于ruby-on-rails - Rails 表单参数没有被拾取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48602511/

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