Rails形成没有被拾取的参数

时间:2018-02-03 22:01:37

标签: ruby-on-rails forms

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

模型event.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 %>

控制器 应用程序/控制器/ 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

我没有使用资源:事件,而是手动执行。为什么不把帕拉姆捡起来?

1 个答案:

答案 0 :(得分:3)

试试这个:

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