Unicode in clean Rails app URL - ruby-on-rails

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'

Related

Rails link_to do block gives `unexpected keyword_do_block` error

I'm trying to use a link_to block in Rails 2.3.14 with Ruby 1.9.3, and it's causing
ActionView::TemplateError syntax error, unexpected keyword_do_block
HTML
<% #contents.each do |item| %>
<%= link_to "#" do %> # this is the line referenced in the error message
<%= content_tag(:h3, item.title) %>
<div class="details">
# divs with text
</div>
<% end %>
<% end %>
Detailed error message
The lines of code included in the error message do not perfectly match the code included above, which I simplified for debugging, but you can see that the line referenced is the same.
ActionView::TemplateError (/my/file/_partial.html.erb:78: syntax error, unexpected keyword_do_block
...;#output_buffer.safe_concat((do).to_s);#output_buffer.safe_c...
... ^
/my/file/_partial.html.erb:97: syntax error, unexpected keyword_else, expecting keyword_end
'); else
^
/my/file/_partial.html.erb:123: syntax error, unexpected keyword_ensure, expecting $end) on line #78 of app/views/content/_list.html.erb:
75: <%= hoverhide_image(item) %>
76: </div>
77: <div class="hovershow">
78: <%= link_to("#") do %>
79: <%= content_tag(:h3, item.title) %>
80: <div class="details">
81: <div>
If I replace the link_to block with a tags, it renders perfectly.
If I remove the = from line 3 (making it <% link_to "#" do %> instead), no error is thrown, but of course my text doesn't render.
Any idea what's going on?
(edited to add more details on the error message)

Pusher Chat Example Error

I am trying to work with tarnfeld's PusharChat-Rails
I downloaded the file, did the usual bundle install & db:migrate.
I changed the api key in PusherChat-Rails / app / views / layouts / application.html.erb
but I am getting this error
NoMethodError in Chat#view
Showing /Users/gkolan/work/PusherChat/app/views/chat/view.html.erb where line #22 raised:
undefined method `auto_link_urls' for #<#<Class:0x007fdddb772818>:0x007fdddb710230>
Extracted source (around line #22):
19: <ul id="messages">
20: <% #messages.each do |message| %>
21: <% user = ChatUser.find(message.user_id) %>
22: <li<% if user.id == #user.id %> class="you"<% end %>><strong><%= user.nickname %></strong> said:<br><%= auto_link_urls(message.message, { :target => "_blank" }) %></li>
23: <% end %>
24: </ul>
25: <div id="message-overlay"></div>
Rails.root: /Users/gkolan/work/PusherChat
I think its a very simple error. Could you please help me solve this?
Thanks in advance!
The auto_link_urls method has been deprecated for rails >= 3.1.0.
You may :
modify your Gemfile to use 3.0.9
add the rails_autolink gem to your Gemfile

NoMethodError in one of my models #show actions

I've got the following code in my show.html.erb file:
<p>
<b>Status:</b>
<% if #server.serverUp?.to_s == "Up" %>
<% #server.update_attribute(#server.serverStatus, 'Up') %>
<span style="color: green;"> <%= #server.serverUp? %></span>
<% else %>
<span style="color: #ff0000;"> <%= #server.serverUp? %></span>
<% end %>
</p>
My main concerns is with the line <% #server.update_attribute(#server.serverStatus, 'Up') %> which breaks my app from working. This line is supposed to save the status of the Server, whether it is "Up" or "Down", and save it in 'servers' table under the 'serverStatus' column.
However, when I go to http://localhost:3000/servers/id_of_server, where 'id_of_server' is a number from 1-300, the following error message appears:
NoMethodError in Servers#show
Showing C:/SIS/app/views/servers/show.html.erb where line #18 raised:
undefined method `=' for #<Server:0x60b9358>
Extracted source (around line #18):
15: <p>
16: <b>Status:</b>
17: <% if #server.serverUp?.to_s == "Up" %>
18: <% #server.update_attribute(#server.serverStatus, 'Up') %>
19: <span style="color: green;"> <%= #server.serverUp? %></span>
20: <% else %>
21: <span style="color: #ff0000;"> <%= #server.serverUp? %></span>
I don't seem to understand the error message, as I haven't used an equal sign in the extract of code provided. Any help would be appreciated.
If you guys need any more info let me know.
Thanks in advance
Edit: While I'm at it, I just wanted to ask whether I'm not following the RoR conventions by putting that type of code into the show.html.erb file as opposed to somewhere else? Because in the same file I also have another algorithm which reads all the attributes of a model, puts it into an array, and displays only the unique values.
Thanks
You should write:
<% #server.update_attribute(:serverStatus, 'Up') %>
And yes: you should not update models in view. In your case it could cleanly live in a before_save or why not in an after_initialize callback in the model I guess.
Lastly: in Ruby, convention is snake case so server_status instead of serverStatus

Ruby on Rails: hours worked this week

I have a "Log" project, each log has a :date, :hours, :description. I am simply trying to determine how many hours I have worked in a week, but am having trouble determining the proper separation of code. Let me know if any further code is needed. Rails 3.
log.rb
def self.days_in_range(from, to)
Log.where(:date => (from.to_date)..(to.to_date))
end
index.html.erb
<% content_for :sidebar do %>
<h4> Sidebar Content </h4>
<ul>
<li>Hours worked this week:
<%= Log.hours_this_week %> # unsure how to call
</li>
<li>Hours worked in total:
<%= Log.sum(:hours) %>
</li>
<li>Most hours worked in a day:
<%= Log.maximum(:hours) %>
</li>
</ul>
<% end %>
logs_helper.rb?
def hours_this_week
today = Time.now
day_of_week = today.wday
sunday = today - day_of_week.days
days = Log.days_in_range(today, sunday)
hours = 0
days.each do |day|
hours += day.hours
end
end
[solved] error
Showing /Users/***/Documents/workspace/***/hours_tracker/hours/app/views/logs/index.html.erb where line #33 raised:
undefined method `hours_this_week' for #<LogsController:0x103b66be8>
Extracted source (around line #33):
30: <h4> Sidebar Content </h4>
31: <ul>
32: <li>Hours worked this week:
33: <%= hours_this_week %>
34: </li>
35: <li>Hours worked in total:
36: <%= Log.sum(:hours) %>
Rails.root: /Users/***/Documents/workspace/***/hours_tracker/hours
full trace
[updated] new error
error
ArgumentError in Logs#index
Showing /Users/***/Documents/workspace/***/hours_tracker/hours/app/views/logs/index.html.erb where line #33 raised:
wrong number of arguments (0 for 1)
Extracted source (around line #33):
30: <h4> Sidebar Content </h4>
31: <ul>
32: <li>Hours worked this week:
33: <%= hours_this_week %>
34: </li>
35: <li>Hours worked in total:
36: <%= Log.sum(:hours) %>
Rails.root: /Users/***/Documents/workspace/***/hours_tracker/hours
This is tangential to your question, but I noticed this looking at your hours_this_week method. I could be wrong, but one little thing you may want to look at here is that your hours_this_week method is going to return the collection iterated through in your each statement (i.e. days), not the product of that statement (the new value of hours).
You could either just add the line:
hours
to the end of this method, or use inject instead of each:
# The initial "hours" declaration is no longer necessary,
# because inject returns its result, rather than the
# collection it is iterating through.
days.inject(0) {|hours, day| hours += day.hours }
That line would eliminate the need for your each statement and for explicitly returning hours at the end of your hours_this_week method.
That said, putting the method in logs_helper.rb and calling it with:
<%= hours_this_week %>
would be the way to go.
Assuming your index.html.erb file is in your app/views/logs folder, you can just call hours_this_week directly:
<%= hours_this_week %>

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.

Resources