rails form_tag和调用destroy方法的单选按钮

时间:2022-11-24 10:14:35

I am creating an admin page for my app and it simply displays everything in my database. I want to have one of the table header's be delete, and the table data be a radio button that when clicked and hit enter, deletes that column from my table (and database).

我正在为我的应用创建一个管理页面,它只显示我数据库中的所有内容。我希望其中一个表头被删除,而表数据是一个单选按钮,当单击并按回车键时,从我的表(和数据库)中删除该列。

I have the DELETE route created from calling resources :urls in routes.rb:

我有从调用资源创建的DELETE路由:routes.rb中的url:

resources :urls

# DELETE /urls/:id(.:format)      urls#destroy

I have the Destroy Method in my url Controller:

我在我的网址控制器中有Destroy方法:

def destroy
    id = params[:id]
    url = Url.find(id)
    url.destroy

end

And in my admin.html.erb I have the following code:

在我的admin.html.erb中,我有以下代码:

<div class="webpage-name"> Admin Page </div>

    <table class="table table-hover table-striped">
        <thead>
            <tr>
                <th> id </th>
                <th> link </th>
                <th> random_string </th>
                <th> clicks </th>
                <th> delete </th>
            </tr>
        </thead>
        <tbody>
            <% @urls.each do |url| %>
                <tr>
                    <td><%= url.id %></td>
                    <td><%= url.link %></td>
                    <td><%= url.random_string %></td>
                    <td><%= url.clicks %></td>
                    <td><%= form_tag(urls_path, method: "delete")%><%= url.radio_button%></td>
                </tr>
            <% end %>
        </tbody>
    </table>

</div>

What I am trying to implement is the final <td>. I created a form_tag that sends the information to urls_path with the method: "delete". But now how do I actually make a radio button, checkbox or anything that allows me to select many options and then hit enter and delete them? Also, I'm very new at this so I'm not sure if my form_tag syntax is correct and doing what I believe it to be doing.

我想要实现的是最终的。我创建了一个form_tag,它使用以下方法将信息发送到urls_path:“delete”。但是现在我如何实际制作一个单选按钮,复选框或任何允许我选择许多选项然后点击输入并删除它们的东西?另外,我对此非常陌生,所以我不确定我的form_tag语法是否正确并且做了我认为它正在做的事情。

When I remove <%= url.radio_button%> from my code, I am able to view the admin page normally and just have an empty delete column.

当我从我的代码中删除<%= url.radio_button%>时,我能够正常查看管理页面并且只有一个空的删除列。

1 个解决方案

#1


0  

Try to submit form on radio button click like

尝试单击单选按钮提交表单

<% form_tag(url_path(url), method: "delete") do %>
<%= radio_button_tag :remove, url.id, false, :onchange =>"this.closest('form').submit();" %>
<% end %>

http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-radio_button_tag I am not sure this could work or not just try it.

http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-radio_button_tag我不确定这可行或不试试。

#1


0  

Try to submit form on radio button click like

尝试单击单选按钮提交表单

<% form_tag(url_path(url), method: "delete") do %>
<%= radio_button_tag :remove, url.id, false, :onchange =>"this.closest('form').submit();" %>
<% end %>

http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-radio_button_tag I am not sure this could work or not just try it.

http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-radio_button_tag我不确定这可行或不试试。