0
我一直在關注Railscast Raffler教程,並試圖在我的用戶控制器中創建一個新視圖,這將使用Angular顯示所有用戶角色爲「user 「從用戶表(我還沒有實現這個),然後隨機選擇其中一個成爲贏家(勝利者字段將布爾檢查爲真)。目前,我決定在用戶控制器中添加一個新的方法,「抽獎」,當然還有一個相應的視圖來展示這一點。然而,當我打開用戶/抽獎活動,我碰到下面的錯誤,我似乎也跟着定義資源之前的路線的正確方法:Rails/Angular:沒有路由匹配[GET]
No route matches [GET] "/raffle"
(也許還有更優化的方式來做到這一點,但我的要求是,我請用角來實現這一點,因爲我真的需要學習它)
這是我的routes.rb:
Rails.application.routes.draw do
resources :scholarships
devise_for :users, :controllers => { registrations: 'registrations' }
get 'users/raffle' => 'users#raffle'
resources :users
get 'static_pages/instructions'
get 'static_pages/about'
get 'static_pages/contact'
get 'static_pages/landing_page'
root 'static_pages#landing_page'
end
這裏是我的用戶控制器:
class UsersController < ApplicationController
include Devise::Controllers::Helpers
before_action :authenticate_user!
#Users who are not signed in cannot view users list
before_action :set_user, only: [:show, :edit, :update, :destroy]
load_and_authorize_resource
respond_to :html, :json, only: [:raffle]
# GET /users
# GET /users.json
def index
@users = User.all
end
def raffle
end
# GET /users/1
# GET /users/1.json
def show
end
# GET /users/new
def new
@user = User.new
end
# GET /users/1/edit
def edit
@user = current_user
end
# POST /users
# POST /users.json
def create
@user = User.new(user_params)
respond_to do |format|
if @user.save
format.html { redirect_to @user, notice: 'User was successfully created.' }
format.json { render :show, status: :created, location: @user }
else
format.html { render :new }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /users/1
# PATCH/PUT /users/1.json
def update
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to @user, notice: 'User was successfully updated.' }
format.json { render :show, status: :ok, location: @user }
else
format.html { render :edit }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
# DELETE /users/1
# DELETE /users/1.json
def destroy
@user.destroy
respond_to do |format|
format.html { redirect_to users_url, notice: 'User was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
end
#Never trust paramaters from the scary internet, man.
def user_params
params.require(:user).permit(:first_name, :last_name, :email)
end
end
我有角的JavaScript文件:
var rafflerApp = angular.module('rafflerApp', ["ngResource"]);
rafflerApp.controller('rafflerController', function($scope, $resource) {
User = $resource("https://stackoverflow.com/users/:id", {id: "@id"},{update: {method: "PUT"}})
$scope.users = User.query()
//Draw a New Winner
$scope.drawWinner = function(){
pool=[];
angular.forEach($scope.users,function(user){
if(!user.winner){
pool.push(user) ;
}
});
if(pool.length >0) {
user = pool[Math.floor(Math.random()*pool.length)];
user.winner = true;
user.$update();
$scope.lastWinner = user;
}
}
});
我raffle.html.erb文件:
<% if user_signed_in? && current_user.role == "Admin" %>
<h1 class="center">Winner Raffler</h1>
<h4 class="center">To select a winner for this year's scholarship, simply click "choose winner" below</h4>
<div ng-controller="rafflerController">
<ul>
<li ng-repeat="user in users">
{{user.first_name}}
<span ng-show="user.winner">Winner</span>
</li>
</ul>
</div>
<button ng-click="drawWinner()">Draw Winner</button>
<% end %>
用戶模式:
create_table "users", force: :cascade do |t|
t.string "first_name"
t.string "last_name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.inet "current_sign_in_ip"
t.inet "last_sign_in_ip"
t.integer "role", default: 1
t.boolean "winner", default: false
t.integer "scholarship_id"
end
預先感謝您。
補充說,它仍然吐出同樣的錯誤 –