0

I'm trying to provide a form to create a "message" for each feature on the features_index page. Unfortunately I get the error "Couldn't find Feature without an ID."

Have I passed the locals in properly? Params does not appear to have a feature_id! (perhaps this is the time for an @ instead?

The features/index view:

 - @features.each do |feature|
%tr
  %td= feature.project_id
  %td= feature.subject
  %td= feature.details
  %td= link_to 'Show', feature
  %td= link_to 'Edit', edit_feature_path(feature)
  %td= link_to 'Destroy', feature, :confirm => 'Are you sure?', :method => :delete

  - if feature.messages.length > 0
    - feature.messages.each do |message|
      = render partial: "message_form", locals: {message: message}
  - else
    %td
      = render partial: "message_form", locals: {message: Message.new(feature_id: feature)}

%br/

The features/_message_form:

= simple_form_for(message) do |f|
  = f.error_notification

.form-inputs
  -# = f.input :feature_id
  -# = f.input :sender
  -# = f.input :receiver
  = f.input :subject
  = f.input :details

.form-actions
  = f.button :submit

and the messages controller:

def create
@feature = Feature.find(params[:message][:feature_id])

@message = @feature.messages.build(params[:message])

respond_to do |format|
  if @message.save
    flash[:notice] = "Message sent!"
    format.html { redirect_to @message, notice: 'Message was successfully created.' }
    format.json { render json: @message, status: :created, location: @message }
  else
    flash[:notice] = "Message failed to send."
    format.html { render action: "new" }
    format.json { render json: @message.errors, status: :unprocessable_entity }
  end
end
end

Thank you!

Doug
  • 293
  • 1
  • 2
  • 10

1 Answers1

1

I believe you will need a hidden_field to pass through the param for feature_id into your create action.

f.hidden_input :feature_id
Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
Andrew Cetinic
  • 2,805
  • 29
  • 44
  • thank you - it worked perfectly! (I also made the really quick fix of{message: Message.new(feature_id: feature.id)}" – Doug Jun 08 '12 at 02:03