简体   繁体   中英

Rails form params not being picked up

I'm doing a small project to get familiar with Rails. I have the following code:

Model event.rb

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

View 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 app/controllers/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

Routes

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

However each time I submit, the params is coming up empty: 空的参数

I didn't use resource :events and instead doing it manually to practice. Why isn't params being picked up?

试试这个:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM