我正在做一个小项目来熟悉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"
我没有使用资源:事件,而是手动执行。为什么不把帕拉姆捡起来?
答案 0 :(得分:3)
试试这个:
params.require(:event).permit(:title, :date)