delayed_job_active_record - Undefined method `any?' - ruby-on-rails

Attempting to send some stuff to the background with delayed_job_active_record, but how come I can't use any?? Works fine when things are not sent into the background.
undefined method `any?' for #<Delayed::Backend::ActiveRecord::Job:0x000000044b1348>
index.html.erb
<% if #products.any? %>
<div class="products">
<% #products.each do |product| %>
<%= product.name %>
<% end %>
</div>
<% else %>
<div class="products processing">
<!-- Try again later -->
</div>
<% end %>
main_controller.rb
class MainController < ApplicationController
def index
# Delay fetching
#products = Affiliate.delay.fetch
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)
affiliate = Affiliate.find_or_create_by(:name => product.name, :url => product.url)
affiliate.save
end
end
end

This doesn't work because #products isn't an array any more - it's a reference to a job that will get executed at some time in the future
You need a rethink of how your setup works. For example your view could poll via Ajax to see if the job has been completed. If it has then fetch the results from the DB, since you seem to be saving the API results there.

Related

uninitialized constant ActionView::CompiledTemplates::BlogPost

So I have the following in my blog_posts.rb model:
class BlogPost::Fetch < ActiveRecord::Base
API_URI = "http://blog.url.com/rss.xml".freeze
def class
posts = Rails.cache.fetch(API_URI, expires_in: 10.minutes) do
begin
feed = Feedjira::Feed.fetch_and_parse API_URI
feed.entries.take(2)
rescue
[]
end
end
posts
end
end
The following in my view helper:
module BlogHelper
def fetch_blog_posts(entries)
posts.each do |post|
unless exists? :guid => post.id
create!(
:title => post.title,
:pubDate => post.date
)
end
end
end
end
Then in my view:
<% BlogPost.fetch_blog_posts(2).each do |post| %>
<div class="home-blog-post">
<div class="news-date">
<%= post.display_date %>
With this present I end up with uninitialized constant ActionView::CompiledTemplates::BlogPost.
What am I missing? I feel like it's something simple that I'm just not seeing.
EDIT:
I have tried the following as well:
Added include ActiveRecord::Helpers in my model and changed the view to be BlogPost::Fetch. Same error
Put my helper method into the model and used both BlogPost::Fetch and BlogPost in the view. Same error for both.
Changed my model from inheriting from ActiveRecord::Base to Operator. Same error.
The only time I've gotten a different error is when I changed the model name to blog_post.rb and I get a circular error.
Another Edit:
I've pulled everything from the view helper as it's pointless. I'm not using a controller as it's in the CMS. So my model named fetch.rb now has the following:
class BlogPost::Fetch < ActiveRecord::Base
API_URI = 'http://blog.url.com/rss.xml'.freeze
def call
posts = Rails.cache.fetch(API_URI, expires_in: 10.minutes) do
begin
feed = Feedjira::Feed.fetch_and_parse API_URI
feed.entries.take(2)
rescue
[]
end
end
posts
end
def fetch_blog_posts
#posts = BlogPost::Fetch.run.result
end
end
Then in the view:
<% BlogPost.fetch_blog_posts(2).each do |post| %>
<div class="home-blog-post">
<div class="news-date">
<%= post.display_date %>
</div>
<%= link_to post.title, blog_post_path(post.url_name), class: 'news-title' %>
</div>
Still getting the same error.
So I tried changing my view to be:
<% #posts(2).each do |post| %>
<div class="home-blog-post">
<div class="news-date">
<%= post.display_date %>
</div>
<%= link_to post.title, blog_post_path(post.url_name), class: 'news-title' %>
</div>
In this case I end up with the error of: syntax error, unexpected '(', expecting keyword_end '.freeze; #posts(2).each do |post| ^
Took awhile but this worked. I changed the model to be:
class Fetch < ActiveRecord::Base
API_URI = 'http://blog.url.com/rss.xml'.freeze
def self.fetch_blog_posts(posts)
posts = Rails.cache.fetch(API_URI, expires_in: 10.minutes) do
begin
feed = Feedjira::Feed.fetch_and_parse API_URI
feed.entries.take(2)
rescue
[]
end
end
posts
end
end
Then updated the view to:
<% Fetch.fetch_blog_posts(2).each do |post| %>
<div class="home-blog-post">
<div class="news-date">
<%= post.published.strftime("%b %d, %Y") %>
</div>
<%= link_to post.title, post.url, class: 'news-title' %>

Undefined method `add' for Hash

I'm implementing this shopping cart gem, but having problems when I add the item to cart in the controller. I tried executing the code using rails console and it works fine. Not sure why I'm getting this error from the controller.
Error:
products_controller.rb:
helper_method :add_to_cart
def add_to_cart
#cart = session[:active_cart]
#product = Product.find(params[:product_id])
#cart.add(#product, 99.99)
end
routes.rb
post '/add_to_cart/:product_id' => 'products#add_to_cart', :as => 'add_to_cart'
views/products/index.html
<% products.each do |product| %>
<%= button_to "Add to Cart", add_to_cart_path(:product_id => product.id), :method => :post %>
<a href="<%= addresses_path(:brand => product.brand.id, :product_id => product.id) %>" class="list-group-item">
<%= image_tag product.image.url(:square), class: "product-list-group-item" %>
<%= product.name %>
<span class="badge">$<%= number_with_precision(product.price, precision: 2) %></span>
</a>
<% end %>
#cart = session[:active_cart] this is returning a hash and that's why you get the mentioned error when you call this:
#cart.add(#product, 99.99)
Because there is no add method implemented for hash object.
I suggest you to inspect the #cart object in your controller like this:
#cart = session[:active_cart]
puts #cart.inspect
puts #cart.class
and then you will see, it's a hash object and you should be able to extract the required cart object from that hash.
The main issue is to get the correct #cart object from the session. Once you do that, then, it should work :)

Rails 4 retrieve nested attributes for display in an index.html.erb

Just looking to find out how to retrieve nested images for display on my front page. I have no problems with a standard model but have been unable to find how to bring has_many nested attribute through. All my nested Forms work fine just have neglected the front end.
eg. product has nested product_images. This doesn't look like a clever way of doing it as the last five images uploaded wont necessarily be related to the last five products added.
Could someone please share an example.
cheers
app/controller/home_controller.rb
class HomeController < ApplicationController
def index
#products = Product.last(5)
#product_images = ProductImage.last(5)
end
end
app/views/home/index.html.erb
<% #products.each do |pd| %>
<div><%= pd.product_name %>
<% end %>
<% #product_images.each do |pd| %>
<%= image_tag (pd.product_image(:medium)) %>
<% end %>
</div>
You can try this:
app/controller/home_controller.rb
class HomeController < ApplicationController
def index
#products = Product.last(5)
#product_ids = #products.collect(:id)
#product_images = ProductImage.where(:id => #product_ids).last(5)
end
end
app/views/home/index.html.erb
<% #products.each do |pd| %>
<div><%= pd.product_name %>
<% end %>
<% #product_images.each do |pd| %>
<%= image_tag (pd.product_image(:medium)) %>
<% end %>

Link to remote destroy is not updating div

I created a view that show all my invoices products and also created a link that remove an invoice product from a div and it will be updated so everytime that I remove everything will be in the same page.
Here is the table
invoices
|id| |name|
1 ABC
2 DEF
invoice_products
|id| |invoice_id| |word|
1 1 AAAA
2 1 BBBB
3 2 CCCC
4 2 DDDD
Here is the controller:
def show
#invoice= Invoice.find(params[:id])
end
def destroy_job
#job = InvoiceProduct.find(params[:id])
#invoice = #job.invoice
#job.destroy()
render :partial=>"finance_management/invoice/partials/new_subjects" }
end
Here is the model
class Invoice < ActiveRecord::Base
has_many :invoice_products
end
class InvoiceProduct < ActiveRecord::Base
belongs_to :invoice
end
Here is the view: "show.html.erb"
<%= #invoice.id %>
<div id="table"%>
<% #invoice.invoice_products.each do |i| %>
<%= i.name %>
<%= i.word %>
<%=link_to_remote(image_tag("image.png"), :update => "table",:url => { :controller=>'finance_management/invoice_product',:action => 'destroy_job',:id=>i.id } )%>
<% end %>
</div>
The log:
ActionView::TemplateError (undefined method `invoice_products' for nil:NilClass)
I created "delete_job.js.erb":
$('table').html("<%= j(render partial: 'finance_management/invoice/partials/new_subjects') %>");
But I got this error:
NoMethodError (undefined method `invoice_products=' for nil:NilClass):
The problem is that is not updating the div seems because getting nil error
Somebody can help me please?
According to this you must do this:
The controller invoice_controller.rb:
def show
#invoice= Invoice.find(params[:id])
#products = InvoiceProduct.find(:all,:conditions=>['invoice_id = ?',params[:id] ])
end
def destroy_job
#job = InvoiceProduct.find(params[:id])
#obj_invoice = Invoice.find(#job.invoice_id)
#job.destroy
end
The view "invoice/show.html.erb"
<%= #invoice.id %>
<div id="table"%>
<%= render :partial=>"invoice/partials/products" %>
</div>
Don't forget to create the partial view "invoice/partials/_products.erb" this will replace the div
<% #products.each do |i| %>
<%= i.name %>
<%= i.word %>
<%=link_to_remote(image_tag("image.png"),:url=>{:controller=>'invoice',:action => 'destroy_job',:id=>i.id})%>
<% end %>
Finally create "invoice/destroy_job.rjs"
page.replace_html 'table', :partial=>'invoice/partials/products'
Create a file "destroy_job.js.erb" and place render method inside this file instead of calling it in controller:
$('YOUR_ID').html("<%= j(render partial: 'finance_management/invoice/partials/new_subjects') %>");

Passing local variable to partial inside for each loop rails 3

This is my code for rendering the partial (the #parties collection is being generated correctly, I have tested that):
<% #parties.each do |party| %>
<div class="item">
<%= render 'parties/party', :object => party %>
</div>
<% end %>
And this is the code in the partial:
<%= party.name %>
However, I get the following error:
undefined method `name' for nil:NilClass
I'm at my wits end, someone please help :-|
Also, this is the code for the controller to render the view containing the partial (The controller's called default_controller):
def index
#parties = Party.all
end
Is it of any consequence that this isn't the parties_controller?
I've tried something like below and it worked
<%= render :partial => 'party', :object => party %>
and I can access like party.name. the local variable is named after the partial name which is party here.
Note: Im assuming that your both partials are of parties_controller. So this should work.
Update:
Here is what ive tried with again
class PostsController < ApplicationController
#... ...
def index
#posts = Post.all
#comments = Comment.all #<---- Loading comments from PostsController
#... ...
end
end
#views/posts/index.html.erb
<% #comments.each do |comment| %>
<%= render :partial=>"comments/comment", :object=>comment %>
<% end %>
#views/comments/_comment.html.erb
<%= comment.body %>
And its working :)

Resources