在嵌套模型导轨中传递数组隐藏字段 [英] pass array hidden field in nested model rails

查看:78
本文介绍了在嵌套模型导轨中传递数组隐藏字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的视图中包含以下代码:

I have following code in my view:

    <% @m1.map(&:id).each do |id|%>

    <%= b.fields_for :modul1hours do |f| %>

    <%= f.hidden_field :modul1_id, id %>
    <%= f.text_field :module_est_hours, :size => 30 %>
    </tr>
  <% end %>

<%end%>

在控制台中传递的参数

Parameters: {"authenticity_token"=>"LJ/ZME2lHZ7VwCDgPKX6OFe326fXSXo5UB4M0cPwbCE=", "esthour"=>{"rfp_id"=>"6", "ecommerce_est_hours"=>"", "modul1hours"=>{"module_est_hours"=>"3"}, "designpages_est_hours"=>"", "cms_est_hours"=>""}, "modul1_ids"=>["12", "13", "14"], "utf8"=>"✓", "project_id"=>"second", "commit"=>"Add Todo"}

当前用户:admin(id = 1)

Current user: admin (id=1)

modul1_ids是基于创建的三个文本框的隐藏数组,但是当我提交页面时给了我

modul1_ids is the hidden array based on that three text box is created but when i submit the page gives me:

ActionView::Template::Error (undefined method `merge' for 12:Fixnum):

在第一个文本框中,我通过了1 秒2 并排在第三名

in first textbox i passed 1 second 2 and in third 3

最后一个值(3)是传递的,您可以在控制台参数module_est_hours"=>"3中看到,但是其余两个字段y不传递又如何解决错误.请帮助我.

last value(3) isthe s passing that one can see in the console params module_est_hours"=>"3, but what about rest two fields y not passing and whats the solution for an error. Please help me.

编辑1

    <% @m1.map(&:id).each do |id|%>

    <%= b.fields_for :modul1hours do |f| %>

    <%= hidden_field_tag "modul1_ids[]", id %>
    <%= f.text_field :module_est_hours, :size => 30 %>
    </tr>
  <% end %>

<%end%>

此代码未给出错误,但值也未存储在modul1hours表中

this code does not give the error, but also value is not stored in modul1hours table

modul1hours表的字段为:

integer :modul1_id
decimal :module_est_hours
decimal :module_act_hours
integer :esthours_id

]

.rb

属于:esthour

belongs_to :esthour

attr_accessible:module_est_hours,:module_act_hours

attr_accessible :module_est_hours,:module_act_hours

和控制器

更新

    def new
@esthour = Esthour.new
 @gg =  @esthour.modul1hours.build
@project = params[:project_id]
@rfp = params[:rfp_id]

@m1 = Modul1.where(:rfp_id => @rfp.id)

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

结束 通过更新 #GET/project_todos/1/edit

end over Update # GET /project_todos/1/edit

      def edit
      @esthour = Esthour.find(params[:id])
      end


           def create

            @project = params[:project_id]


            @esthour = Esthour.new(params[:esthour])

            user_params = params.select{|k,v| k.include?('esthour')}

            respond_to do |format|

           if @esthour.save

       get_issue_attribute_param1(user_params)

             format.html { redirect_to project_rfp_url(@project,@esthour.rfp_id), :notice => 'hours was successfully created.' }
            format.json { render :json => @esthour, :status => :created, :location => @esthour }
  else
            format.html { render :action => "new" }
            format.json { render :json => @esthour.errors, :status => :unprocessable_entity }
  end
end
 end

是否需要任何构建?例如未保存在表中的控制器coz记录的新def中的Esthour.modul1hour.build?

视图

    <%= form_for @esthour,:rfp_id => @rfp.id,:project_id => @project do |b| %>

    <%= b.hidden_field :rfp_id, :value => @rfp.id %>

    <%= hidden_field_tag :project_id, @project %>
   <table>
      <tr>    <td><b>Menutype </b></td>
    <% if  @rfp.menutype.present? %>
        <td><%= @rfp.menutype %></td> 
        <td><%= b.number_field :menutype_est_hours %></td>
    <% end %>
</tr>

      <tr>           <td> <b>Number of menu</b> </td>
    <% if  @rfp.numberofmenu.present? %>
        <td><%= @rfp.numberofmenu %></td> 
        <td><%= b.number_field :numberofmenu_est_hours %></td>
    <% end %>
   </tr>

    <tr>

        <% @m1.map(&:id).each do |id|%>

         <%= b.fields_for :modul1hours do |f| %>


          <%= f.hidden_field :modul1_id, value => id  %>
          <%= f.text_field :module_est_hours, :size => 30 %>
         </tr>
      <% end %>

     <% end %>



      </table>
     <%= b.submit 'Add Todo' %>
     <% end %>


     @esthour = Esthour.new

         @gg =  @esthour.modul1hours.build
       @project = params[:project_id]

推荐答案

隐藏字段并不是这里的问题

Hidden fields aren't really the issue here

除了@BroiStatse的答案,我可以在您如何处理控制器上的参数方面看到问题

Apart from @BroiStatse's answer, I can see the issue as how you handle the params on your controller

嵌套模型

将数据发送到控制器会将数据发送到相关模型.通常使用 accepts_nested_attributes_for 处理,但也可以手动处理

Sending data to a controller sends that data to the relevant models. This is normally handled with accepts_nested_attributes_for, but can be handled manually too

从您的控制器代码中,我 无法看到您如何处理多余的数据,但是您的错误是由参数的错误merge引起的

From your controller code, I can't see how you're dealing with your extra data, but your error is caused by the incorrect merge of the params

我将使用accepts_nested_attributes_for来保存数据,而不是手动保存数据,如下所示:

Instead of saving the data manually, I would use the accepts_nested_attributes_for to save the data, like this:

#app/models/project.rb
Class Project < ActiveRecord::Base
    accepts_nested_attributes_for :modul1hours
end

这会将参数传递给modul1hours模型,然后您必须通过相关的attr_accessible动作来捕获它们

This will pass the params to your modul1hours model, where you'll then have to capture them with the relevant attr_accessible actions

f.fields_for

为了使accepts_nested_attributes_for正常工作,必须确保正确使用f.fields_for功能.

In order to get accepts_nested_attributes_for working properly, you have to ensure you use the f.fields_for function correctly.

您必须首先在new控制器操作中build ActiveRecord对象,如下所示:

You have to first build the ActiveRecord objects in your new controller action, like this:

def new
    @project = Project.new
    @project.modul1hours.build
end

您的问题是,您然后循环浏览modul1hours模型的ID,从而人工产生f.fields_for.如果在控制器中内置了ActiveRecord对象,则Rails仅输出f.fields_for:

Your problem is that you're then cycling through the ID's of your modul1hours model, yielding the f.fields_for artificially. Rails will only output an f.fields_for if the ActiveRecord object has been built in the controller:

此RailsCast 为您提供了一个更好的主意

This RailsCast gives you a better idea about this

我要这样做的是:

#app/controllers/projects_controller.rb
def new
    @project = Project.new
    @m1.map(&:id).each do |i|
        @project.modul1hours.build
    end
end

#app/views/projects/new.html.erb
<%= b.fields_for :modul1hours do |f| %>
    <%= hidden_field_tag :id, value :id %>
    <%= f.text_field :module_est_hours, :size => "30" %>
<% end %>

我还在考虑如何将ID分配给隐藏字段

I'm still thinking about how I would assign the ID's to the hidden field

更新

尝试一下:

#app/controllers/projects_controller.rb
def new
    @project = Project.new
    @project.modul1hours.build
end

modul1hours替换为

这篇关于在嵌套模型导轨中传递数组隐藏字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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