我正在尝试提交表单并收到此错误: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
。我哪里错了?
答案 0 :(得分: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
无误地工作