1

Okay so my associations are:

Outlet -> has_many :monitorings
Monitoring -> belongs_to :outlet

My Routes:

resources :outlets do
   resources :monitorings
end

View:

<%= link_to new_outlet_monitoring_path(@outlet) %>

When I click the link, the logs show that the outlet_id is passed as a parameter to the new page correctly. But when saving the monitoring record, the outlet_id becomes nil.

Any help?

UPDATE:

# views/monitorings/_form.html.erb  

<%= form_for(@monitoring) do |f| %>
<h2>Type of Monitoring</h2>
<fieldset data-role="controlgroup" >
    <div class="radio-group">
      <%= f.radio_button :mtype, "Full" %><%= f.label :mtype, "Full", value: "Full" %>
      <%= f.radio_button :mtype, "Partial" %><%= f.label :mtype, "Partial", value: "Partial" %>
      <%= f.radio_button :mtype, "None" %><%= f.label :mtype, "None", value: "None" %>
    </div>
</fieldset>
<hr>
<%= f.submit "Next Step" %>
<% end %>

And the controller:

# controllers/monitoring_controller.rb  


def new
    @monitoring = Monitoring.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @monitoring }
    end
end

def create
  @monitoring = Monitoring.new(params[:monitoring])

  respond_to do |format|
    if @monitoring.save
      format.html { redirect_to @monitoring, notice: 'Monitoring was successfully created.' }
      format.json { render json: @monitoring, status: :created, location: @monitoring }
    else
      format.html { render action: "new" }
      format.json { render json: @monitoring.errors, status: :unprocessable_entity }
    end
  end
end
Ammar
  • 1,091
  • 12
  • 23

1 Answers1

1

This is most likely an issue with the way you are creating the new monitoring record. Can we see your form and your create controller action?

JohnColvin
  • 2,489
  • 1
  • 14
  • 8
  • I've added it as an update to the original question for you :) – Ammar Mar 07 '12 at 22:15
  • 1
    There are two ways to do this. Both involve you creating the outlet object in the new controller action. So add `@outlet = Outlet.find(params[:outlet_id])` to the new action. Now you have two options. 1: change the declaration of @monitoring to `@monitoring = @outlet.monitorings.new` and change nothing in your view. 2: leave the declaration of @monitoring alone and change your form_for to `form_for [@outlet, @monitoring] do |f|` – JohnColvin Mar 09 '12 at 05:29
  • I use option 2 and have not tried option 1 before. I **THINK** it will work, though. – JohnColvin Mar 09 '12 at 05:35
  • I've tried both of these and just can't seem to get it to work. The logs are showing that the outlet_id is passed into monitorings#new `Parameters: {"outlet_id"=>"2"}`, but when the create action runs the outlet_id remains empty. – Ammar Mar 09 '12 at 11:04
  • outlet_id will always be passed to the create action because you have a nested route that looks like /outlets/:outlet_id/monitorings/. You are creating a monitorings object using params[:monitoring]. Which means you need to pass the outlet id as monitoring[outlet_id]. The second option I gave you should do that automatically. – JohnColvin Mar 09 '12 at 14:05
  • There's an example of what I'm talking about here: http://apidock.com/rails/ActionView/Helpers/FormHelper/form_for – JohnColvin Mar 09 '12 at 14:16
  • I've figured out that it's not setting the outlet_id because I'm not displaying a field for it in the form, even though I'm passing in the param using the controller, is there a way around this? – Ammar Mar 12 '12 at 22:51
  • The nested form helper should be creating a hidden field for outlet_id for you. It would get you by to create your own hidden field for it, but it would be nice to know what's going wrong. – JohnColvin Mar 12 '12 at 23:17