I have a problem while uploading a file under Mac. It was fine under windows dev environment, and ok under ubuntu prod, but throw this under Mac os 10.6.8 when I try to upload and parse a file:
Errno::ENOENT in ProductsController#import_adjust
No such file or directory - /var/folders/v8/v8lBbdWcGJmntpbJRzGcfU+++TI/-Tmp-/RackMultipart20120824-71301-1aqop0s
Here is a form:
<%= form_for :upload, :html => {:multipart => true}, :url => {action: "upload"} do |f| %>
<%= f.file_field :my_file %>
<%= f.submit "Upload" %>
<% end %>
And this is a code:
def import_adjust
case params[:commit]
when "Adjust"
#col_default = params[:col_data]
#abort #col_default.to_yaml
#update csv reader with form data, restore filters from params
when "Complete"
#all ok, read the whole file
#abort params.to_yaml
self.import_complete
return
else
#col_default = nil
end
#read first part of the file
#tmp = session[:import_file]
#csv = []
source = CSV.open #tmp, {col_sep: ";"}
5.times do
line = source.readline
if line.size>0
#line_size = line.size
#csv.push line
end
end
#generate a selection array
#selection = select_tag 'col_data[]', options_for_select([['name','name'], ['brand','brand'], ['delivery_time','delivery_time'], ['price','price']])
##csv = [selection * line_size] + #csv
end
Related
A controller extracts data from deeply nested associations where bilancino belongs_to :operativo which in turn belongs_to :cdg as such:
#cdgs = Cdg.order("id ASC").all
#bilancinos = Bilancino.joins(:operativo).order('cdg_id ASC').all
(with a where clause in there).
However when rendering
<% #cdgs.each do |cdg| %>
<% #cdgs_for_bilancino = #bilancinos.select{ |i| i.operativo.cdg_id == cdg } %>
<%= #cdgs_for_bilancino.each do |bilancino| %> XX <% end %>
<%= number_with_precision(#cdgs_for_bilancino.map(&:saldo).sum, :precision => 0, :delimiter => "\u00a0") %>
<% end %>
is generating an empty array, yet if underneath the following
<% #bilancinos.each do |bilancino| %>
<%= bilancino.operativo.cdg.conto %> <%= bilancino.saldo %><br />
<% end %>
will render. Thus the expression #bilancinos.select{ |i| i.operativo.cdg_id is missing the nested target somehow.
What is the proper syntax?
I suspect your issue is here:
#bilancinos.select{ |i| i.operativo.cdg_id == cdg }
For example, this is what I see in the console of one of my apps:
irb(main):022:0> recruiter = Recruiter.find_by_id(1)
Recruiter Load (0.9ms) SELECT "recruiters".* FROM "recruiters" WHERE "recruiters"."id" = $1 LIMIT 1 [["id", 1]]
=> #<Recruiter id: 1>
irb(main):023:0> ping = Ping.find_by_id(1)
Ping Load (0.9ms) SELECT "pings".* FROM "pings" WHERE "pings"."id" = $1 LIMIT 1 [["id", 1]]
=> #<Ping id: 1, recruiter_id: 1>
irb(main):024:0> recruiter.pings.select{ |p| p.id == ping }
Ping Load (0.5ms) SELECT "pings".* FROM "pings" WHERE "pings"."recruiter_id" = $1 [["recruiter_id", 1]]
=> []
irb(main):025:0> recruiter.pings.select{ |p| p.id == ping.id }
=> [#<Ping id: 1, recruiter_id: 1>]
So try:
#bilancinos.select{ |i| i.operativo.cdg_id == cdg.id }
This question already has an answer here:
Rails in rendering unnecessary information
(1 answer)
Closed 8 years ago.
I have a function in ruby
def words
ret =""
res = Net::HTTP.start(url.host, url.port) {|http|http.request(req)}
res.body.each_line do |line|
words = line.split("\"")
ret << words[1] << " "
end
return ret
end
say ret returns "Bill Dan Mike Sarah".
in my view I am doing
<%= #class.words.split(" ").each do |name| %>
<p><%= name %></p>
<% end %>
Instead of just displaying each name on a line, it does
Bill
Dan
Mike
Sarah
["Bill", "Dan", "Mike", "Sarah"] #this shouldn't be printed
What is causing it to display the entire array there at the end? How do I prevent this?
Remove an extra = from first line in each:
<% #class.words.split(" ").each do |name| %>
<p><%= name %></p>
<% end %>
I have a mobile application that makes a request to my Rails backend.
The backend JSON returns the information in the following manner:
def get_data
respond_to do |format|
format.json{
data = Form.select('column1, column2').where(field:'Rhodes')
render :json => data
}
end
end
In my application (Rhodes), I'm using Rho::AsyncHttp.post and send to my webview index the data:
response = Rho::AsyncHttp.post(
:url => 'http://xyx.xyx.x.yx:3000/data/get_data.json',
:headers => {"Content-Type" => "application/json; charset=utf-8", "Accept" => "application/json"}
)
WebView.navigate( url_for :action => :index , :query => { :form => response['body'] })
Now, when I inspect the variable form in my webview I get the data in the following structure:
<%= #form.inspect %>
"[{\"campo\"=>\"nombre\",\"tipo\"=>\"textfield\"},{\"campo\"=>\"nombre\",\"tipo\"=>\"textfield\"},{\"campo\"=>\"nombre\",\"tipo\"=>\"textfield\"}]"
So, I need to show this data in a Table and I want loop this. I tried with each:
<h2>Data</h2>
<% #form.each do |x| %>
<div><%= x.tipo %></div>
<% end %>
But I get the error: undefined method each for #.
So, I tried to convert it to a JSON object:
data = Rho::JSON.parse(#form)
But, I get the following error:
JSON error code: 10; Ofset: 9
Trace:
(eval):36:in ´parse´
(eval):36:in '´
lib/rho/render.rb:175:in ´eval_compiled_file´
lib/rho/render.rb:175:in ´render´ [...]
I'm using Ruby 1.9.3 with Rhodes 3.5.1.12
UPDATE
I fix the problem with the function eval. Now I can loop the string.
But when I try to print the variable. These not show.
<% data = eval(#form) %>
<% data.each do |hash| %>
<ul>
<li><%= puts hash['campo'] %></li> #only display the bullets but no the data
<li><%= puts hash['tipo'] %></li> #only display the bullets but no the data
</ul>
<br />
<% end %>
I don't use Rhodes, but...
You can't use JSON on this string:
"[{\"campo\"=>\"nombre\",\"tipo\"=>\"textfield\"},{\"campo\"=>\"nombre\",\"tipo\"=>\"textfield\"},{\"campo\"=>\"nombre\",\"tipo\"=>\"textfield\"}]"
because it isn't a JSON serialized object. The => are the giveaway. JSON serializes hashes using : to delimit the key/value pairs.
Using inspect is probably confusing you because it will turn what looks like an array of hashes into a pseudo-string. I suspect it's really this:
ary = [{"campo"=>"nombre","tipo"=>"textfield"},{"campo"=>"nombre","tipo"=>"textfield"},{"campo"=>"nombre","tipo"=>"textfield"}]
because if I inspect ary I get:
# => "[{\"campo\"=>\"nombre\", \"tipo\"=>\"textfield\"}, {\"campo\"=>\"nombre\", \"tipo\"=>\"textfield\"}, {\"campo\"=>\"nombre\", \"tipo\"=>\"textfield\"}]"
This is how to iterate over the array of hashes:
ary.each do |hash|
puts hash['tipo']
end
# >> textfield
# >> textfield
# >> textfield
In my Chef cookbook (for mailman), I want use an attribute that is a hash and contains all the configuration options I want to write into a template (similar to how it's done in the chef-client cookbook).
So I want to have in my attributes:
default[:myapp][:config] = {
"I_AM_A_STRING" => "fooo",
"I_AM_AN_INT" => 123,
"I_AM_AN_ARRAY" => ["abc", "cde"]
}
As output, I want the following (mailman compatible):
I_AM_A_STRING = 'fooo'
I_AM_AN_INT = 123
I_AM_AN_ARRAY = ['abc', 'cde']
However, it does not work.
To debug the problem, I have the following code in my erubis template:
<% node[:myapp][:config].each_pair do |key, value| -%>
<% case value.class %>
<% when Chef::Node::ImmutableArray %>
# <%= key %> is type <%= value.class %>, used when Chef::Node::ImmutableArray
<%= key %> = ['<%= value.join("','") %>']
<% when Fixnum %>
# <%= key %> is type <%= value.class %>, used when Fixnum
<%= key %> = <%= value %> #fixnum
<% when String %>
# <%= key %> is type <%= value.class %>, used when String
<%= key %> = '<%= value %>' #string
<% else %>
# <%= key %> is type <%= value.class %>, used else
<%= key %> = '<%= value %>'
<% end -%>
<% end -%>
So I want to use the case .. when to distinguish between value.class. However, none of the when conditions matches. So the output is:
# I_AM_A_STRING is type String, used else
I_AM_A_STRING = 'fooo'
# I_AM_AN_INT is type Fixnum, used else
I_AM_AN_INT = '123'
# I_AM_AN_ARRAY is type Chef::Node::ImmutableArray, used else
I_AM_AN_ARRAY = '["abc", "cde"]'
When I try when Whatever.class, the first when matches for all types.
What am I doing wrong?
Looks like you mean
<% case value %>
instead of
<% case value.class %>
That's how case..when works in ruby. It's often the case you need to switch based on value (or even a range of values ), and also on type.
It let's you do things like this in ruby (and hence also in erubis/eruby):
def f(x)
case x
when String then "#{x} is a String"
when 42 then "It's #{x==42}, x is 42"
when 21..27 then "#{x} is in 21..27"
when Fixnum then "#{x} is numeric, that's all I know"
else "Can't say."
end
end
f(1) # "1 is numeric, that's all I know"
f(25) # "25 is in 21..27"
f("hello") # "hello is a String"
f(false) # "Can't say."
I just installed redmine on ubunutu/nginx/postgres and I get this error when creating a new project... any ideas? (I'm new to ruby so can't really tell what it wants to happen)
Started GET "/projects/new" for 50.116.19.7 at Tue Aug 14 08:31:52 +0200 2012
Processing by ProjectsController#new as HTML
Rendered projects/_form.html.erb (4.2ms)
Rendered projects/new.html.erb within layouts/base (6.0ms)
Completed 500 Internal Server Error in 131ms
ActionView::Template::Error (undefined local variable or method `self_and_descendants' for #<Project:0xb490ca54>):
4: <!--[form:project]-->
5: <p><%= f.text_field :name, :required => true, :size => 60 %></p>
6:
7: <% unless #project.allowed_parents.compact.empty? %>
8: <p><%= label(:project, :parent_id, l(:field_parent)) %><%= parent_project_select_tag(#project) %></p>
9: <% end %>
10:
app/models/project.rb:333:in 'allowed_parents'
app/views/projects/_form.html.erb:7:in
'_app_views_projects__form_html_erb___577557764__627958918'
app/views/projects/new.html.erb:4:in '_app_views_projects_new_html_erb___126828522__630632748'
app/helpers/application_helper.rb:950:in `labelled_form_for'
app/views/projects/new.html.erb:3:in '_app_views_projects_new_html_erb___126828522__630632748'
from redmine/app/models/project.rb (sorry, cant seem to figure out how to propery format code to display here)
# Returns an array of projects the project can be moved to
# by the current user
def allowed_parents
return #allowed_parents if #allowed_parents
#allowed_parents = Project.find(:all, :conditions => Project.allowed_to_condition(User.current, :add_subprojects))
#allowed_parents = #allowed_parents - self_and_descendants
if User.current.allowed_to?(:add_project, nil, :global => true) || (!new_record? && parent.nil?)
#allowed_parents << nil
end
unless parent.nil? || #allowed_parents.empty? || #allowed_parents.include?(parent)
#allowed_parents << parent
end
#allowed_parents
end
# Sets the parent of the project with authorization check
def set_allowed_parent!(p)
unless p.nil? || p.is_a?(Project)
if p.to_s.blank?
p = nil
else
p = Project.find_by_id(p)
return false unless p
end
end
if p.nil?
if !new_record? && allowed_parents.empty?
return false
end
elsif !allowed_parents.include?(p)
return false
334,5 35%
elsif !allowed_parents.include?(p)
return false
end
set_parent!(p)
end
There is no function call self_and_descendants as you have declared in your Project model.
Use self.descendants
Check out this link Class descendants