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.
Related
I'm probably overlooking something simple here but I still haven't been able to find the problem.
Controller:
(First line is a long Thinking Sphinx query)
#artwork_q = Artwork.search params[:q], :conditions => {:subject => params[:artwork][:subject_ids], :location => params[:artwork][:location_ids], :artist_last => params[:artwork][:artist_id], :artist_first => params[:artwork][:artist_id]}, :order => params[:sort][:title]
#last_artwork = Artwork.new
#last_artwork = #artwork_q.last
render 'index2'
View:
<% #artwork_q.each.with_index do |art, index| %>
<div class="a-result<%= " last-one" if art.id == #last_artwork.id %>" id="<%= index %>">
<% end %>
I tested art.id and that's working fine. It's the #last_artwork.id that's raising the error which is this:
NoMethodError in Artworks#index2
Showing /Users/<user>/Developer/<project>/<rails_root>/app/views/artworks/index2.html.erb where line #49 raised:
undefined method `id' for nil:NilClass
Thank you for any help!
[].last => nil i.e. your query returns no results. Simply check #artwork_q has some results in before looping through them.
<% unless #artwork_q.blank? %>
<% #artwork_q.each.with_index do |art, index| %>
<div class="a-result<%= " last-one" if art.id == #last_artwork.id %>" id="<%= index %>">
<% end %>
<% end %>
Try this:
<% unless #artwork_q.blank? %>
<% #artwork_q.each.with_index do |art, index| %>
<% unless #last_artwork.blank? %>
<div class="a-result<%= " last-one" if art.id == #last_artwork.id %>" id="<%= index %>">
<% end %>
<% end %>
<% end %>
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.
How do I display products from the ShopSense API in Rails using shopsense-ruby?
JSON.parse(response)["products"] is returning nil:
NoMethodError in Main#index
Showing /home/dev/demo-shopsense/app/views/shared/_shopsense.html.erb where line #3 raised:
undefined method `any?' for nil:NilClass
Extracted source (around line #2):
<div class="custom_widget">
<% if #products.any %>
<% #products.each do |product| %>
<div class="product">
<%= image_tag product['image']['url'] %>
<p><%= product['name'] %></p>
main_controller.rb:
class MainController < ApplicationController
def index
client = Shopsense::API.new('partner_id' => 'uid0000-0000000-00')
response = client.get_looks("New")
#products = JSON.parse(response)["products"]
end
end
shared/_shopsense.html.erb:
<div class="custom_widget">
<% if #products.any? %>
<% #products.each do |product| %>
<div class="product">
<%= image_tag product['image']['url'] %>
<p><%= product['name'] %></p>
</div>
<% end %>
<% end %>
</div>
I've set up a simple demo app demo-shopsense that should be easy to get up and running.
The parsed response of your request looks like this:
{"totalCount"=>"536121",
"looks"=>
[{"id"=>"12328367",
"title"=>"black",
...
As you can see instead of 'products' we have 'looks'.
#products = JSON.parse(response)["looks"]
When you are working with external services, it's very important to find out the exact format of the response.
EDIT:
I think in this case you actually need to use 'search' method on the client instead of 'get_looks', if you want to get products.
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
This is my code in my model:
def to_param
require 'unicode'
"#{id}-#{Unicode::normalize_KD("-"+name+"-").downcase.gsub(/[^[:alnum:]]/,'-')}".gsub(/-{2,}/,'-')
end
I needed the unicode feature because in the name of my certain entries, it consists of accent (foreign characters). Without the unicode it was set to replace the accent character with a -.
But the problem right now is it gives me the following error:
ActionView::TemplateError (no such file to load -- unicode) on line #50 of app/views/spots/index.html.erb:
47:
48: <% #shops.each do |spot| %>
49: <div id="<%= dom_id(shop) %>" class="item">
50: <a href="<%= shop_path(shop, :type => #type) %>">
51: <% if !shop.photos.blank? %>
52: <%= image_tag(shop.photos.last.url(:thumb), :class => 'thumbnail') %>
53: <% else %>
What should I do? Thanks.
Try adding the unicode gem to your Gemfile:
gem 'unicode'