简体   繁体   English

基于同一页面上的表单提交的表加载,Ruby on Rails 3

[英]Table loading based on form submission on same page, Ruby on Rails 3

I have a page with a table that loads information based on variables in my controller. 我有一个带有表格的页面,该表格可根据控制器中的变量加载信息。 I am now trying to implement a form on the page. 我现在正在尝试在页面上实现表单。 I want the form to initiate the table information instead of the controller. 我希望表单启动表信息而不是控制器。 I have to take the form information and do some logic against the database to filter the information on the table. 我必须采取表单信息,并对数据库做一些逻辑以过滤表上的信息。 Something like this: 像这样:

<% @tableResellers = [] %>
<% if @tableResellers << @reseller.company_region.include?(reseller.company_region) %>
<td><% @tableResellers.company_region %></td>

How do you get the form information into @tableResellers on the same page and instantly load the table information? 如何将表单信息获取到同一页面上的@tableResellers中并立即加载表信息?

My Form: 我的表格:

<%= form_for(@reseller) do |f| %>
<fieldset>      
    <div class="row">
        <span class="span5 pagination-right">
            Region <span style="color: red">*</span>
            <%= f.select :company_region, options_for_select([ ["North America", "North America"], ...]) %>
        </span>
        <span class="span6"></span>
    </div>
    <div class="row">
        <span class="span5 pagination-right">
            Country <span style="color: red">*</span>
            <%= f.select :company_country, options_for_select([ ['Australia', 'Australia'], ... ]) %>
        </span>
        <span class="span6"></span>
    </div>
    <div class="row">
        <span class="span5 pagination-right">
            State/Province <span style="color: red">*</span>
            <%= f.select :company_state, options_for_select([ ['Alabama', 'Alabama'], ...]) %>
        </span>
        <span class="span6"></span>
    </div>
        <div class="row">
        <span class="span5 pagination-right">
            City <span style="color: red">*</span>
            <%= f.text_field :company_city, placeholder: "City" %>
        </span>
    </div>

</fieldset>
<hr />
<p class="pagination-centered">
    <%= f.submit "Register", class: "btn btn-large btn-primary" %>
</p>
<% end %>

My Table Body: 我的桌身:

<tbody>
    <% @resellers.each do |reseller| %>
        <tr>
                <td><%= reseller.contact_name %></td>
                <td><%= link_to reseller.company_name, grandstreamers_reseller_path(reseller) %></td>
                <td><%= reseller.company_region %></td>

                <% if reseller.company_country == "United States" %>
                <td><%= reseller.company_state %></td>
                <% else %>
                <td><%= reseller.company_country %></td>
                <% end %>
                <td><%= reseller.company_city %></td>
                <td><%= reseller.area_served.gsub(/['-]/, '')%></td>                    
                <td><%= reseller.contact_email %></td>
                <td><%= reseller.company_website %></td>
                <td><%= reseller.approved ? "Yes" : "No" %></td>
            <% if @reseller_users_incerts.include?(reseller.id) %>
                <td><%= "Yes" %></td>
            <% else %>
                <td><%= "No" %></td>
            <% end %>
                <td><%= link_to "Edit", edit_grandstreamers_reseller_path(reseller) %>
            </tr>

    <% end %>
</tbody>

Based on your comment, you are looking to a partial to draw the table, let's call it: 根据您的评论,您正在寻找局部绘制表格的方法,我们称其为:

_draw_resellers.html.erb

So, what I would do would be to post the form data with an ajax method to an action in the controller that will render the PARTIAL (not the view, because it will reload your full asset library). 因此,我要做的就是使用ajax方法将表单数据发布到将呈现PARTIAL(而不是视图,因为它将重新加载整个资产库)的控制器中的操作。

In your controller you would probably do something like this: 在您的控制器中,您可能会执行以下操作:

def resellers_table
    @resellers=Reseller.where(your_params).all
    render :partial 'draw_resellers'
end

You could use the basic jquery ajax function for that. 您可以为此使用基本的jquery ajax函数。 You need, of course, to add an id to your table or tbody (tab_resellers, for instance) in order to replace the html with the ajax response text. 当然,您需要向表或表体(例如,tab_resellers)中添加一个id,以便用ajax响应文本替换html。 So, you should replace your f.submit with a button to call your ajax function. 因此,应该用一个按钮替换f.submit来调用ajax函数。

Check here for the method. 在这里检查方法。 https://api.jquery.com/jQuery.ajax/ It's quite straightforward. https://api.jquery.com/jQuery.ajax/这很简单。 Basically, your button would be something like 基本上,您的按钮将类似于

<%= link_to_function "Register", "draw_my_table(your_params)" %>

and your js 和你的js

function draw_my_table(your_params)  
{ 
    $.ajax({
        type: "GET",
        dataType: "html",
        url: your_url,  //the route to your url with the params (if form is 'get' method)
        beforeSend: function(data)
        {
        },
        success: function(data)
        {
            $('#tab_resellers').html(data);
        }
    });
}

Edit: 编辑:

Your main view would be something like this: 您的主要观点将是这样的:

controller: 控制器:

def filter_resellers #for example
    @resellers=Reseller.all
    @reseller= #not sure what it is, could be plain html form, not rails form
end

and your view, filter_resellers.html.erb 和您的视图filter_resellers.html.erb

<table>
    <tr>
    #headers
    </tr>
    <tdata id='tab_resellers'>
        <%= render :partial => 'draw_resellers' %>
    </tdata>
</table>

<% form_for ....

Your partial, _draw_resellers.html.erb , would be, as you write it _draw_resellers.html.erb ,您的部分_draw_resellers.html.erb将是

<% @resellers.each do |reseller| %>
...
<% end %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM