rails将数据返回到ajax成功函数

时间:2017-10-15 17:58:07

标签: ruby-on-rails ajax

我有这个ajax函数,它调用名为changeshudo的控制器方法

       $('select#weeknumber').on('change', function(event) {
          var selected_shudo = $(this).val();             
          $.ajax({
            type:'post',
            url:'/clients/changeshudo',
            data:{ week: selected_shudo},
            success: function(data){  

              },
              error: function(XMLHttpRequest, textStatus, errorThrown) { 
                  alert("Status: " + textStatus); alert("Error: " + errorThrown); 
              }   
          });
        });

在控制器中我有方法

           def changeshudo
           @client = Client.find_by('weeknumber = ?',params[:week])             

            if @client.nil?           
           render :js => "params[:week]"
            else   
          @client = Client.find_by(weeknumber: params[:week])      
          render :js => "window.location = '#{edit_client_path(@client)}'"
        end

      end

if @ client.nil?是的,然后我想将params [:week]发送回成功函数并重定向到newnation,其中weeknumber设置为value等于params [:week]

即。如果数据库中不存在该值,那么表单的所有字段将仅使用您要提交表单的周数值设置清除

下面给出的

是调用更改事件ajax方法的下拉列表

    <% if @client[:id].nil? %>   
    <%= f.select :weeknumber, options_for_select(@fmon_date ,selected: @fmon_date.last), {:include_blank => "Please select"}, class: 'form-control' %>
    <% else %>
    <%= f.select :weeknumber, options_for_select(@fmon_date , selected: @client[:weeknumber]), {:include_blank => "Please select"}, class: 'form-control' %>
    <% end %>

请帮我将数据从控制器传递到ajax成功方法并重定向到new,并在下拉列表中设置值

0 个答案:

没有答案