简体   繁体   中英

In Rails4, using Trailblazer, how do I access the current_user

We are building a Rails4 app using Trailblazer. I have never worked with Trailblazer before and I am confused about how to do things.

We are building an auction site. I was previously using a traditional controller, and this route endpoint was working fine:

 def bill    
     @profile = Profile.find_by user_id: current_user_id
     @current_order = Order.order(created_at: :desc).find_by(user_id: current_user_id)
     @batch = @current_order.batch

     if @batch.nil?
       puts "There was no batch linked to the current order of #{@current_order.id}"
       flash[:error] = "We are sorry, but we could not determine which batch your order belongs to."
     else
       @price_shown_to_customer = @batch.price + ENV["FUELBID_FEE_PER_GALLON"].to_f
       @amount = @current_order.quantity * @price_shown_to_customer
     end

But now I'm suppose to create this as a Trailblazer api, using a Representer class.

So in routes.rb I added something for "charges":

 namespace :api do
   get '/price' => 'info#info'
   post '/order' => 'orders#create'
   get '/charges' => 'charges#bill'
 end

I created this Api but copying-and-pasting another:

 module Api
   class ChargesController < ApiApplicationController

     respond_to :json

     def bill
       respond_with OpenStruct.new.extend(ChargesRepresenter)
     end

   end
 end

I tested the above with a simple Representer and it all worked fine, so everything is good up to this point. If I return simple data from the Representer, then I can see it fine here:

http://localhost:3000/api/charges.json

But I need to get the current_user. How is this done? Right now, this does not work:

 module ChargesRepresenter
   include Roar::JSON

   collection :price_shown_to_customer

   def price_shown_to_customer
     current_order = Order.order(created_at: :desc).find_by(user_id: current_user_id)
     puts "current_order"
     puts current_order.id
     batch = current_order.batch
     batch.price + ENV["FUELBID_FEE_PER_GALLON"].to_f  
   end
 end     

current_user_id exists in my traditional controllers because we set up Devise and so my traditional controllers inherit it:

  class ChargesController < SecuredController

But is there any way to get it in a Trailblazer Representer?

Hope this answer is not too late.

  1. If you can switch to Decorator pattern instead of a Module.
  2. Representer really doesn't need to know and doesn't care if it is called from controller or console or test. All it needs is a hash to build your json object from. So you can just pass another attribute called current_user_id to your Representer and then use it inside r presenter like you do.

FYI:

If you need a more immediate response you can also copy your question to https://gitter.im/trailblazer/chat . There are usually several people hanging out there. But it's also good to post a question here for posterity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM