Couldn't find Submission without an ID
这个问题困扰了我最近几天,我似乎无法解决它。 当我尝试在应用程序中创建Emailinterest对象时会发生此问题。
我有两个模特。
对于每次提交,都可能有多个电子邮件兴趣,但电子邮件兴趣不存在
C:\滑轨\ actuirl5 \应用\控制器\ emailinterests_controller.rb
submission_id实际上是emailinterest对象的成员。 submission_id假设包含提交对象的ID值。
def create
@emailinterest = Emailinterest.new(params[:emailinterest])
@submission = Submission.find(params[:submission_id])
respond_to do |format|
if @emailinterest.save
Notifier.emailinterest_notification(@emailinterest, @submission).deliver
format.html { redirect_to(@emailinterest, :notice => 'Email was successfully sent!') }
format.xml { render :xml => @emailinterest, :status => :created, :location => @emailinterest }
else
format.html { render :action => "new" }
format.xml { render :xml => @emailinterest.errors, :status => :unprocessable_entity }
end
end
end
C:\滑轨\ actuirl5 \应用\视图\提交\ show.html.erb
....
<%= render :partial=>"form_new_emailinterest", :locals=>{:emailinterest=>Emailinterest.new(:submission_id=>@submission.id)} %>
C:\滑轨\ actuirl5 \应用\视图\提交
<%= form_for(emailinterest) do |f| %>
<%= hidden_field :submission_id, :value => @submission.id %>
<div class="field">
<%= f.label :sender_email %><br />
<%= f.text_field :sender_email %>
</div>
<div class="field">
<%= f.label :sender_email_content %><br />
<%= f.text_area :sender_email_content %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
C:\滑轨\ actuirl5 \应用\控制器\ emailinterests_controller.rb
def create
@emailinterest = Emailinterest.new(params[:emailinterest])
@submission = Submission.find(params[:submission_id])
respond_to do |format|
if @emailinterest.save
Notifier.emailinterest_notification(@emailinterest, @submission).deliver
format.html { redirect_to(@emailinterest, :notice => 'Email was successfully sent!') }
format.xml { render :xml => @emailinterest, :status => :created, :location => @emailinterest }
else
format.html { render :action => "new" }
format.xml { render :xml => @emailinterest.errors, :status => :unprocessable_entity }
end
end
end
C:\滑轨\ actuirl5 \应用\视图\ submissions_form_new_emailinterest.html.erb
<%= form_for(emailinterest) do |f| %>
<%= hidden_field_tag :submission_id, value = @submission.id %>
<div class="field">
<%= f.label :sender_email %><br />
<%= f.text_field :sender_email %>
</div>
<div class="field">
<%= f.label :sender_email_content %><br />
<%= f.text_area :sender_email_content %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
C:\滑轨\ actuirl5 \应用\视图\提交\ show.html.erb
<%= render :partial=>"form_new_emailinterest", :locals=>{:emailinterest=>Emailinterest.new} %>
答案 0 :(得分:3)
考虑到你在show动作中设置了@submission,这应该有效:
<%= hidden_field :submission_id, :value => emailinterest.submission_id %>
然后
@submission = Submission.find(params[:emailinterest][:submission_id])
您无法在部分中访问@submission等实例变量。这就是为什么你有选择:locals。