Rails 3.2 - 嵌套资源传递ID

时间:2012-03-07 21:05:39

标签: ruby-on-rails forms ruby-on-rails-3.2 nested-resources

好的,我的关联是:

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

我的路线:

resources :outlets do
   resources :monitorings
end

查看:

<%= link_to new_outlet_monitoring_path(@outlet) %>

单击链接时,日志显示outlet_id作为参数正确传递给新页面。 但是在保存监控记录时,outlet_id变为零。

任何帮助?

更新

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

控制器:

# 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

1 个答案:

答案 0 :(得分:1)

这很可能是您创建新监控记录的方式的问题。我们可以看到您的表单和创建控制器操作吗?