开发者

Rails 3-"Couldn't find [model] without an ID" problem

开发者 https://www.devze.com 2023-04-01 07:18 出处:网络
Couldn\'t find Submission without an ID This problem has haunted me for last few days, and I can\'t seem to fix it.
Couldn't find Submission without an ID

This problem has haunted me for last few days, and I can't seem to fix it. This problem occurs when I try to create an Emailinterest object in the app.

I have two models.

  • Submission
  • Emailinterest

For every submission, there can be several emailinterest, but emailinterest cannot exist

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

C:\Rails\actuirl5\app\views\subm开发者_Python百科issions\show.html.erb

....

<%= render :partial=>"form_new_emailinterest", :locals=>{:emailinterest=>Emailinterest.new(:submission_id=>@submission.id)} %> 

C:\Rails\actuirl5\app\views\submissions

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

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


Considering you have set @submission in your show action this should work:

<%= hidden_field :submission_id, :value => emailinterest.submission_id %>

and then

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

You cannot access instance variables like @submission in a partials. This is why you have the option :locals.

0

精彩评论

暂无评论...
验证码 换一张
取 消