动态控制数据不存储在数据库中

时间:2014-03-27 06:12:30

标签: jquery ruby-on-rails

对于注册用户获取一些凭据后,我获得用户的教育意味着学位名称和持续时间from_year和to_year到静态控件下面是静态控件代码:

<table width="100%">
      <tr style = "text-align:center;">
         <td>
            <%= f.text_field :tf_Degree,placeholder: "Degree" %>
         </td>
      </tr>
      <tr style = "text-align:center;">
         <td>
            From
            <%= f.select :fromyear, (1995..Time.now.year).to_a, :include_blank => {:year => "Select year"} %>
            to
            <%= f.select :toyear, (1995..Time.now.year).to_a, :include_blank => {:year => "Select year"} %>
         </td>
      </tr>
</table>

如果我想进一步增加用户的教育,那么我点击add按钮使用jquery添加动态控件,下面是动态控件代码:

<table>
      <tr>
         <td align="center">
            <table id="Controls">
            </table>

            <div><input value="Add" type="submit" /></div>
            <input id="valEdu" type="hidden" value="0" />

         </td>
      </tr>
</table>

以下是用于创建动态控件的jquery:

     <script type="text/javascript">

     $(document).ready(function() {
        $("input[value='Add']").click(function(e)    {

            e.preventDefault();
            var hidval = $('#valEdu').val();
            if(hidval = 0)
            {
             hidval = 1;
            }
            else
            {
             hidval++;
            }  
            alert(hidval);

            var rownum=$("#Controls > tbody > tr").length;
            alert(rownum);
            var updated_row_num;
            if (rownum == 0)
            {
                updated_row_num=0;
            }
            else {
                updated_row_num=rownum/2;
            }

            // var field = $("#field").val();
            var year = new Date().getFullYear();

            var DDL_fromProfession = "<select name='ParametersFromSch["+ updated_row_num +"]' id='DDL_FromSchYear'>";
            for (var i = year; i >= 1950; --i) {
                DDL_fromProfession += "<option text='" + i + "' value='" + i + "'>" + i + "</option>";
            }
            DDL_fromProfession += "</select>";

                var DDL_ToProfession = "<select name='ParametersToSch["+ updated_row_num +"]'  id='DDL_ToSchYear'>";
                for (var j = year; j >= 1950; --j) {
                    if (j != year) {
                        DDL_ToProfession += "<option text='" + j + "' value='" + j + "'>" + j + "</option>";
                    }
                    else {
                        DDL_ToProfession += "<option text='Present' value='Present'>Present</option>";

                    }
                }
                DDL_ToProfession += "</select>";

     var input1 = "<input name='parametersSch["+ updated_row_num +"]' id='field' type='text' placeholder='Degree' style='text-align:center;' onfocus='WaterMarkSchool(this, event);' onblur='WaterMarkSchool(this, event);' />"
                var newRow = "<tr><td align='center' style='font-size: x-large; color: #212121;' height='35px'>"
                + input1 + "</td></tr>";


var controls = "<tr><td>From "+ DDL_fromProfession + " To "+DDL_ToProfession+ "</td></tr>";
controls += "<br/><button type='button' class='btn_rmv'>Remove</button></td></tr>";

     $('#Controls').append(newRow);
     $('#Controls').append(controls);
     return false;
  });

            $('#Controls').on('click', '.btn_rmv', function() {
              alert("ali");
                var index = $(this).closest('tr').index() + 2
                $('#Controls tr:nth-child(n+' + (index - 2) + ')').remove();
                return false;
            });

 });
</script> 

但问题是只有静态控件数据存储在db中,并且动态控件不存储在db中,为了解决这个问题,我添加了隐藏控件并在jquery中我检查如果隐藏控件值等于零,然后它保存1,否则它的增量如下:

 var hidval = $('#valEdu').val();
                if(hidval = 0)
                {
                 hidval = 1;
                }
                else
                {
                 hidval++;
                }  
                alert(hidval);

在控制器页面中,我正在检查隐藏控件的状况:

{用于存储静态数据)

 if !params[:Educations][:tf_Degree].blank? && params[:Educations][:tf_Degree] != "Degree" 
           @Degree = params[:Educations][:tf_Degree]
           @From = params[:Educations][:fromyear]
           @To = params[:Educations][:toyear]
           @Education = Education.new( :UserID => current_user.id, :SchoolName => @Degree, :SchoolFrom => @From, :SchoolTo => @To )
           @Education.save
        end
        {for storing dynamic data}
        if params[:valEdu].present? && params[:valEdu] > 0 {here I am checking hidden value}
           params[:parametersSch].each do |keydeg, degree|
            if !degree.nil?
               @degree=degree
                puts @degree
                params[:ParametersFromSch].each do |keyfrom, from|
                if keydeg== keyfrom
                   @from=from
                end
            end
            params[:ParametersToSch].each do |keyto, to|
                if keydeg==keyto
                   @to=to          
                end
            end
            @Education = Education.new( :UserID => current_user.id, :SchoolName => @degree, :SchoolFrom => @from, :SchoolTo => @to )
            @Education.save
           end
          end
        end
        #render 'index'
        redirect_to '/default'
      end

在上面我检查它隐藏的控件值是grater 0然后它将数据存储在db中,但是它不能正常工作,请帮助我,等待你的回复。 感谢

1 个答案:

答案 0 :(得分:0)

好的,我并不是真的了解所有内容,但我发现代码中至少有2个错误。

首先,在您的javascript代码中

if(hidval = 0){
  ...
}
else{
  ...
}

在这段代码中,我认为hidval的开头是一个字符串,因此它不是0而是'0'。现在,hidval = 0是一个总会返回false的条件!你想写的是hidval == 0我想。另外,我不了解情况,你需要什么,因为最后,你总是a + 1没有?所以不是为了整个条件,为什么不写: hidval++。它会有相同的行为。

最后,在您的ruby代码中:

params[:valEdu] > 0 

可能会引发错误,例如:ArgumentError: comparison of String with 0 failed

因为查询参数总是字符串,所以你可以做

params[:valEdu].to_i > 0

哼,如果你想更新隐藏输入的值,你不是这样做的,这里你只是更新javascript变量的值,但是没有触及DOM元素,你可以写什么那就是:

a = $('#valEdu').val();
$('#valEdu').val(++a);

我不知道这是否是一个答案,但至少它会帮助你进一步我猜!