Favourite functionality Ruby on Rails

隐身守侯 提交于 2019-12-14 03:11:59

问题


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:

I need to add a controller "toogle_favourite", already made it with "rails generate controller toogle_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) The index.html of my application displays me a list of items that I optionally can set as favourite (there is a button).

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) Render another page listing all the favourite elements.


回答1:


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.

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

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

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>

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 %>

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



回答2:


I believe you should mark that favourite button with some CLASS. And then just paste something like in application.js:

$( ".your_class" ).on('change', function() {
      var item_id = $(this).data('item');
      var favourite = $(this).val();

      $.ajax({
        dataType: 'json',
        url: /toogle_favourite/+item_id+'/toogle_favourite',
        data: { favourite: favourite },
        type: "POST",
        success: function(r) {
        // Here you should refresh item or do something
        }
      });
});


来源:https://stackoverflow.com/questions/21953729/favourite-functionality-ruby-on-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!