0

我正在尝试提交表单并收到此错误:Couldn't find Event without an ID

这是控制器:

def create
@event = Event.find(params[:event_id])
@event_sponsorship = EventSponsorship.new(params[:event_sponsorship])
@event_sponsorship.sponsor_id = current_user.id
@event_sponsorship.event_id = @event

respond_to do |format|
  if @event_sponsorship.save
    format.html { redirect_to @event_sponsorship, notice: 'Event sponsorship was successfully created.' }
    format.json { render json: @event_sponsorship, status: :created, location: @event_sponsorship }
  else
    format.html { render action: "new" }
    format.json { render json: @event_sponsorship.errors, status: :unprocessable_entity }
  end
end
end

这是表格:

<%= simple_form_for(@event_sponsorship) do |f| %>
<%= f.error_notification %>

<div class="signin">
<%= f.hidden_field :event_id, value: @event %>

 <%= f.hidden_field :sponsor_id, value: current_user.id %>

<%= f.button :submit, :class => "btn btn-success btn-block", value: "Yes" %>
</div>
<% end %>

create方法中event_id应该从 URL 中找到。我哪里错了?

4

1 回答 1

0

发布您在服务器控制台中看到的错误,您会看到您的参数“event_id”是(我猜)“event_sponsorship”的子键

试试这个:

@event = Event.find(params[:event_sponsorship][:event_id])

您甚至可能需要使用以下代码:

@event = Event.find(params[:event_sponsorship].delete(:event_id))

这将删除参数中的“event_id”,并允许EventSponsorship.new正常工作

于 2013-03-20T23:08:41.367 回答