Missing something fundamental here. Getting undefined method 'items_loaded' when trying to check if REST Client is done fetching some items from this API.
Live app which you can run on the fly: http://runnable.com/VW9rQx-KiIFfmpII/ajax-affiliates
Started GET "/check_items_loaded" at 2015-06-04 17:03:44 +0000
Processing by MainController#check_items_loaded as */*
Completed 500 Internal Server Error in 2ms (ActiveRecord: 0.0ms)
NoMethodError (undefined method `items_loaded' for #<Class:0x00000004e694a8>):
app/controllers/main_controller.rb:12:in `check_items_loaded'
main_controller.rb
class MainController < ApplicationController
def index
# Delay fetching
# #products = Affiliate.fetch
#products = Affiliate.delay.fetch
# Let us know when fetching is done
Affiliate.set_items_loaded
end
def check_items_loaded
#items_status = Affiliate.items_loaded
respond_to do |wants|
wants.js
end
end
end
affiliate.rb
require "rest_client"
class Affiliate < ActiveRecord::Base
def self.fetch
response = RestClient::Request.execute(
:method => :get,
:url => "http://api.shopstyle.com/api/v2/products?pid=uid7849-6112293-28&fts=women&offset=0&limit=10"
)
#products = JSON.parse(response)["products"].map do |product|
product = OpenStruct.new(product)
product
end
end
def self.set_items_loaded
self.update_all(items_loaded: true)
end
end
routes.rb
get '/check_items_loaded', to: 'main#check_items_loaded', as: :check_items_loaded
20150604120114_add_items_loaded_to_affiliates.rb
class AddItemsLoadedToAffiliates < ActiveRecord::Migration
def self.up
change_table :affiliates do |t|
t.column :items_loaded, :boolean, default: false
end
end
def self.down
change_table :affiliates do |t|
t.remove :items_loaded
end
end
end
#items_status = Affiliate.items_loaded
items_loaded is an instance method, not a class method.
Related
I'm trying to access the 'home' page of my Rails app by going to localhost:3000/home
When I try the link above, however, I get the below error message --- however this doesn't make sense to me because 'category' is just a parameter for an order. Order has a category, store, items and other parameters.
My code is below. What am I doing wrong here?
Showing /Users/fk/tenence_ai/app/views/tenence/home.html.erb where line #49 raised:
undefined method `category' for #<Order:0x007f8ed99c8500>
Extracted source (around line #49):
47 <div>
48 <%= form_for :order, url: orders_path do |f| %>
49 <%= f.text_field :category,:id=> 'category',:style=>'display:none' %>
ORDERS CONTROLLER
class OrdersController < ApplicationController
respond_to :html, :json
def show
#order = Order.find(params[:id])
#idx = Order.last.id
render json: #order
end
def create
#order = Order.new(order_params)
#order.save
end
def edit
#order = Order.find(params[:id])
end
def update
#order = Order.find(params[:id])
#order.update(order_params)
end
respond_to :html, :json
private
def order_params
params.require(:order).permit(:address,:store,:name,:items,:category,:status,:total,{:item => []},{:price => []})
end
end
ORDER MODEL
class Order < ApplicationRecord
end
RELEVANT MIGRATION
class CreateOrders < ActiveRecord::Migration[5.0]
def change
create_table :orders do |t|
t.string :name
t.text :address
t.string :store
t.text :items
t.timestamps
end
end
end
It seems that you want to add category field:
Adding in params.require is good but you also need to add attr_accessor :category in your model.
class Order < ApplicationRecord
attr_accessor :category
end
attr_accessor can be used for values you don't want to store in the database directly and that will only exist for the life of the object.
Well I got another error now with my database this time saying one of the classes I made "Parts" is not a class. I can't seem to know where this is tracing from
and here is my Parts database values (the file name is parts.rb)
class Parts < ActiveRecord::Migration
def change
create_table :parts do |t|
t.string :name
t.text :description
t.integer :category_id
end
end
end
my parts controller:
class PartsController < ApplicationController
before_filter :authorize, :except => :index
def index
#parts = Part.all
end
def new
#part = Part.new
end
def show
#part = Part.find(params[:id])
end
def create
#part = Part.new(part_params)
if #part.save
redirect_to part_path(#part)
end
end
def edit
#part = Part.find(params[:id])
end
def update
#part = Part.find(params[:id])
if #part.update_attributes(part_params)
redirect_to #part
end
end
def destroy
#part = Part.find(params[:id])
#part.destroy
redirect_to parts_path
end
private
def part_params
params.require(:part).permit(:description, :name)
end
end
my parts model is just
class Part < ActiveRecord::Base
end
Thanks for any help
It may just not like 'Parts' as a class name conflicting with something in the model or controller. or you have a Parts module defined somewhere?
I suggest changing the migration class name to, say, CreateParts, ie.
def CreateParts < ActiveRecord::Migration
def change
…
end
end
And I'd change the filename too just in case (2016…09_create_parts.rb)
Hope you generate the migration file with model by command like
bundle exec rails g model Part name:string description:text category_id:integer
it will create the migration file 2016...09_create_parts.rb
and it will be look like
def CreateParts < ActiveRecord::Migration
def change
create_table :parts do |t|
t.string :name
t.text :description
t.integer :category_id
end
end
end
I want to implement koala gem in rails app
But i am getting an error "undefined method get_access_token'
My facebook controller code is
class FacebookController < ApplicationController
before_filter :authenticate_user!
def index
unless current_user.facebook_oauth_setting
#oauth = Koala::Facebook::OAuth.new("app-id", "secret", "http://#{request.host}:#{request.port}/callback")
session["oauth_obj"] = #oauth
redirect_to #oauth.url_for_oauth_code
else
redirect_to "/facebook_profile"
end
end
def callback
unless current_user.facebook_oauth_setting
#oauth = session["oauth_obj"]
Rails.logger.info("**************#{#oauth}***************")
Rails.logger.info("**********#{params[:code]}*************")
FacebookOauthSetting.create({:access_token => #oauth.get_access_token(params[:code]), :user_id => current_user.id})
redirect_to "/facebook_profile"
else
redirect_to "/"
end
end
def facebook_profile
if current_user.facebook_oauth_setting
#graph = Koala::Facebook::API.new(current_user.facebook_oauth_setting.access_token)
#profile = #graph.get_object("me")
#picture = #graph.get_picture("me")
#feed = #graph.get_connections("me","feed")
#friends = #graph.get_connections("me", "friends")
else
redirect_to "/"
end
end
end
My model to store access token is
class TwitterOauthSetting < ActiveRecord::Base
belongs_to :user
end
My migration to store access_token is
class CreateFacebookOauthSettings < ActiveRecord::Migration
def change
create_table :facebook_oauth_settings do |t|
t.string :access_token
t.integer :user_id
t.timestamps null: false
end
end
end
Create the #oauth object inside the callback method rather than bringing it from the session. So do this:
#oauth = Koala::Facebook::OAuth.new("app-id", "secret", "http://#{request.host}:#{request.port}/callback")
FacebookOauthSetting.create(:access_token => #oauth.get_access_token(params[:code]), :user_id => current_user.id)
It should solve your problem.
type of object should be Koala::Facebook
Missing something fundamental here. Unable to update items_loaded once REST Client is done fetching some items from this API.
Live app which you can run on the fly: http://runnable.com/VW9rQx-KiIFfmpII/ajax-affiliates
undefined method `items_loaded=' for #<Class:0x000000037cce20>
app/models/affiliate.rb:17:in `set_items_loaded'
app/controllers/main_controller.rb:8:in `index'
main_controller.rb
class MainController < ApplicationController
def index
# Delay fetching
# #products = Affiliate.fetch
#products = Affiliate.delay.fetch
# Let us know when fetching is done
Affiliate.set_items_loaded
end
def check_items_loaded
#items_status = Affiliate.items_loaded
respond_to do |wants|
wants.js
end
end
end
affiliate.rb
require "rest_client"
class Affiliate < ActiveRecord::Base
def self.fetch
response = RestClient::Request.execute(
:method => :get,
:url => "http://api.shopstyle.com/api/v2/products?pid=uid7849-6112293-28&fts=women&offset=0&limit=10"
)
#products = JSON.parse(response)["products"].map do |product|
product = OpenStruct.new(product)
product
end
end
def self.set_items_loaded
self.items_loaded = true
end
end
20150604120114_add_items_loaded_to_affiliates.rb
class AddItemsLoadedToAffiliates < ActiveRecord::Migration
def self.up
change_table :affiliates do |t|
t.column :items_loaded, :boolean, default: false
end
end
def self.down
change_table :affiliates do |t|
t.remove :items_loaded
end
end
end
Actually, in your class Affiliate, you defined the method self.set_items_loaded which get all Affiliate object and set attribute items_loaded to true on each object of this class.
If you really want to do that, you should write that
affiliate.rb
def self.set_items_loaded
self.update_all(items_loaded: true)
end
main_controller.rb
Affiliate.set_items_loaded
If you just want to update one object of Affiliate to set item_loaded to true, you should define your method that way and use it on one object
affiliate.rb
def set_items_loaded
self.items_loaded = true
end
main_controller.rb
Affiliate.first.set_items_loaded # to get the first object of Affiliate updated
/config/routes.rb
Rails.application.routes.draw do
devise_for :users
root 'posts#hello'
resources :users
resources :posts
end
/app/controllers/posts_controller.rb
class PostsController < ApplicationController
before_filter :authenticate_user! , except: [:hello]
def hello
end
def new
end
def create
#post = Post.create(inner)
redirect_to post_path(#post.id)
end
def index
#posts = Post.all.order('id desc')
end
def show
#post = Post.find_by(id: params[:id])
end
private
def inner
params.require(:post).permit(:title, :desc)
end
end
When I click posts_path link, I get an error: uninitialized constant PostsController::Post app/controllers/posts_controller.rb, line 17
def index
#posts = Post.all.order('id desc')
end
Whats wrong?
UPD
class Post < ActiveRecord::Base
belongs_to :user
validates :desc, presence: true
validates :title, presence: true, length: { maximum: 45 }
end
UPD2
class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
t.string :title
t.string :desc
t.timestamps
end
end
end
As per Rails convention model names should be singular. So, model Post would be located under folder app/models and named post.rb and NOT Posts.rb. You are getting the error because Rails would look for a file named post.rb by convention and if not found it throws the error.