我正在將acts_as_votable集成到我的模型的元素中,而且我幾乎在那裏,但是我面臨着一些缺少路由的問題。就我所瞭解的other question而言,在對我的Model元素中的一個加註後,該應用程序將被重定向到:post。我試圖包括資源:黑客和資源:黑客(已經存在),但它似乎不是解決方案。未初始化的常量PostsController - 集成充當可用
的routes.rb
Rails.application.routes.draw do
resources :hacks do
member do
put "like", to: "posts#upvote"
end
end
devise_for :users, :controllers => { omniauth_callbacks: 'omniauth_callbacks' }
match '/profile/:id/finish_signup' => 'users#finish_signup', via: [:get, :patch], :as => :finish_signup
match 'tagged' => 'hacks#tagged', :as => 'tagged', via: 'get'
#To show public profiles
get '/users/:id', :to => "users#show", :as => :user
root "pages#home"
get 'pages/about'
# TheComments routes
concern :user_comments, TheComments::UserRoutes.new
concern :admin_comments, TheComments::AdminRoutes.new
resources :comments, concerns: [:user_comments, :admin_comments]
end
劈顯示
<p>
<strong>Liked by: </strong>
<%= @hack.liked_by @user %>
<%= link_to "Favorite?", like_hack_path(@hack), method: :put, class: "button tiny" %>
</p>
黑客控制器
class HacksController < ApplicationController
before_action :set_hack, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:new]
# GET /hacks
# GET /hacks.json
def index
@hacks = Hack.all
end
# GET /hacks/1
# GET /hacks/1.json
def show
@hack = Hack.find params[:id]
@comments = @hack.comments.with_state([:draft, :published])
end
# GET /hacks/new
def new
@hack = current_user.hacks.build
end
# GET /hacks/1/edit
def edit
end
# POST /hacks
# POST /hacks.json
def create
@hack = current_user.hacks.build(hack_params)
respond_to do |format|
if @hack.save
format.html { redirect_to @hack, notice: 'Hack was successfully created.' }
format.json { render :show, status: :created, location: @hack }
else
format.html { render :new }
format.json { render json: @hack.errors, status: :unprocessable_entity }
end
end
end
def upvote
@hack = Hack.find(params[:id])
@hack.liked_by current_user
redirect_to @hack
end
# PATCH/PUT /hacks/1
# PATCH/PUT /hacks/1.json
def update
respond_to do |format|
if @hack.update(hack_params)
format.html { redirect_to @hack, notice: 'Hack was successfully updated.' }
format.json { render :show, status: :ok, location: @hack }
else
format.html { render :edit }
format.json { render json: @hack.errors, status: :unprocessable_entity }
end
end
end
def tagged
if params[:tag].present?
@hacks = Hack.tagged_with(params[:tag])
else
@hacks = Hack.postall
end
end
# DELETE /hacks/1
# DELETE /hacks/1.json
def destroy
@hack.destroy
respond_to do |format|
format.html { redirect_to hacks_url, notice: 'Hack was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_hack
@hack = Hack.find(params[:id])
end
def correct_user
@hack = current_user.hacks.find_by(id: params[:id])
redirect_to hacks_path, notice: "Not authorized to edit this hack" if @hack.nil?
end
# Never trust parameters from the scary internet, only allow the white list through.
def hack_params
params.require(:hack).permit(:description, :image, :url, :tag_list)
end
end
的ActionController :: RoutingError在/黑客/ 1 /像 未初始化的常數PostsController
完整跟蹤
Started PUT "/hacks/1/like" for 127.0.0.1 at 2014-06-29 12:05:51 +0200
ActionController::RoutingError - uninitialized constant PostsController:
actionpack (4.1.1) lib/action_dispatch/routing/route_set.rb:69:in `rescue in controller'
actionpack (4.1.1) lib/action_dispatch/routing/route_set.rb:64:in `controller'
actionpack (4.1.1) lib/action_dispatch/routing/route_set.rb:44:in `call'
actionpack (4.1.1) lib/action_dispatch/journey/router.rb:71:in `block in call'
actionpack (4.1.1) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.1.1) lib/action_dispatch/routing/route_set.rb:676:in `call'
omniauth (1.2.1) lib/omniauth/strategy.rb:186:in `call!'
omniauth (1.2.1) lib/omniauth/strategy.rb:164:in `call'
warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
warden (1.2.3) lib/warden/manager.rb:34:in `call'
rack (1.5.2) lib/rack/etag.rb:23:in `call'
rack (1.5.2) lib/rack/conditionalget.rb:35:in `call'
rack (1.5.2) lib/rack/head.rb:11:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/flash.rb:254:in `call'
rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.1.1) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.1.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
activerecord (4.1.1) lib/active_record/migration.rb:380:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.1.1) lib/active_support/callbacks.rb:82:in `run_callbacks'
actionpack (4.1.1) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
better_errors (1.1.0) lib/better_errors/middleware.rb:84:in `protected_app_call'
better_errors (1.1.0) lib/better_errors/middleware.rb:79:in `better_errors_call'
better_errors (1.1.0) lib/better_errors/middleware.rb:56:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.1.1) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.1.1) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.1.1) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.1.1) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.1.1) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.1.1) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.1.1) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/static.rb:64:in `call'
rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
railties (4.1.1) lib/rails/engine.rb:514:in `call'
railties (4.1.1) lib/rails/application.rb:144:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
() Users/javier/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
() Users/javier/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
() Users/javier/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'
請用完整的stacktrace發佈確切的錯誤。 – Pavan
@Pavan剛剛完成。 – malditojavi
你有'PostsController'嗎? – Pavan