Rails 3 - 对象的ID必须是整数值吗?

时间:2011-08-31 19:36:08

标签: ruby-on-rails ruby ruby-on-rails-3

C:\滑轨\ actuirl5 \应用\控制器\ emailinterests_controller.rb

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

我一直被困在这条线上

@submission = Submission.find(params[:submission_id])

出现此错误。

Couldn't find Submission without an ID

提交和Emailinterest都是对象。

Emailinterest需要一些有关要创建的Submission对象成员的信息。

submission_id实际上是Emailinterest对象的成员。 submission_id假设包含Submission对象的ID值。

现在我将submission_id作为字符串值。它必须是整数值吗?


FIX

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} %>

2 个答案:

答案 0 :(得分:2)

此错误:

  

无法找到没有ID的提交

可能是由

引起的
@submission = Submission.find(nil)

nil是Hash在你要求它没有的密钥时会给出的(除非有人提供了不同的默认值,但是params并且它包含的Hashes应该给你nil)。因此,您:submission_id内没有params[:emailinterest]。根据您的评论,我猜你确实有params[:submission_id]所以试试这个:

@submission = Submission.find(params[:submission_id])

如果这不起作用,请执行logger.debug params.inspect并查看日志以查看:submission_id的位置(如果有的话)。

答案 1 :(得分:1)

它可以是字符串或任何东西,只要它可以转换为整数。

您可以使用rails控制台验证这一点:

ruby-1.9.2-p0 > Product.find("1")
  Product Load (0.2ms)  SELECT "products".* FROM "products" WHERE "products"."id" = ? LIMIT 1  [["id", "1"]]
 => #<Product id: 1, collection_id: nil, name: "test", description: nil, price: 1000, picture_file_name: nil, picture_content_type: nil, picture_file_size: nil, picture_updated_at: nil, created_at: "2011-07-14 10:10:45", updated_at: "2011-07-14 10:10:45">

如果您想知道submission_id中的内容,可以通过将其放在方法的开头来记录该值:

logger.debug params[:emailinterest][:submission_id]