Rails 3.2 - 嵌套的资源传递ID [英] Rails 3.2 - Nested Resource Passing ID

查看:140
本文介绍了Rails 3.2 - 嵌套的资源传递ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我的协会是:

  Outlet  - > has_many:监控
监控 - > belongs_to:outlet

我的路线:

  resources:outlets做
资源:监控
结束

查看:

 <%= link_to new_outlet_monitoring_path(@outlet)%> 

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



有什么帮助吗?

更新:

 #views / monitorings / _form.html.erb 

<%= form_for(@monitoring)do | f | %GT;
< h2>监控类型< / 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下一步%>
<%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


解决方案

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


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

解决方案

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?

这篇关于Rails 3.2 - 嵌套的资源传递ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆