最喜欢的功能Ruby on Rails [英] Favourite functionality Ruby on Rails

查看:73
本文介绍了最喜欢的功能Ruby on Rails的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为我的项目添加功能,但是我只使用Ruby on Rails大约2周,我有点迷失了.问题如下:

I need to add functionality to my project but I have only been using Ruby on Rails for about 2 weeks and I'm kind of lost. The question is the following:

我需要添加一个控制器"toogle_favourite",并且已经通过"rails generate controller toogle_favourite"使其成为了

I need to add a controller "toogle_favourite", already made it with "rails generate controller toogle_favourite"

现在,我需要向控制器"toogle_favourite"添加一个操作,以修改"favourite"字段的值("favourite"是我之前使用脚手架创建的布尔字段).

Now I need to add an action to the controller "toogle_favourite" that modifies the value of the field "favourite" (favourite is a boolean field I made before with scaffolding).

此外,我的应用程序需要实现另一个页面,每当我添加一个收藏夹项目时,该收藏夹就会显示出来.让我们看一下它如何工作的示例:

In addition, my application needs to implement another page where the favourites will be displayed every time I add an item as a favourite. Let's see an example of how it has to work:

1)我的应用程序的index.html向我显示了一个列表,可以选择将其设置为收藏夹(有一个按钮).

1) The index.html of my application displays me a list of items that I optionally can set as favourite (there is a button).

2)一旦我将1个元素标记为收藏夹,其模型的布尔字段就会变为"TRUE",因此它是收藏夹.

2) As soon as I mark 1 element as favourite, the boolean field of its model turns into "TRUE", so it's a favourite.

3)渲染另一个列出所有喜欢的元素的页面.

3) Render another page listing all the favourite elements.

推荐答案

下面的代码向用户显示了选择复选框的列表,这些用户可以从选择中检查自己的收藏夹.一旦用户对他们的选择(即收藏夹)感到满意,他们可以单击提交"按钮,然后会出现一个新页面,显示一个仅包含他们选择的收藏夹的过滤列表.

The code below presents a list of selection checkboxes to users, who may check their favourites from the selection. Once the user is satisfied with their selections (i.e. favourites), they may click the submit button, and a new page will appear displaying a filtered list containing only the favourites they chose.

如果您希望获得有关HTML的技巧,请查看我的应用程序 Trendmyhunch 的代码(在我的GitHub帐户).

If you'd like tips with HTML, check out the code for my app Trendmyhunch (shared on my GitHub account).

步骤1::将index.html文件更改为index.erb:

Step 1: Change your index.html file to index.erb:

步骤2:将以下嵌入式Ruby添加到您的index.erb文件(视图)以及您自己的自定义HTML中:

Step 2: Add the following embedded Ruby into your index.erb file (view) plus your own customised HTML:

<h2>Select Favourites:</h2>
<%= form_tag '/favourites#form', :name => 'form1', :method => 'get' do %> # http get request
    <% @favs.each do |fav| %> 
        <%= fav.name, params[:fav_name] %>  # create original list from data source
    <% end %>
    <% @favs.each do |fav| %> 
        <%= label_tag :filter, "" %>
        <%= check_box_tag 'filter_values[]', fav.id, false %> # array of checkbox values
        <%#= check_box(:accepted, 'false', selected:false) %> 
        <%= hidden_field_tag 'favourites_ids[]', fav.id %> # hidden array of values accessible across pages
    <% end %>
<% end %>

<h2>Choose Next Action:</h2>
<%= form_tag '/favourites/show', :method => 'post' do %> 
    <% @favs.each do |fav| %> # same as previous hidden array allowing access to checkbox values
        <%= hidden_field_tag 'favourites_ids[]', fav.id %> //       
    <% end %>
<% end %>
<%= submit_tag("Set Favourite", data: { disable_with: "Please wait.." }, class: "btn btn-primary") %><span class="help-block">Please press this button after selecting the items you wish to set as favourites.</span>

步骤3:将以下嵌入式Ruby添加到show.erb文件(视图)中,以及您自己的自定义HTML:

Step 3: Add the following embedded Ruby into your show.erb file (view) plus your own customised HTML:

<h2>Show Favourites:</h2>
<% @favs.each do |fav| %> # list the names of the checkboxes that were checked as favourites
    <%= fav.name %> 
<% end %>

步骤4:将以下内容添加到toogle_favourite.rb文件(控制器)中:

Step 4: Add the following into your toogle_favourite.rb file (controller):

class ToogleFavouriteController < ApplicationController
    def index
        if params[:filter_values].present?
            @favs = Favourite.where(:id => params[:filter_values]) # assign to a variable only the database id's of favourites that are selected as favourites in the list of checkboxes
            @favs.toggle = true
        elsif !params[:filter_values].present?
            @favs = Favourite.all
        end
    end

    def show
        @favs = Favourite.where(:id => params[:favs_ids])
    end
end

这篇关于最喜欢的功能Ruby on Rails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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