Jason Kim
Jason Kim

Reputation: 19041

Rails 3 - Does ID of an object have to be an integer value?

C:\Rails\actuirl5\app\controllers\emailinterests_controller.rb

C:\Rails\actuirl5\app\controllers\emailinterests_controller.rb

submission_id actually a member of emailinterest object. submission_id is suppose to contain the ID value of submission object.

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

I keep getting stuck at this line

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

with this error.

Couldn't find Submission without an ID

Submission and Emailinterest are both objects.

Emailinterest need some information about the members of the Submission object to be created.

submission_id actually a member of Emailinterest object. submission_id is suppose to contain the ID value of Submission object.

Now I have submission_id as string value. Does it have to be an integer value?


FIX

C:\Rails\actuirl5\app\controllers\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:\Rails\actuirl5\app\views\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:\Rails\actuirl5\app\views\submissions\show.html.erb

<%= render :partial=>"form_new_emailinterest", :locals=>{:emailinterest=>Emailinterest.new} %>

Upvotes: 0

Views: 154

Answers (2)

mu is too short
mu is too short

Reputation: 434685

This error:

Couldn't find Submission without an ID

is probably caused by

@submission = Submission.find(nil)

and nil is what a Hash will give when you ask it for a key it doesn't have (unless someone has supplied a different default of course but params and its contained Hashes should give you nil). So you don't have a :submission_id inside params[:emailinterest]. Based on your comments, I would guess that you do have params[:submission_id] so try this:

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

and if that doesn't work, do a logger.debug params.inspect and look at your logs to see where :submission_id is (if anywhere at all).

Upvotes: 2

Benoit Garret
Benoit Garret

Reputation: 13675

It can be a string or anything, as long as it can be converted to an integer.

You can verify this using your rails console:

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">

If you want to know what's really in submission_id, you can log the value by putting this at the beginning of your method:

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

Upvotes: 1

Related Questions