我正在尝试学习使用rails。我在关注http://guides.rubyonrails.org/getting_started.html 我添加了相关的模型。但是当我创建新事件时..它也会复制它。
class EventsController < ApplicationController
def create
@category = Category.find(params[:category_id])
@event = @category.events.create(event_params)
redirect_to category_path(@category)
end
def destroy
@category = Category.find(params[:category_id])
@event = @category.events.find(params[:id])
@event.destroy
redirect_to category_path(@category)
end
private
def event_params
params.require(:event).permit(:event, :genus, :description)
end
结尾
这是模型:
class Event < ActiveRecord::Base
belongs_to :category
end
这是我用来在特定类别中保存事件的表格:
<%= form_for ([@category, @category.events.build]) do |f| %>
<p>
<%= f.label :name %><br>
<%= f.text_field :event %>
</p>
<p>
<%= f.label :genus %><br>
<%= f.text_field :genus %>
</p>
<p>
<%= f.label :description %><br>
<%= f.text_area :description %>
</p>
<p>
<%= f.submit %>
</p>
这是显示视图代码:
<p>
<strong>Title:</strong>
<%= @category.name %>
</p>
<p>
<strong>Text:</strong>
<%= @category.key %>
</p>
<h2>Events</h2>
<%= render @category.events %>
<h2>
Add Event
</h2>
<%= render 'events/form' %>
<%= link_to 'Back', categories_path %>
事情是当我创建记录时..它会创建重复条目并删除记录它会删除这两个条目。
我无法弄清楚我的代码有什么问题。我只是按照指南..