I have two tables: Dimensions and Task. For each dimension have N task.
so in Task Controller i have this:
def new
#dimensions = Dimension.all
#dimensions.each do |dimension|
#task = Task.new
end
end
and the view Task this
<h1>Tasks#new</h1>
<%= form_for(#task) do |task| %>
<div class='service'>
<li class="col-md-3">
<div class="thumbnail">
<div class="caption">
<h4><%= task.name %></h4>
<p><%= task.description %></p>
</div>
<span>
</span>
</div>
</li>
</div>
<% end %>
but on the task view it shows me this error message
undefined method 'name'
undefined method 'descripcion'
try this,
def new
#dimensions = Dimension.all
#dimensions.each do |dimension|
#task = dimension.tasks.new //OR #task = dimension.build_tasks
end
end
Related
When I'm attempting to show a department view on my product#new page, this is my product#new page
<% #produto.errors.full_messages.each do |msg| %>
<div class="alert alert-danger" role="alert">
<%= msg %>
</div>
<% end %>
<%= form_for #produto do |produto| %>
<div class="row">
<div class="col-sm-3">
<div class="form-group">
<%= produto.label :nome %>
<%= produto.text_field :nome, class:"form-control" %>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-3">
<div class="form-group">
<%= produto.label :descricao %>
<%= produto.text_area :descricao, class:"form-control", rows:"4" %>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-2">
<div class="form-group">
<%= form.label :departamento %>
<%= form.collection_select :departamento_id, #departamentos, :id, :nome, {}, class:"form-control" %>
</div>
</div>
</div>
<%= produto.submit "Criar",class:"btn btn-success" %>
<% end %>
This is my product_controller.rb
class ProdutosController < ApplicationController
def index
#produtos = Produto.order(nome: :asc).limit 5
#produtos_com_desconto = Produto.order(:preco).limit 1
end
def new
#produto = Produto.new
#departamentos = Departamento.all
end
def create
valores_produto = params.require(:produto).permit(:nome, :descricao, :preco, :quantidade)
#produto = Produto.new valores_produto
#Produto.create valores_produto
if #produto.save
flash[:notice] = "Produto criado com sucesso!"
redirect_to root_url
else
render :new
end
end
def destroy
id = params[:id]
Produto.destroy id
redirect_to root_url
end
def search
#nome = params[:nome]
#produtos = Produto.where "nome like ?", "%#{#nome}%"
end
end
So, when i'm trying to access my product/new page , i'm receiving the error NameError in Produtos#new: undefined local variable or method `form' for #<#<Class.
<%= form.label :departamento %> is looking for the form variable but there is none defined. Instead <%= form_for #produto do |produto| %> has defined producto and uses it as <%= produto.label :nome %>. So you probably want <%= producto.label :departamento %>
Note the variable should probably be form. The variable is a FormBuilder, not a Produto. Calling it producto is confusing.
I've installed the Spree Multi-Vendor gem and have built my Vendor Show page, but am getting a NameError message from the cache_key_for_vendor_products line. Here is my Vendor Controller
module Spree
class VendorsController < Spree::StoreController
before_action :load_taxon, only: :show
def index
#vendors = Spree::Vendor.active
end
def show
#vendor = Spree::Vendor.active.friendly.find(params[:id])
#searcher = build_searcher(searcher_params)
#products = #searcher.retrieve_products.where(vendor_id: #vendor.id)
end
private
def load_taxon
#taxon ||= Spree::Taxon.friendly.find(params[:taxon_id]) if params[:taxon_id].present?
end
def searcher_params
searcher_params = params.merge(include_images: true)
searcher_params[:taxon] = load_taxon.id if load_taxon.present?
searcher_params
end
def vendor_params
params.require(:spree_vendor).permit(
*Spree::PermittedAttributes.vendor_attributes,
stock_locations_attributes: Spree::PermittedAttributes.stock_location_attributes
)
end
def vendor_user_params
params.require(:spree_vendor).require(:user).permit(Spree::PermittedAttributes.user_attributes)
end
def vendor_stock_location_params
params.require(:spree_vendor).require(:stock_location).permit(Spree::PermittedAttributes.stock_location_attributes)
end
end
end
My Vendor Show
<% description = simple_format(#vendor.about_us, class: 'mb-0') %>
<% short = truncate(description, separator: ' ', length: 200, escape: false) %>
<div class="container">
<div class="row px-lg-3 mt-3 vendor-details">
<div class="col-12 text-uppercase text-center mt-4 mb-5 mb-sm-4">
<h1 class="mt-2 mb-4"><%= #vendor.name %></h1>
</div>
<div class="col-12 col-sm-6 col-lg-5 text-center pt-sm-4 pt-lg-3 pb-sm-5 vendor-details-image">
<% if #vendor.image.present? %>
<%= image_tag main_app.url_for(#vendor.image.attachment), class: 'mw-100' %>
<% end %>
</div>
<div class="col-12 col-sm-6 col-lg-5 offset-lg-1 mt-5 mt-sm-0 d-flex flex-column justify-content-center vendor-description">
<div class="vendor-details-about">
<% if description == short %>
<%= description %>
<% else %>
<div class="js-readMore">
<%= tag.div class: "js-readMore-short" do %>
<%= short %>
<% end %>
<%= tag.div class: "js-readMore-full d-none" do %>
<%= description %>
<% end %>
</div>
<% end %>
</div>
</div>
</div>
<p><%= #vendor.contact_us %></p>
<hr/>
<div class="row">
<div class="col-12 mt-5 vendor-details-products">
<% cache(cache_key_for_vendor_products) do %>
<%= render template: #taxon ? 'spree/taxons/show' : 'spree/products/index' %>
<% end %>
</div>
</div>
</div>
And Multi Vendor Helpers located in lib/spree_multi_vendor/multi_vendor_helpers.rb
module SpreeMultiVendor
module MultiVendorHelpers
include ::Spree::ProductsHelper
include ::Spree::FrontendHelper
def cache_key_for_vendor_products
count = #products.count
max_updated_at = (#products.maximum(:updated_at) || Date.today).to_s(:number)
products_cache_keys = "spree/vendor-#{#vendor.slug}/products/all-#{params[:page]}-#{params[:sort_by]}-#{max_updated_at}-#{count}"
(common_product_cache_keys + [products_cache_keys]).compact.join('/')
end
end
end
I've gotten these codes here: https://github.com/spree-contrib/spree_multi_vendor/pull/120/files
On my vendor show page, I get this error and I can't for the life of me figure out what went wrong:
NameError in Spree::Vendors#show
undefined local variable or method `cache_key_for_vendor_products' for #<#Class:0x00007fc173a88478:0x00007fc176034b40>
Can any of you help?
ArticlesController
def home
#url = 'https://meta.discourse.org/latest.json'
#forum_data = HTTParty.get(#url).parsed_response
#topic_one = #forum_data['topic_list']['topics'][0]['title']
#topic_two = #forum_data['topic_list']['topics'][1]['title']
#topic_three = #forum_data['topic_list']['topics'][2]['title']
#topic_four = #forum_data['topic_list']['topics'][3]['title']
#topic_five = #forum_data['topic_list']['topics'][4]['title']
end
articles/home.html.erb
<div class="topics">
<ul>
<li>
<%= #topic_one %>
</li>
<li>
<%= #topic_two %>
</li>
<li>
<%= #topic_three %>
</li>
<li>
<%= #topic_four %>
</li>
<li>
<%= #topic_five %>
</li>
</ul>
</div>
I am struggling with how I can pass the integer into the object as a variable and I am also struggling with how I can create a method to move this into the model.
You can wrap it in a PORO:
class ForumTopicList
def initialize(discourse_forum_url)
#forum_url = discourse_forum_url
#forum_data = HTTParty.get(#url).parsed_response
end
def topics
#forum_data['topic_list']['topics'].map { |topic| ForumTopic.new(topic) }
end
class ForumTopic
def initialize(topic_hash)
#topic_hash = topic_hash
end
def title
#topic_hash['title']
end
end
end
This will allow you to reduce your controller to:
def home
url = 'https://meta.discourse.org/latest.json'
#topics = ForumTopicList.new(url).topics.first(5)
end
And your view can become
<div class="topics">
<ul>
<%= #topics.each do |topic|>
<li> <%= topic.title %> </li>
</ul>
</div>
i'm working on a Rails application with Spree Commerce.
I'm trying to display all products in a taxon cause i'm using them not as category but as some kind of registry.
So, i'm editing /app/views/spree/taxons/show.html.erb. Especifically this line:
<%= render partial: 'spree/shared/products', locals: { products: #products, taxon: #taxon } %>
Changing it for:
<%= render :partial => 'spree/shared/products', :locals => { :products => #products } %>
But it still says "No products found". But in my index they are showed.
So, How can I properly show all products?
Thanks in advance.
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux]
Rails 4.2.5
Spree 3.0.4
As requested:
1) This is the show from products
<% #body_id = 'product-details' %>
<% cache [I18n.locale, current_currency, #product, #product.possible_promotions] do %>
<div data-hook="product_show" itemscope itemtype="https://schema.org/Product">
<div class="col-md-4" data-hook="product_left_part">
<div data-hook="product_left_part_wrap">
<div id="product-images" data-hook="product_images">
<div id="main-image" class="panel panel-default" data-hook>
<div class="panel-body text-center">
<%= render :partial => 'image' %>
</div>
</div>
<div id="thumbnails" data-hook>
<%= render :partial => 'thumbnails' %>
</div>
</div>
<div data-hook="product_properties">
<%= render :partial => 'properties' %>
</div>
<div data-hook="promotions">
<%= render :partial => 'promotions' %>
</div>
</div>
</div>
<div class="col-md-8" data-hook="product_right_part">
<div data-hook="product_right_part_wrap">
<div id="product-description" data-hook="product_description">
<h1 class="product-title" itemprop="name"><%= #product.name %></h1>
<div class="well" itemprop="description" data-hook="description">
<%= product_description(#product) rescue Spree.t(:product_has_no_description) %>
</div>
<div id="cart-form" data-hook="cart_form">
<%= render :partial => 'cart_form' %>
</div>
</div>
<%= render :partial => 'taxons' %>
</div>
</div>
</div>
<% end %>
2) Show from taxons
<h1 class="taxon-title"><%= #taxon.name %></h1>
<% content_for :sidebar do %>
<div data-hook="taxon_sidebar_navigation">
<%= render partial: 'spree/shared/taxonomies' %>
<%= render partial: 'spree/shared/filters' if #taxon.leaf? %>
</div>
<% end %>
<div data-hook="taxon_products">
<%= render partial: 'spree/shared/products', locals: { products: #products, taxon: #taxon } %>
</div>
<% unless params[:keywords].present? %>
<div data-hook="taxon_children">
<% cache [I18n.locale, #taxon] do %>
<%= render partial: 'taxon', collection: #taxon.children %>
<% end %>
</div>
<% end %>
3) This is how I render in my index
<div data-hook="homepage_products">
<% cache(cache_key_for_products) do %>
<%= render :partial => 'spree/shared/products', :locals => { :products => #products } %>
<% end %>
</div>
4) _products.html.erb
<%
paginated_products = #searcher.retrieve_products if params.key?(:keywords)
paginated_products ||= products
%>
<% content_for :head do %>
<% if paginated_products.respond_to?(:num_pages) %>
<%= rel_next_prev_link_tags paginated_products %>
<% end %>
<% end %>
<div data-hook="products_search_results_heading">
<% if products.empty? %>
<div data-hook="products_search_results_heading_no_results_found">
<%= Spree.t(:no_products_found) %>
</div>
<% elsif params.key?(:keywords) %>
<div data-hook="products_search_results_heading_results_found">
<h6 class="search-results-title"><%= Spree.t(:search_results, keywords: h(params[:keywords])) %></h6>
</div>
<% end %>
</div>
<% if products.any? %>
<div id="products" class="row" data-hook>
<% products.each do |product| %>
<% url = spree.product_url(product, taxon_id: #taxon.try(:id)) %>
<div id="product_<%= product.id %>" class="col-md-3 col-sm-6 product-list-item" data-hook="products_list_item" itemscope itemtype="https://schema.org/Product">
<div class="panel panel-default">
<% cache(#taxon.present? ? [I18n.locale, current_currency, #taxon, product] : [I18n.locale, current_currency, product]) do %>
<div class="panel-body text-center product-body">
<%= link_to small_image(product, itemprop: "image"), url, itemprop: 'url' %><br/>
<%= link_to truncate(product.name, length: 50), url, class: 'info', itemprop: "name", title: product.name %>
</div>
<div class="panel-footer text-center">
<span itemprop="offers" itemscope itemtype="https://schema.org/Offer">
<span class="price selling lead" itemprop="price"><%= display_price(product) %></span>
</span>
</div>
<% end %>
</div>
</div>
<% end %>
<% reset_cycle("classes") %>
</div>
<% end %>
<% if paginated_products.respond_to?(:num_pages) %>
<%= paginate paginated_products, theme: 'twitter-bootstrap-3' %>
<% end %>
Controller index:
module Spree
class HomeController < Spree::StoreController
helper 'spree/products'
respond_to :html
def index
#searcher = build_searcher(params.merge(include_images: true))
#products = #searcher.retrieve_products
#taxonomies = Spree::Taxonomy.includes(root: :children)
end
end
end
Go to spree/frontend/app/controllers/spree/taxons_controller.rb
make the show method look like this
def show
#taxon = Taxon.friendly.find(params[:id])
return unless #taxon
#searcher = build_searcher(params.merge(include_images: true))
#products = #searcher.retrieve_products
#taxonomies = Spree::Taxonomy.includes(root: :children)
end
First of, your file should be called _products.html.erb.
But it still says "No products found"
Second, could you please show us this file?
But in my index they are showed.
Could you also show us how you render your products in your index?
EDIT: It seems like in Taxons#show, the #products instance variable is not set. Check if you set it in the show action in your TaxonsController.
Actually I am looking for an answer to "No products found"
Here is my question link:
Spree Commerce: No Products Found
Spree Commerce: No Products Found
I have just joined stackoverflow.com a couple of minutes, can't add a comment to above answers.
Here is /controllers/spree/taxons_controller.rb:
module Spree
class TaxonsController < Spree::StoreController
rescue_from ActiveRecord::RecordNotFound, with: :render_404
helper 'spree/products'
respond_to :html
def show
#taxon = Taxon.friendly.find(params[:id])
return unless #taxon
#searcher = build_searcher(params.merge(taxon: #taxon.id, include_images: true))
sorting_scope = params[:order].try(:to_sym) || :ascend_by_updated_at
#taxonomies = Spree::Taxonomy.includes(root: :children)
#searcher.retrieve_products
#products = #searcher.retrieve_products
#curr_page = params[:page] || 1
#products_per_page = Spree::Config.products_per_page
#start_no = #curr_page == 1 ? 1 : ((#curr_page.to_i-1)*#products_per_page)+1
#end_no = #products.length
if #curr_page.to_i > 1
#end_no = ( (#curr_page.to_i-1)*#products_per_page) + #end_no
end
# #end_no = #product_count <= #product_count ? #product_count : #products.length+(#curr_page.to_i-1)*#products_per_page
end
private
def accurate_title
#taxon.try(:seo_title) || super
end
end
end
I have the following Code on a partial that dynamically generates a list of associated steps to a pin. Both pins and steps have image(s). Users are able to add multiple steps to a pin and then this view dynamically generates a view of these steps. For each step there are associated image(s) which I'd like to pop-up as a modal. The challenge is I keep getting the following error:
"undefined local variable or method `step' for #<#:0x00000102f498d8>"
When I stub out rendering the partial, the modal appears basically blank but works. Any ideas How do I do this?
<div class="col-md-6">
<% (0..(Step.where("pin_id = ?", params[:id]).count)-1).each do |step| %>
<div class="col-md-12">
<div class="well">
<ul class="nav pull-right">
<% if current_user == #pin.user %>
<%= link_to edit_step_path((Step.where("pin_id = ?", params[:id]).fetch(step)), :pin_id => #pin.id) do %>
<span class="glyphicon glyphicon-edit"></span>
Edit
<% end %> |
<%= link_to Step.where("pin_id = ?", params[:id]).fetch(step), method: :delete, data: { confirm: 'Are you sure?' } do %>
<span class="glyphicon glyphicon-trash"></span>
Delete
<% end %> |
<%= link_to new_step_image_path(:step_id => Step.where("pin_id = ?", params[:id]).fetch(step), :pin_id => #pin.id) do %>
Add
<span class="glyphicon glyphicon-picture"></span>
<% end %>
<% end %>
<% if StepImage.where("step_id = ?", Step.where("pin_id = ?", params[:id]).pluck(:id).fetch(step)).count == 0 %>
<% else %>
| <a href="#StepImageModal" data-toggle="modal">
<span class="glyphicon glyphicon-picture"></span> (<%= StepImage.where("step_id = ?", Step.where("pin_id = ?", params[:id]).pluck(:id).fetch(step)).count %> ) </strong>
</a>
<% end %>
</ul>
<strong> Step <%= step+1 %>
<p>
<%= Step.where("pin_id = ?", params[:id]).pluck(:description).fetch(step) %>
</p>
</div>
</div>
<%end %>
</div>
<div class="modal fade" id="StepImageModal">
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h3>Modal Header</h3>
</div>
<div class="modal-body">
<%= render :partial => 'pins/step_images',
:locals => { :step => step } %>
</div>
<div class="modal-footer">
Close
</div>
</div>
I don't get what you're trying to do here
Loop
As mentioned in the comments, you've got a loop which goes through your steps. However, outside the loop, you want to display the partial
The solution to your woes will either be to set an instance variable (not desired), or use another persistent data type. I'd do this:
#app/controllers/steps_controller.rb
def action
#step = {}
end
#app/views/steps/action.html.erb
<% (0..(Step.where("pin_id = ?", params[:id]).count)-1).each do |step| %>
<% #step[step.id.to_sym] = step.details %>
<% end %>
<%= render :partial => 'pins/step_images', :locals => { :step => #step } %>
unless
Some refactoring for you:
<% unless StepImage.where("step_id = ?", Step.where("pin_id = ?", params[:id]).pluck(:id).fetch(step)).count == 0 %>
| <%= link_to "#StepImageModal", data: { toggle: "modal"} do %>
<span class="glyphicon glyphicon-picture"></span>(<%= StepImage.where("step_id = ?", Step.where("pin_id = ?", params[:id]).pluck(:id).fetch(step)).count %> ) </strong>
<% end %>
<% end %>