Form inputs don't create record - ruby-on-rails

I am using paperclip and dropbox for images.
I am not sure why I am getting this. Some of the solutions i tried for resolving the sqlite3 busy exception error didn't work.
But I also suspect my parameters somehow aren't correct...
the controller specifies i need a character object, but the parameters don't seem to have that. I know i use a file attachment called :image, but paperclip, imagemagick, and dropbox are supposed to handle the creation of the image attributes once the image gets uploaded and the form is submitted.
This is the error I get in my browser.
Form:
= form_for(#character, :html => { :multipart => true }) do |f|
- if #character.errors.any?
#error_explanation
%h2= "#{pluralize(#character.errors.count, "error")} prohibited this character from being saved:"
%ul
- #character.errors.full_messages.each do |msg|
%li= msg
#stripe_error.alert.alert-danger{ :style => "display:none" }
%noscript JavaScript is not enabled and is required for this form. First enable it in your web browser settings.
.form-group
= f.label :name
= f.text_field :name
.form-group
= f.label :image
= f.file_field :image
.action
= f.submit
Controller:
def create
#character = Character.new(character_params)
respond_to do |format|
if #character.save
format.html { redirect_to #character, notice: 'Character was successfully created.' }
format.json { render action: 'show', status: :created, location: #character }
else
format.html { render action: 'new' }
format.json { render json: #character.errors, status: :unprocessable_entity }
end
end
end
...
private
# Use callbacks to share common setup or constraints between actions.
def set_character
#character = Character.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def character_params
params.require(:character).permit(:name, :image)
end
end
Gemfile: (yes, did bundle install)
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.0'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more:
https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 1.2'
group :doc do
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', require: false
end
# Use Haml for views
gem 'haml-rails'
gem 'bootstrap-sass', "~> 3.0.3.0"
gem 'paperclip', "~> 3.0"
gem 'paperclip-dropbox', ">= 1.1.7"
group :development do
gem 'nokogiri'
#gem 'hpricot', '0.8.6'
gem 'ruby_parser', '~> 3.1.1'
# erubis is already included in action pack
gem 'html2haml'
end
group :development, :test do
gem 'sqlite3'
end
group :production do
gem 'pg'
gem 'rails_12factor'
end
# Use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'
# Use unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano', group: :development
# Use debugger
# gem 'debugger', group: [:development, :test]

Related

Ruby on Rails - undefined method `split' for nil:NilClass / NoMethodError in StocksController#search

I'm a Rails newbie and I'm taking the Complete Ruby on Rails Developer Course on Udemy. I'm getting the following error when I try to go to the /search_stocks route.
Here's the repo in its current state (I also have the code pasted below): https://github.com/sarahbasinger/rails-stock-tracker
Here's the Udemy course repo:
https://github.com/udemyrailscourse/finance-tracker
The TA for the course suggests it might be a gem version conflict. I'm using Rails 5.1.4 (maybe a newbie mistake - I thought using the latest and greatest would be a good way to go). The teacher in the course is using Rails 4. The TA suggested I use the same gem versions as the course, so I updated my Gemfile to match the course Gemfile, ran bundle install, and with that, I can't even get the rails server to run. I get a different error. So I'm back to trying to get this app running using Rails 5. However, I have no experience trying to resolve gem version conflicts, if that is the issue.
Here's the relevant code:
Model
class Stock < ActiveRecord::Base
def self.new_from_lookup(ticker_symbol)
looked_up_stock = StockQuote::Stock.quote(ticker_symbol)
new(name: looked_up_stock.name, ticker: looked_up_stock.symbol, last_price: looked_up_stock.l)
end
end
Controller
class StocksController < ApplicationController
def search
#stock = Stock.new_from_lookup(params[:stock])
render json: #stock
end
end
View
<h1>My portfolio</h1>
<h3>Search for stocks</h3>
<div id="stock-lookup">
<%= form_tag search_stocks_path, method: :get, id: "stock-lookup-form" do %>
<div class="form-group row no-padding text-center col-md-12">
<div class="col-md-10">
<%= text_field_tag :stock, params[:stock], placeholder: "Stock ticker symbol", autofocus: true, class: "form-control search-box input-lg" %>
</div>
<div class="col-md-2">
<%= button_tag(type: :submit, class: "btn btn-lg btn-success") do %>
<i class="fa fa-search"></i> Look up a stock
<% end %>
</div>
</div>
<% end %>
</div>
Gemfile
source 'https://rubygems.org'
git_source(:github) do |repo_name|
repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
"https://github.com/#{repo_name}.git"
end
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.1.4'
gem 'devise'
gem 'twitter-bootstrap-rails'
gem 'jquery-rails'
gem 'devise-bootstrap-views'
gem 'stock_quote'
# Use Puma as the app server
gem 'puma', '~> 3.7'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 3.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
group :development, :test do
gem 'sqlite3'
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
# Adds support for Capybara system testing and selenium driver
gem 'capybara', '~> 2.13'
gem 'selenium-webdriver'
end
group :development do
# Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
gem 'web-console', '>= 3.3.0'
gem 'listen', '>= 3.0.5', '< 3.2'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
end
group :production do
gem 'pg'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
Any help is appreciated!
It looks to me that you need a parameter called "stock". In line 4
#stock = Stock.new_from_lookup(params[:stock])
It is looking for a URL parameter called "stock" which is a ticker symbol. Try something like:
localhost:3000/search_stocks?stock=goog
That will create a stock param and give it something to look up. You could also put in some code to handle the case where the param is nil.
def search
if params[:stock]
#stock = Stock.new_from_lookup(params[:stock])
else
# you should have some directions here for what happens if there is no stock param given.
#stock = nil
end
render json: #stock
end
Probably even better to do that in the model, now that I think of it:
def self.new_from_lookup(ticker_symbol)
if ticker_symbol
looked_up_stock = StockQuote::Stock.quote(ticker_symbol)
else
# something here for a missing stock param
looked_up_stock = Stock.first
end
new(name: looked_up_stock.name, ticker: looked_up_stock.symbol, last_price: looked_up_stock.l)
end
I hope this helps!
You can try the following because most of the time gem functionality updating that's why not working on implementing this code. I've recently worked this type of project write code the below which is tested.
Model
def self.find_by_ticker(ticker_symbol)
where(ticker: ticker_symbol).first
end
def self.new_from_lookup(ticker_symbol)
begin
looked_up_stock = StockQuote::Stock.quote(ticker_symbol)
price = strip_commas(looked_up_stock.l)
new(name: looked_up_stock.name, ticker: looked_up_stock.symbol, last_price: price)
rescue Exception => e
return nil
end
end
def self.strip_commas(number)
number.gsub(",", "")
end
Hope to help

Rails 5 : NoMethod Error - Undefined Method Paginate

I have a JSON API based on Rails 5. I have a method that brings list of all the products for one particular company, which I want to paginate so that not to bring up all the data at once. For this I came across two gems, namely will_paginate and api-pagination.
After running bundle install and restarting the server, I'm still getting the following error :
NoMethodError (undefined method `paginate' for #<Api::V1::ProductsController:0x007fb25404db30>)
The Products Controller :
require 'roo'
class Api::V1::ProductsController < ApplicationController
respond_to :json
def index
comp_id = params[:company_id]
cat_id = params[:category_id]
if comp_id
if comp_id && cat_id
product = Product.where(:company_id => comp_id, :category_id => cat_id)
else
product = Product.where(:company_id => comp_id)
end
paginate json: product, status: 200
else
render json: { errors: "Company ID is NULL" }, status: 422
end
end
end
Gemfile.rb
source 'https://rubygems.org'
git_source(:github) do |repo_name|
repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
"https://github.com/#{repo_name}.git"
end
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.0.2'
# Use mysql as the database for Active Record
gem 'mysql2', '>= 0.3.18', '< 0.5'
# Use Puma as the app server
gem 'puma', '~> 3.0'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
#Devise Gem for Authentication
gem "devise"
#For JSON Serialization
gem 'active_model_serializers'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 3.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
gem 'carrierwave'
gem 'roo'
#For Pagination
gem 'will_paginate'
gem 'api-pagination'
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platform: :mri
end
group :development do
# Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
gem 'web-console', '>= 3.3.0'
gem 'listen', '~> 3.0.5'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
# Use Capistrano for deployment
gem 'capistrano', group: :development
gem 'capistrano-rails', group: :development
gem 'capistrano3-puma', group: :development
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
Can someone tell me where I'm going wrong?
Try to make sure your ApplicationController inherits from ActionController::API instead of ActionController::Base. And if for some reason you can't update your controller, you can include the Rails::Pagination module into your ApplicationController manually.
credits
Update:
class ApplicationController
include Rails::Pagination
......
....
end

Rails cannot load such file while authenticating yahoo via oauth

I am getting this error i don't understand why I have that in my gem file and also included it properly still have no idea how to resolve this error
cannot load such file -- omniauth_oauth
[yahoos_controller]
require 'omniauth_oauth'
require 'multi_json'
module OmniAuth
module Strategies
#
# Authenticate to Yahoo via OAuth and retrieve basic
# user information.
#
# Usage:
#
# use OmniAuth::Strategies::Yahoo, 'consumerkey', 'consumersecret'
#
class YahoosController < omniAuth::strategies::OAuth
def initialize(app, consumer_key, consumer_secret)
super(app, :yahoo, consumer_key, consumer_secret,
# Specifying the full url is the only way yahoo seems to work. Serious WTFery here.
:request_token_url => 'https://api.login.yahoo.com/oauth/v2/get_request_token',
:access_token_url => 'https://api.login.yahoo.com/oauth/v2/get_token',
:authorize_url => "https://api.login.yahoo.com/oauth/v2/request_auth")
end
def auth_hash
ui = user_info
OmniAuth::Utils.deep_merge(super, {
'uid' => ui['uid'],
'user_info' => ui,
'extra' => {'user_hash' => user_hash}
})
end
def user_info
profile = self.user_hash['profile']
nickname = profile['nickname']
{
'uid' => profile['guid'],
'nickname' => nickname,
'name' => profile['givenName'] || nickname,
'image' => profile['image']['imageUrl'],
'description' => profile['message'],
'urls' => {'Profile' => profile['profileUrl'] }
}
end
def user_hash
uid = #access_token.params['xoauth_yahoo_guid']
#user_hash ||= MultiJson.decode(#access_token.get("http://social.yahooapis.com/v1/user/#{uid}/profile?format=json").body)
end
end
end
end
[gem file]
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.4'
# Use sqlite3 as the database for Active Record
gem 'mysql2','~> 0.3.20'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
gem 'omniauth', '~> 1.2'
gem 'omniauth-auth0', '~> 1.1'
gem 'omniauth-yahoo-oauth2', '~> 1.1'
gem 'nokogiri'
gem 'multi_json', '~> 1.11', '>= 1.11.2'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use Unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug'
end
group :development do
# Access an IRB console on exception pages or by using <%= console %> in views
gem 'web-console', '~> 2.0'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
end
The gem omniauth-auth0 is for authenticating with the auth0 service. You don't appear to want this.
The gem omniauth_oauth is for building generic strategies - you don't need this either.
To authenticate with Yahoo, you should only need these gems:
gem 'omniauth', '~> 1.2'
gem 'omniauth-yahoo-oauth2', '~> 1.1'
To set up Yahoo, it is a simpler set up than the one you are using. It is shown in the docs here. The key part is:
Rails.application.config.middleware.use OmniAuth::Builder do
provider :yahoo_oauth2, yahoo_client_id, yahoo_secret,
name: 'yahoo'
end
Nick

Datatables + JSON + best_in_place

I can not add gem best_in_place to my table.
class_datatable.rb:
def data
class.map do |record|
[
best_in_place(record, :name),
best_in_place(record, :short_name)
]
end
end
There is an error: NoMethodError (undefined method `best_in_place' for ClassDatatable:0xa9283a44)
My gem file:
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.4'
# Use sqlite3 as the database for Active Record
gem 'ruby-oci8'
gem 'activerecord-oracle_enhanced-adapter'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
gem 'jquery-turbolinks'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use Unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
#---------------------------------My gems--------------------------------------------------------
#Twitter Bootstrap
gem "therubyracer"
gem "less-rails" #Sprockets (what Rails 3.1 uses for its asset pipeline) supports LESS
gem "twitter-bootstrap-rails"
gem 'client_side_validations'
#Datatables
gem 'jquery-datatables-rails', '~> 1.12.2'
gem 'font-awesome-rails' # Icons
gem 'foundation-rails' # zurb foundation
#Best in place
gem 'best_in_place'
#Russian langue
gem 'russian', '~> 0.6.0'
#Jquery UI
gem 'jquery-ui-rails'
#Paginate
gem 'will_paginate'
#-----------------------------------end---------------------------------------------------------
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug'
end
group :development do
# Access an IRB console on exception pages or by using <%= console %> in views
gem 'web-console', '~> 2.0'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
end
Bundle install is done!
If I do like this on the page:
<% Class.all.each do |record| %>
<%= best_in_place(record, :name) %>
<% end %>
That works.
However, the method does not work "def data"
Understood!
It was necessary to add delegate best_in_place section at the beginning of the file class_datatable.rb
For anyone struggling to find the right way to delegate, this is how it is done:
class MyCustomDatatable < AjaxDatatablesRails::Base
# either define them one-by-one
def_delegator :#view, :link_to
def_delegator :#view, :h
def_delegator :#view, :mail_to
# or define them in one pass
def_delegators :#view, :link_to, :h, :mailto, :edit_resource_path, :other_method
# now, you'll have these methods available to be used anywhere
# example: mapping the 2d jsonified array returned.
def data
records.map do |record|
[
link_to(record.fname, edit_resource_path(record)),
mail_to(record.email),
# other attributes
]
end
end
end
Source: https://github.com/antillas21/ajax-datatables-rails#using-view-helpers

ActiveModel Serializers: How do I serialize a resource collection?

I have a bookmarks resource and have mapped it to serve json by default under my api namespace like so in my routes.rb:
namespace :api, defaults: {format: 'json'} do
resources :bookmarks
get ':username', to: 'users#index'
get ':username/bookmarks/:id', to: 'users#show'
end
I have a Api::UsersController controller and a supporting BookmarkSerializer that works just fine on an individual bookmark resource like http://localhost:3000/api/emma_carter/bookmarks/87
But when I try to hit http://localhost:3000/api/emma_carter which is supposed to serve all bookmarks owned by the user, I get all different kinds of errors. Here is my Api::UsersController
module Api
class UsersController < ApplicationController
respond_to :json
def index
user = User.find_by(username: params[:username])
bookmarks = user.bookmarks
render json: bookmarks
end
def show
user = User.find_by(username: params[:username])
bookmark = user.bookmarks.find_by(params[:id])
render json: bookmark
end
end
end
The show method works but the index method gives me ArgumentError in Api::UsersController#index
wrong number of arguments (1 for 0)
UPDATE: Full stack trace here: https://gist.github.com/amite/b79fc42bfd73de5a07bd
screenshot
Here is the serializer:
class BookmarkSerializer < ActiveModel::Serializer
attributes :id, :url, :title, :domain, :notes, :image, :created, :username
belongs_to :user
def created
object.created_at
end
def username
user.username
end
end
Looking at other solutions on stack overflow, I have also tried other versions of my index method:
def index
user = User.find_by(username: params[:username])
bookmarks = user.bookmarks
bookmarks.map { |bookmark| ::BookmarkSerializer.new(bookmark)}.to_json #updated line
end
This gives me the error:
Missing template api/users/index, application/index with {:locale=>[:en], :formats=>[:json], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}.
Next the last version of my index method looks like this:
def index
user = User.find_by(username: params[:username])
bookmarks = user.bookmarks
ActiveModel::ArraySerializer.new(bookmarks, each_serializer: ::BookmarkSerializer).to_json
end
This gives me the error uninitialized constant ActiveModel::ArraySerializer
What am I doing wrong? I am using rails 4.1.5 and the github version of the active_model_serializers
gem.
gem 'active_model_serializers', github: 'rails-api/active_model_serializers'
UPDATE: Since I am trying to output a collection of bookmarks I also tried using a separate serializer
BookmarksSerializer but I am getting the same error: ArgumentError in Api::UsersController#index
wrong number of arguments (1 for 0)
UPDATE2: Here is a version of the index method that kinda works in the sense that it renders the resource collection in json format:
def index
user = User.find_by(username: params[:username])
bookmarks = user.bookmarks
respond_with bookmarks.to_json
end
But this still does not use the BookmarksSerializer
class BookmarksSerializer < ActiveModel::Serializer
attributes :id, :title
end
It just outputs the default hash
Full Gemfile
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.1.5'
# Use mysql as the database for Active Record
gem 'mysql2'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.3'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
gem 'bourbon'
gem 'neat'
gem 'bitters'
gem 'refills'
gem 'wisper'
gem 'rails-ioc'
gem 'reform'
gem 'cells'
gem "pundit"
gem 'active_model_serializers', github: 'rails-api/active_model_serializers'
gem "font-awesome-rails"
gem 'simple_form'
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
group :development, :test do
# Call 'debugger' anywhere in the code to stop execution and get a debugger console
gem 'byebug'
gem 'ffaker'
gem 'pry-rails'
gem 'capybara'
gem 'factory_girl_rails'
gem 'database_cleaner'
gem 'capybara-webkit'
gem 'rspec-cells'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-commands-rspec'
gem 'rspec-rails'
gem 'guard-rspec'
gem 'rb-fsevent' if `uname` =~ /Darwin/
end
# Use ActiveModel has_secure_password
gem 'bcrypt', '~> 3.1.7'
# Use unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
# Use debugger
# gem 'debugger', group: [:development, :test]
turns out I need to include v0.9 of the gem
gem 'active_model_serializers', github: 'rails-api/active_model_serializers', branch: '0-9-stable'
you could try change:
def index
user = User.find_by(username: params[:username])
bookmarks = user.bookmarks
bookmarks.map { |bookmark| ::BookmarkSerializer.new(bookmark)}.to_json #updated line
end
to:
def index
user = User.find_by(username: params[:username])
bookmarks = user.bookmarks
render json: bookmarks.map { |bookmark| ::BookmarkSerializer.new(bookmark)}
end

Resources