Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #routes.rb
- get 'admin' => 'admin#index', as: :admin_index
- resources :admin, except: [:index]
- root 'polls#index'
- resources :polls, only: [:index, :votes_yes, :votes_yes, :mostrar_encuesta]
- post 'polls/votes_yes/:id' => 'polls#votes_yes', as: :votes_yes
- post 'polls/votes_no/:id' => 'polls#votes_no', as: :votes_no
- get 'mostrar_encuesta/:id' => 'polls#show', as: :mostrar_encuesta
- #admin_controller.rb
- class AdminController < ApplicationController
- before_action :set_poll, only: [:show, :edit, :update, :destroy]
- def index
- @polls = Poll.all
- end
- def new
- @poll = Poll.new
- end
- def edit
- end
- def show
- end
- def create
- @poll = Poll.new(poll_params)
- @poll.save
- redirect_to admin_index_path
- end
- def update
- @poll.update(poll_params)
- redirect_to admin_index_path
- end
- def destroy
- @poll.destroy
- redirect_to admin_index_path
- end
- private
- def set_poll
- @poll = Poll.find(params[:id])
- end
- def poll_params
- params.require(:poll).permit(:title, :description, :votes_yes, :votes_no)
- end
- end
- #polls_controller.rb
- class PollsController < ApplicationController
- before_action :set_poll, only: [:show, :votes_yes, :votes_no]
- def index
- @polls = Poll.all
- end
- def show
- end
- def votes_yes
- @poll.votes_yes ||= 0
- @poll.votes_yes += 1
- @poll.save
- redirect_to root_path
- end
- def votes_no
- @poll.votes_no ||= 0
- @poll.votes_no += 1
- @poll.save
- redirect_to root_path
- end
- private
- def set_poll
- @poll = Poll.find(params[:id])
- end
- end
- #Estructura de mis vistas:
- #app/views/admin
- # edit.html.erb
- # index.html.erb
- # new.html.erb
- # show.html.erb
- #app/views/polls
- # index.html.erb
- # show.html.erb
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement