mercredi 1 juillet 2015

Rake Aborted! "Don't know how to build task 'dropbox:authorize'"

Working with rails and my application, and bumped into a problem. I've come to the step where I need to install dropbox, and incorporate it to my code. The first step was to install the dropbox gem, and that happened with ease. Then I had to complete the migration, which was also a success. Then it came to the problem to rake dropbox, which the problem happend.

This is the code that I put into my command line:

rake dropbox:authorize APP_KEY=your_app_key APP_SECRET=your_app_secret ACCESS_TYPE= dropbox | app_folder. 

Of course I put my key and secret code into the code, but when I pressed enter, I got the following error.

rake aborted! Don't know how to build task 'dropbox:authorize'

YAML encoding my subclass of string as string

I'm using ruby 1.9.3

I'm having difficulty with using psych (1.3.4) when serializing a custom subclass of string.

Basically I have:

class BinaryData < String
  def my_method
    ...
  end
end

In my model I have:

class MyModel < ActiveRecord::Base
  serialize :parameters

  def set_stuff
    data = BinaryData.new('some string here that needs to be treated specially later')
    self.parameters = {'foo' => 'bar', 'data' => data}
    seld.save
   end
end

When I was using syck it would serialize something like this:

---
  foo: bar
  data: !ruby/string:BinaryData "some string here that needs to be treated specially later"

But with psych I get:

---
  foo: bar
  data: some string here that needs to be treated specially later

How can I force psych to encode it as my object?

RSpec 3.2: How to test render inside a private method of controller?

I'm new to rspec and I'm using RSpec 3.2. I have a method in my controller which is like,

class MainController < ApplicationController
  before_filter :get_items
  //other methods
 def get_items
   if @user.admin?
     @user_items = [ModuleName].fetchItems
   end

   if @user_items.empty?
     flash.now[:error] = "No items for the user"
     render "index"
   end
 end
end

And my RSpec looks like this,

require "rails_helper"

 RSpec.describe MainController, :type => :controller do

  describe '#get_items' do
     it 'gets the items for admin' do
      @controller = MainController.new
      @controller.instance_eval{ get_items }   # invoke the private method
      expect(@controller.instance_eval{ @user_items }).not_to be_blank
      expect(response).not_to render_template("index")
      end
  end
end

The above rspec works fine when I keep @user.admin? as true. How do I test the other 'IF' block where it renders the index and also mock the value of '@user.admin?' to be true or false? Please note that '@user.admin?' comes from another module in my lib folder.

How to call javasscript_tag in rails?

I followed the each step of above question and here are my changes:

In show.html.erb (http://localhost:3000/posts/24)

<% javascript_tag do %>
 $(function() {
   setInterval(function(){ $.get("/posts/<%= @post.id   %>");   }, 5000);
    });
<% end %>

<span id="changed_data">
 It should be change after every 5 seconds
</span>

controller class

def show
 @post = Post.find(params[:id])
 respond_to do |format|
   format.js
   format.html
 end

end

Added a new file

app/views/posts/show.js.erb
$("#changed_data").html("<h2> Changed </h2>");

But it sounds like that javascript_tag is not calling jquery method. What am I doing wrong here?

Validate paramaters in Restful endpoints

I am rookie in rails restful web service and i am trying build service that returns a json dump of deals.So far my app returns these deals in json format when you hit http://localhost:3000/api/deals. Now i want to add two mandatory parameters(deal_id and title) and two optional parameters in the uri http://localhost:3000/api/deals?deal_id=2&title=book. What is the best way to validate these two mandatory parameters. Assuming Deal model has fields deal_id, title, description and vendor.
Here is my code

Controller

  module Api
  class DealsController < ApplicationController
  respond_to :json

  def index
   @deals = Deal.all
   respond_with (@deals)
  end
 end
end

routes

 namespace :api,:defaults => {format:'json'} do
 resources :deals
end

Rails ActiveRecord Serializer not found

I have a few serializers set up in my app and recently decided to create another one for a new feature. This one is nearly identical to the others, except that the model/attributes are different. I'm not sure why, but no matter what I do, Rails doesn't seem to detect that I have a serializer for my model and bypasses it. Even if I just put in a bunch of gibberish code that should raise an exception, Rails delivers a JSON response with all the attributes of my model. I'm not sure what I'm missing here. I referenced this Railscast to see if I missed a step, but as far as I can tell, everything looks right.

Here is my app/serializers/job_description_serializer.rb:

class JobDescriptionSerializer < ActiveModel::Serializer
    attributes :id,
        :title,
        :role,
        :description,
        :posting_link,
        :company_id,
        :company_name,
        :created_at,
        :updated_at

    def company_name
        object.company.name
    end
end

Here is my jobs_controller.rb. I've tried this without the root: false argument as well.

class Admin::JobsController < ApplicationController
    layout 'admin'

    before_action :authenticate_user!
    before_action :authenticate_admin
    def index
        @job_descriptions = JobDescription.all
        @total_count = @job_descriptions.count

        respond_to do |format|
            format.html { render action: 'index' }
            format.json { render json: { jobs: @job_descriptions, total_count: @total_count }, root: false }
        end
    end
end

How to periodically called controller method in rails?

Possibly duplicated: How to get Rails to periodically call a controller method

I am on : http://localhost:3030/book/5 where I can see <%= @book.count %>

Now I want to write a jquery which periodically (every 5 seconds) call the show method in some interval of time.

 def show
  @book = Book.find(params[:id])
  # update the value of @post.count
  # save new value of count in post table for [:id]
  # return the updated value of count to http://localhost:3030/book/5
end

How can I implement it? Any help would be appreciable.