Spree validate field - ruby-on-rails

I am using Spree for an e-commerce app and I'm trying to validate uniqueness of sku
Spree::Variant.class_eval do
attr_accessible :sku
validates_uniqueness_of :sku
end
Then when I want to create a new record I get this error:
undefined method `price' for nil:NilClass
Extracted source (around line #7):
4: <% content_for :sidebar do %>
5:
6: <h3>
7: <%= #product.name %><span class="sku"><%= #product.sku %></span>
8: </h3>
9: <br class="clear"><ul class="sidebar product-menu" data-hook="admin_product_tabs">
10: <li <%== ' class="active"' if current == 'Product Details' %>>

After almost a year...
You need to write a custom validation in model with something like this
validate :validate_sku, :on => :create
private
def validate_sku
if Spree::Variant.exists?(:sku => sku)
errors.add(:sku, "SKU kod je již používán")
end
end

Related

Catching an exception while using refile gem when the image isn't available

Inside of my console, it shows username#email.com.attacher_. I'm pretty sure the refile isn't working because the image for the user is not there. But, how would I set it to process the method block when file is nil?
module MessagesHelper
def recipients_options
s = ''
users = User.all + BizUser.all; users.each do |user|
s << "<option value='#{user.id}'
data-img-src='#{attachment_url(#user, user.email, :profile_avatar,
:fill, 50, 50)}'>#{user.username}</option>"
end
s.html_safe
end
end
Here is the error showing in my log file
NoMethodError in Messages#new
ActionView::Template::Error (undefined method `profile_avatar_attacher' for nil:NilClass):
9: <div class="row">
10: <div class="large-5 medium-5 small-4 columns" style="padding-bottom: 10px;">
11: <%= label_tag 'recipients', 'Choose recipients' %>
12: <%= select_tag 'recipients', recipients_options, multiple: true, class: 'form-control chosen-it' %>
13: </div>
14: </div>
15: <div class="row">
app/helpers/messages_helper.rb:6:in `block in recipients_options'
module MessagesHelper
def recipients_options
s = ''
users = User.all + BizUser.all; users.each do |user|
s << "<option value='#{user.id}' data-img-src='#{attachment_url(#user, :profile_avatar, :fill, 30, 30)}'>#{user.username}</option>"
end
s.html_safe
end
end
ApplicationHelper for avatar method
def avatar_for_user
image_tag attachment_url(#user, :profile_avatar, :fill, 30, 30)
end
I'm receiving an error in the participants.html.erb partial as well
<% conversation.participants.each do |participant| %>
<% unless participant == current_user %>
<%= avatar_for_user participant %>
<% end %>
<% end %>
You are mixing up the user local variable and the #user instance variable.
In recipients_options you call
attachment_url(#user :profile_avatar, :fill, 30, 30)
You're rendering the same attachment (the one for #user) for every option.
I suspect you want to call
attachment_url(user, :profile_avatar, :fill, 30, 30)
Thus rendering the attachment for each user.
The reason that you're getting the NoMethod-error seems to be that the #user variable is nil.
Also, avatar_for_user seems strange. You define it with no argument but pass it a user in the partial.

RoR - ActionView Template Error

I'm trying to implement new redmine and then migrate my data from old redmine to new redmine.
But there is an template error showing in the log
ActionView::Template::Error (undefined method `is_closed?' for nil:NilClass):
6: <h3><%= format_activity_day(day) %></h3>
7: <dl>
8: <% sort_activity_events(#events_by_day[day]).each do |e, in_group| -%>
9: <dt class="<%= e.event_type %> <%= "grouped" if in_group %> <%= User.current.logged? && e.respond_to?(:event_author) && User.current == e.event_author ? 'me' : nil %>">
10: <%= avatar(e.event_author, :size => "24") if e.respond_to?(:event_author) %>
11: <span class="time"><%= format_time(e.event_datetime, false) %></span>
12: <%= content_tag('span', h(e.project), :class => 'project') if #project.nil? || #project != e.project %>
app/models/issue.rb:677:in `closed?'
app/models/issue.rb:57:in `block in <class:Issue>'
I guess this monday is occured when the css is rendered.
Is there any idea about this problem?
or how to log and find the real cause of the problem?
Thanks

passing an object to render( :template => "template.json.rabl") in RoR 3

I need to embed json into html and in the #322 RailsCasts says the way to do it using RABL...
app/views/places/show.html.erb
<div id="place" data-place="<%= render(template: "places/show.json.rabl") %>" >
here is my rabl template
app/views/places/show.json.rabl
object #place
attributes :id, :name, :desc, :address
node(:type) { |place| place.type_place.name }
child :photos do
attributes :id, :desc
node(:url) { |photo| photo.image.url(:square) }
end
and It is working fine but I want to render the rabl-template in other view like:
app/views/places/finder.html.erb
<%= #places.each do |place| %>
<div id="place-<%= place.id %>" data-place="<%= render(template: "places/show.json.rabl") %>" >
<% end %>
it shows me the next message error
undefined method `type_place' for nil:NilClass
Extracted source (around line #1):
1: object #place
2: attributes :id, :name, :desc, :address
3:
4: node(:type) { |place| place.type_place.name }
from the message I think the error is what I'm not passing the object place to the template... I was trying a lot and I didn't get this.
Thanks in advance, and sorry for my english
You need to explicitly call Rabl::Renderer
<%= #places.each do |place| %>
<div id="place-<%= place.id %>" data-place="<%= Rabl::Renderer.json(place, 'places/show', view_path: 'app/views') %>" >
<% end %>

Changing to_param yields error in "model"_url

Rails 2.3.8.
Here's the to_param in my shop model:
def to_param
require 'unicode'
"#{id}-#{Unicode::normalize_KD("-"+name+"-").downcase.gsub(/[^a-z0-9\s_-]+/,'').gsub(/[\s_-]+/,'-')[0..-2]}".gsub(/-{2,}/,'-')
end
When I tried to change #{id}- to #{id}/ the following:
def to_param
require 'unicode'
"#{id}/#{Unicode::normalize_KD("-"+name+"-").downcase.gsub(/[^a-z0-9\s_-]+/,'').gsub(/[\s_-]+/,'-')[0..-2]}".gsub(/-{2,}/,'-')
end
I get the following error in my index.html.erb:
shop_url failed to generate from {:type=>"places", :action=>"show", :controller=>"shops", :id=>#<shop id: 16, shop_type: "fashion", name: "Shop1", shop_subtype: nil, ...}
Extracted source (around line #54):
51:
52: <% #shops.each do |shop| %>
53: <div id="<%= dom_id(shop) %>" class="item">
54: <a href="<%= shop_path(shop, :type => #type) %>">
55: <% if !shop.photos.blank? %>
56: <%= image_tag(shop.photos.last.url(:thumb), :class => 'thumbnail') %>
57: <% else %>
I am trying to change the URL from shops/12-shop-abc to shops/12/shop-abc. In fact, I am actually trying to change to shops/shop-abc using friendly_id, but it fails on both.
Please help. Thanks.
The . in the name yields the error for normal to_param. It's solved with friendly_id.

Ruby and Nil Object

I am developing a webapp (well trying to) in Ruby on Rails.
I have a partial render in my index.html.erb
<%= render :partial => "houses/index", :locals => #houses %>
index.html.erb is loaded when the user hits the root of my domain. This partial cause this error:
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each
Extracted Source around line 8
5: <th>Washrooms</th>
6: </tr>
7:
8: <% #houses.each do |house| %>
9: <tr>
10: <td><%=h house.short_desc %></td>
11: <td><%=h house.bedrooms %></td>
Trace of template inclusion: app/views/home/index.html.erb
All I would like to is display 5 houses on my Homepage (index.html.erb)
What am I missing to get this to work?
Many thanks
EDIT:
Houses_controller
NoMethodError in Home#index
Showing app/views/houses/_index.html.erb where line #10 raised:
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each
Extracted source (around line #10):
7: Washrooms
8:
9:
10: <% #houses.each do |house| %>
11:
12: <%=h house.short_desc %>
13: <%=h house.bedrooms %>
Trace of template inclusion: app/views/home/index.html.erb
The value of :locals is supposed to be a Hash that defines local variables. So
<%= render :partial => "houses/index", :locals => {:houses => #houses} %>
and then
<% houses.each do |house| %>
To reiterate, since the new error you posted shows that you are still not doing it correctly:
If you're passing it through :locals, you want houses.each, not **#**houses.each. No snail. Local variables don't have a # prefix; instance variables do.
Locals takes a hash of local variables, like so:
<%= render :partial => "houses/index", :locals => { :houses => #houses } %>
Also, when you pass in a local variable to a partial, you should use it as a local variable rather than an instance variable within the partial, as so:
<% houses.each do |house| %>
# do stuff with each house
<% end %>
You don't need to pass it in :locals if it's an instance variable. Instance variables are available in all partials.
In your view:
<% #houses.each do |house| %>
# do stuff with each house
<% end %>

Resources