2

I currently have three models and am trying to bring in a reference of one of the models (webpage.id) into another model (Micropost) - here is the situation:

class Member < ActiveRecord::Base
  has_many :microposts
end

class Webpage < ActiveRecord::Base
  has_many :microposts, :dependent => :destroy
end

class Micropost < ActiveRecord::Base
  belongs_to :member
  belongs_to :webpage
end

What I am trying to do is create a Micropost from the Webpage ':show' method.

Here is the Micropost controller:

class MicropostsController < ApplicationController
  def create
    @micropost  = current_member.microposts.build(params[:micropost])
    @webpage = Webpage.find(params['webpage_id'])
    @micropost.webpage = @webpage
    if @micropost.save
      flash[:success] = "Micropost created!"
      redirect_to root_path
    else
      @feed_items = []
      render 'pages/home'
    end
  end
end

and from the Webpage 'show' view:

<table class="front" summary="For signed-in members">
    <tr>
      <td class="main">
        <h1 class="micropost">What's up?</h1>
        <%= form_for @micropost do |f| %>
          <%= render 'shared/error_messages', :object => f.object %>
          <div class="field">
            <%= f.text_area :content %>
            <input type="hidden" id="webpage_id" name="micropost[webpage_id]" value="<%= @webpage.id %>"/>
          </div>
          <div class="actions">
            <%= f.submit "Submit" %>
          </div>
        <% end %>
      </td>
    </tr>
</table>

When I save the form, this is the error I am getting:

"Couldn't find Webpage without an ID"

When I remove reference to Webpage from the form, the form successfully saves and sets the user_id field - so this is working fine (but obviously in this case the webpage_id is NULL).

Any thoughts on where I am going wrong?

Cheers,

Damo

Damien Quintal
  • 155
  • 1
  • 9

1 Answers1

0

Dump the params to make sure that webpage_id is truly being passed.

In your view add:

<%= debug params %>
Dan Knox
  • 634
  • 5
  • 9