如何在Rails中创建系统投票或投票?

时间:2019-04-04 08:29:39

标签: ruby-on-rails ruby

我想创建一个投票系统。 我创建了一个类别CRUD(创建,读取,更新和销毁)以具有不同的问题主题。 我创建了一个Question CRUD和Response CRUD

这是我的第一个想法,如果您有任何建议我是买家,那肯定很傻

我想要在我的投票表中显示什么 Image of votes table

route.rb

 Rails.application.routes.draw do
  devise_for :users
  resources :categories do
    resources :questions do 
    resources :votes
    end
  end
  resources :questions do
    collection do
      patch :sort
    end
    resources :reponses
  end
end

资源:投票肯定不适当

投票#index.html.erb

 <div class="container bg-primary d-flex flex-column border-left border-right border-light shadow p-5 ">
  <div class="d-flex flex-row">
    <button class="btn  p-3 progress">
      <i class="fas fa-home text-size-2em"></i>
    </button>
  <% @category.questions.each do |question| %>
  <a href="../<%= question.id %>/votes">
    <% if question.id != @question.id %>
      <button class="btn  p-3 bg-secondary progress" id="<%= question.id %>"></button>
    <% else %>
      <button class="btn  p-3 bg-light progress" id="<%= question.id %>"></button>
    <% end %>
      </a>  
    <% end %>
  </div>
  <div class="col-12 d-flex flex-column justify-content-center align-items-center">     
    <div class="col-10 bg-light border border-warning rounded d-flex flex-row justify-content-center">
      <h3 class="text-center pt-4 pb-4 p-2"> <p> <%= @question.question %></p> </h3>
    </div>         
    <%= bootstrap_form_for([@category, @question,@vote]) do |f| %>
      <div class="btn-group-toggle" data-toggle="buttons">
        <% @question.reponses.each do |reponse| %>
        <label class="btn btn-secondary ">
          <input type="radio"  id="<%= reponse.id %>"> <%= reponse.reponse %>
        </label>
        <% end %>
      </div>


          <%= f.submit %>

      </div>
      <% end %>
      <% if @question.prev %>
        <button class="btn btn-secondary m-2 ">
          <i class="fas fa-arrow-circle-left"></i> <%= link_to "Question précédente ",  category_question_votes_path(@category, @question.prev)%>
        </button>
      <% end %>
      <%if  @question.next%>
      <button class="btn btn-secondary m-2 ">
        <%= link_to "Question suivante",  category_question_votes_path(@category, @question.next)%> <i class="fas fa-arrow-circle-right"></i>
      </button>
    <% end %>   
  </div>
</div>

Image of the system voting

category_id-1,值:早餐  question_id-10,值:“您更喜欢搭配:”  票数:

show the url

votes_controller.rb

class VotesController < ApplicationController
    before_action :find_the_category
    before_action :find_the_question
    before_action :authenticate_user!
    before_action :set_votes,  only: [:show, :edit, :update, :destroy]

    def index
      @vote  = Vote.new

    end

    def create 
      @vote = @category.questions.votes.create(vote_params)
      flash[:success] = "à voté"
      if @vote.save
        redirect_to category_question_votes_path(@category, @question)
      end  
    end
private

    def vote_params
      params.require(:vote).permit(:reponse_id)
    end

    def find_the_question
      @question = Question.find(params[:question_id])
    end

    def find_the_category
      @category = Category.find(params[:category_id])
    end
    def set_votes
      @vote = @question.votes.find(params[:id])
    end
end

0 个答案:

没有答案