I'm trying to in the "will_paginate" gem however when I push it to Heroku, I get the following error message on the Heroku server. The solution works perfectly in my development in environment.
2015-11-06T18:34:43.422821+00:00 app[web.1]: Processing by AccountsController#index as HTML
2015-11-06T18:34:43.509289+00:00 app[web.1]:
2015-11-06T18:34:43.509292+00:00 app[web.1]: NoMethodError (undefined method `paginate' for #<Account::ActiveRecord_Relation:0x007f39bc853bd8>):
2015-11-06T18:34:43.509293+00:00 app[web.1]: app/controllers/accounts_controller.rb:8:in `index'
2015-11-06T18:34:43.509294+00:00 app[web.1]:
2015-11-06T18:34:43.509295+00:00 app[web.1]:
Accounts Controller:
class AccountsController < ApplicationController
before_action :authenticate_user!
before_action :set_account, only: [:show, :edit, :update, :destroy]
respond_to :html
def index
#account = Account.all.order("created_at DESC").paginate(:page => params[:page], :per_page => 8)
end
def show
#notes = Note.where(account_id: #account.id) #Where a note belong to the current account
end
def new
#account = Account.new
respond_with(#account)
end
def edit
end
def create
#account = Account.new(account_params)
#account.save
respond_with(#account)
end
def update
#account.update(account_params)
respond_with(#account)
end
def destroy
#account.destroy
respond_with(#account)
end
private
def set_account
#account = Account.find(params[:id])
end
def account_params
params.require(:account).permit(:first_name, :last_name, :return_client, :program_id, :insurance_id, :address, :phone)
end
end
Index.html.erb
<br>
<%= will_paginate #account , renderer: BootstrapPagination::Rails %>
<br>
<%= link_to 'Add Client', new_account_path %>
Gem File
source 'http://rubygems.org'
ruby '2.1.5'
gem 'rails', '4.1.8'
gem 'sqlite3', group: :development
gem 'sass-rails', '~> 4.0.3'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'rails_12factor', group: :production
gem 'pg', group: :production
gem 'carrierwave'
gem "fog"
gem "figaro"
gem 'tzinfo-data', platforms: [:mingw, :mswin]
gem 'execjs'
gem "mini_magick"
gem 'devise'
gem 'searchkick'
group :development, :test do
gem 'rspec-rails', '~> 3.0'
gem 'will_paginate', '~> 3.0'
gem 'will_paginate-bootstrap'
end
Then gem env is the same on both servers. I'm not sure what the problem is.
Remove gem 'will_paginate', '~> 3.0' from group :development, :test do to gem 'will_paginate', '~> 3.0', group: :production and that corrected the NoMethodError message in the Heroku log.
Related
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
Help I dont know what Im doing wrong. I already installed the gems, put them in my gem file, did the bundle install, and restarted the server by using rails s. Why am I getting this error?
_form.html.erb:
<% simple_form_for #message do |f| %>
<%= f.input :title, label: "Message Title" %>
<%= f.input :description %>
<%= f.button :submit %>
<% end %>
messages_controller.rb:
class MessagesController < ApplicationController
def index
end
def new
#message = Message.new
end
def create
#message = Message.new(message_params)
end
private
def message_params
params.require(:message).permit(:title, :description)
end
end
gem file:
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
gem 'rails', '~> 5.1.4'
gem 'sqlite3'
gem 'puma', '~> 3.7'
gem 'sass-rails', '~> 5.0'
gem 'simple_form', '~> 3.5'
gem 'bootstrap-sass', '~> 3.3', '>= 3.3.7'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.2'
gem 'turbolinks', '~> 5'
gem 'jbuilder', '~> 2.5'
group :development, :test do
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
gem 'capybara', '~> 2.13'
gem 'selenium-webdriver'
end
group :development do
gem 'web-console', '>= 3.3.0'
end
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
Gem file :
source 'https://rubygems.org'
ruby "2.3.1"
gem 'rails', '4.2.3'
gem 'bootstrap-sass', '~> 3.3.6'
gem 'font-awesome-rails', '4.3.0.0'
gem 'sass-rails', '~> 5.0.4'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'turbolinks'
gem 'jbuilder', '~> 1.2'
group :doc do
gem 'sdoc', require: false
end
group :development, :test do
gem 'byebug', platform: :mri
gem 'sqlite3'
end
group :development do
gem 'web-console'
gem 'listen', '~> 3.0.5'
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
end
group :production do
gem 'unicorn'
gem 'pg'
gem 'rails_12factor'
end
gem 'simple_form', '~> 3.2', '>= 3.2.1'
gem 'haml', '~> 4.0', '>= 4.0.7'
Controller: (rails g controller Academy/Student)
class Academy::StudentController < ApplicationController
before_action :find_course, only: [:show, :edit, :update, :destroy]
def index
#academy_students = Academy::Student.all.order("created_at DESC")
# render json: Academy::Student.all
end
def new
#academy_student = Academy::Student.new
end
def show
# list = Academy::Student.find(params[:id])
# render json: list
end
def create
if #academy_student = Academy::Student.create(academy_student_params)
redirect_to #academy_student
else
render :new
end
end
def edit
end
def update
if #academy_student.update(academy_student_params)
redirect_to #academy_student
else
render :edit
end
end
def destroy
#academy_student.destroy
redirect_to #academy_student
end
private
def find_course
#academy_student = Academy::Student.find(params[:id])
end
def academy_student_params
params.require(:academy_student).permit(:student_code, :student_name, :student_gender, :student_email, :student_phone, :student_address, :student_degree, :student_grade)
end
end
routes :
namespace :academy do
resources :student
end
academy_student_index GET /academy/student(.:format) academy/student#index
POST /academy/student(.:format) academy/student#create
new_academy_student GET /academy/student/new(.:format) academy/student#new
edit_academy_student GET /academy/student/:id/edit(.:format) academy/student#edit
academy_student GET /academy/student/:id(.:format) academy/student#show
PATCH /academy/student/:id(.:format) academy/student#update
PUT /academy/student/:id(.:format) academy/student#update
DELETE /academy/student/:id(.:format) academy/student#destroy
_form.html.haml
= simple_form_for(#academy_student, html: { class: 'form-horizontal'}) do |f|
= f.error_notification
.form-inputs
= f.input :student_code, label: 'Student Code:'
= f.input :student_name, label: 'Student Name:'
= f.input :student_gender, label: 'Student Gender:'
= f.input :student_email, label: 'Student Email:'
= f.input :student_phone, label: 'Student Phone:'
= f.input :student_address, label: 'Student Address:'
= f.input :student_degree, label: 'Student Degree:'
= f.input :student_grade, label: 'Student Grade:'
= f.button :submit
Error:
NoMethodError in Academy::Student#new (when i try to run : http://localhost:3000/academy/student/new)
undefined method `academy_students_path' for #<#:0x007fd586ab8278> Did you mean? academy_student_path
academy_student_index_path
academy_student_url
academy_courses_path
Note:
This controller & model are generate without scaffold (when i use scaffold, its work)
Its working with update.
According to the naming conventions, controller name should be plural.
Which in your case is students_controller
rename student_controller.rb to students_controller.rb change your controller class name to
class Academy::StudentsController < ApplicationController
# ...
end
also, make changes in routes
routes.rb
resources :students
which will give you
academy_students GET /academy/students(.:format) academy/students#index
POST /academy/students(.:format) academy/students#create
new_academy_students GET /academy/students/new(.:format) academy/students#new
edit_academy_students GET /academy/students/:id/edit(.:format) academy/students#edit
academy_students GET /academy/students/:id(.:format) academy/students#show
PATCH /academy/students/:id(.:format) academy/students#update
PUT /academy/students/:id(.:format) academy/students#update
DELETE /academy/students/:id(.:format) academy/students#destroy
You are trying get academy/student#index
In your given routes details you should see there is no name of academy_students_path And for the academy/student#index you have academy_student_index_path.
Use academy_student_index_path instead academy_students_path
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
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]