Advertisement
tonyjleal

Untitled

Nov 23rd, 2014
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.80 KB | None | 0 0
  1. #routes.rb
  2.   get 'admin' => 'admin#index', as: :admin_index
  3.   resources :admin, except: [:index]
  4.  
  5.  
  6.   root 'polls#index'
  7.   resources :polls, only: [:index, :votes_yes, :votes_yes, :mostrar_encuesta]
  8.   post 'polls/votes_yes/:id' => 'polls#votes_yes', as: :votes_yes
  9.   post 'polls/votes_no/:id' => 'polls#votes_no', as: :votes_no
  10.   get 'mostrar_encuesta/:id' => 'polls#show', as: :mostrar_encuesta
  11.  
  12.  
  13. #admin_controller.rb
  14. class AdminController < ApplicationController
  15.   before_action :set_poll, only: [:show, :edit, :update, :destroy]
  16.  
  17.   def index
  18.     @polls = Poll.all
  19.   end
  20.  
  21.   def new
  22.     @poll = Poll.new
  23.   end
  24.  
  25.   def edit
  26.   end
  27.  
  28.   def show
  29.   end
  30.  
  31.   def create
  32.     @poll = Poll.new(poll_params)
  33.     @poll.save
  34.     redirect_to admin_index_path
  35.   end
  36.  
  37.   def update
  38.  
  39.      @poll.update(poll_params)
  40.     redirect_to admin_index_path
  41.   end  
  42.  
  43.   def destroy
  44.     @poll.destroy
  45.     redirect_to admin_index_path
  46.   end
  47.  
  48.   private
  49.   def set_poll
  50.     @poll = Poll.find(params[:id])
  51.   end
  52.  
  53.   def poll_params
  54.     params.require(:poll).permit(:title, :description, :votes_yes, :votes_no)
  55.   end
  56.  
  57. end
  58.  
  59.  
  60. #polls_controller.rb
  61. class PollsController < ApplicationController
  62.   before_action :set_poll, only: [:show, :votes_yes, :votes_no]
  63.  
  64.   def index
  65.     @polls = Poll.all
  66.   end
  67.  
  68.   def show
  69.   end
  70.  
  71.   def votes_yes
  72.     @poll.votes_yes ||= 0
  73.     @poll.votes_yes += 1
  74.     @poll.save
  75.     redirect_to root_path
  76.   end
  77.  
  78.   def votes_no
  79.     @poll.votes_no ||= 0
  80.     @poll.votes_no += 1
  81.     @poll.save
  82.     redirect_to root_path
  83.   end  
  84.  
  85.   private
  86.     def set_poll
  87.       @poll = Poll.find(params[:id])
  88.     end
  89. end
  90.  
  91.  
  92. #Estructura de mis vistas:
  93. #app/views/admin
  94. #   edit.html.erb
  95. #   index.html.erb
  96. #   new.html.erb
  97. #   show.html.erb
  98.  
  99. #app/views/polls
  100. #   index.html.erb
  101. #   show.html.erb
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement