Sinatra haml Select and Delete several files - ruby

I'm trying to delete several files based on a list but I'm having problems getting the params from the chekcbox's
This is my list.haml:
%form(method="post" action="/selection" enctype="multipart/form-data")
- #files.each do |file|
%br
%input{:type => "checkbox", :name => "checkbox[]", :value => "#{file}" }
=file
%br
%input(type='submit' value="Delete Selected Files")
Now, for now I was just trying to see what I get in params, so I can later deal with how to delete this list of files.
params.inspect
"Gives me" ≃> {"checkbox"=>["yet_another_file.txt", "file1", "file2"]}
But I can't figure out how do I put this into an Array so I can do something like
var.each do |c|
puts c
end
I tried var = params[:checkbox] but var is empty, does anyone now how can I do this?
Thanks

You should use var = params["checkbox"], since the params key is not a symbol, but a string.

Related

Ruby DataMapper::ImmutableError

get '/watch/:id' do |id|
#results = Twitchtvst.all( :fields => [:Twitchtv ],
:conditions => { :user_id => "#{id}" }
)
#p #results.inspect
#results.each do |result|
puts result.id
end
erb :mystream
end
I get this error message immutable resource cannot be lazy loaded. How do I fix this?
The Error message is:
DataMapper::ImmutableError at /watch/1
Immutable resource cannot be lazy loaded
According to the official documentation:
Note that if you don't include the primary key in the selected columns, you will not be able to modify the returned resources because DataMapper cannot know how to persist them. DataMapper will raise DataMapper::ImmutableError if you're trying to do so nevertheless.
I know that you are not modifying anything here but I think that the same rule applies for lazy loading. So I will suggest to try it like that:
#results = Twitchtvst.all( :fields => [:Twitchtv, :id],
:conditions => { :user_id => "#{id}" }
) ode here
Note the id as an additional field.

Datamapper into String

I want to be able to see the string like the TwitchTV name I have in my database. Here is my current code
get '/watch/:id' do |id|
erb :mystream
#result = Twitchtvst.all( :fields => [:Twitchtv ],
:conditions => { :user_id => "#{id}" }
)
puts #result
end
result in terminal;
#< Twitchtvst:0x007fb48b4d5a98 >
How do I get that into a string (TwitchTV answer in database)
Opppppsss!
Here is the real code sample. Sorry!
get '/livestream' do
erb :livestream
#users_streams = Twitchtvst.all
puts #users_streams
end
If I add .to_s at users_stream it does not work
By adding .to_csv, not exactly a string, but it should show the content:
get '/livestream' do
erb :livestream
#users_streams = Twitchtvst.all
#users_streams.each do |us|
p us.to_csv
end
end
You're getting a Collection of Twitchtvst objects, so you need to convert each to a String:
puts Twitchtvst.all.map(&:to_s).join

Why do I get invalid attribute error when I pass in a css selector?

stackoverflow,
Here's what I'm trying to do
def get_element_from_list(root, item, index)
#browser.elements(:css => root).each do |element|
if element.present?
return element.element(:css => item, :index => index)
end
end
raise Selenium::WebDriver::Error::NoSuchElementError
end
get_element_from_list('div[class*=x-combo-list]', 'x-combo-list-item', index).click
gives me Watir::Exception::MissingWayOfFindingObjectException: invalid attribute: :css
What I don't understand is if I simply do
#browser.elements(:css => 'div[class*=x-combo-list]').each do |element|
if element.present?
return element.element(:css => 'x-combo-list-item', :index => index)
end
end
basically replacing root and item with the actual strings it works without error.
I think there might be a bug that prevents locating elements with the :css and :index locator - Issue 241.
You can work around the issue by getting an element collection and then getting the element at the specific index:
return element.elements(:css => 'x-combo-list-item')[index]
(Note that I think this css-selector might be wrong. It is probably meant to be .x-combo-list-item.)
Alternatively, assuming that x-combo-list-item is actually the element's class, you could do:
return element.element(:class => 'x-combo-list-item', :index => index)

ERB is not getting some values of a local hash

I am using Sinatra and rendering views with ERB.
I have the following action
get '/user/:id' do
u = #users.retrieve( params[:id] )
u[:mykey] = [1,2,3]
erb( :user, :locals => { :user => u } )
end
and the view looks like this
<body>
<h1><%= user["name"] %></h1>
<pre><%= user["mykey"].to_json %></pre>
and where I expect to get the [1,2,3] array, I get a big fat null.
Primitive values such as the name, are passed without a problem.
Is this of class Hash or HashWithIndifferentAccess?
You are setting user[:mykey] and retrieving user["mykey"]. Use symbol or string, don't mix them unless you are using HashWithIndifferentAccess.

How do I get an array of check boxes in haml?

I have an array of strings, called #theModels, in a routine implemented as part of a Sinatra server. These models are options for the user to select, and are obtained by the back end (the idea being, as new models are added, then the front end code should not change).
I'm using haml to render html.
How can I enumerate each element in the list of #theModels such that each element is a checkbox? And how can I obtain which checkboxes the user has selected?
I see that just putting
= #theModels
will give me the list of strings contained in #theModels, but without spacing or the like, and certainly not in checkboxes. I've found this question that appears to be similar, but my haml-fu isn't good enough to convert that into what I need.
UPDATE:
These are options associated with a file upload, such that now the code looks like:
%form{:action=>"/Upload",:method=>"post",:enctype=>"multipart/form-data"}
- #theModelHash.each do |key,value|
%br
%input{:type=>"checkbox", :name=>"#{key}", :value=>1, :checked=>value}
=key
%input{:type=>"file",:name=>"file"}
%input{:type=>"submit",:value=>"Upload"}
Problem is, that puts a file upload button on each option, instead of at the end. I only want one submit button in the end; should I have two forms that both report their results when the 'Upload' button is pressed?
UPDATE2:
After a moment's thought, the above can be modified to:
Thanks!
%form{:action=>"/Upload",:method=>"post",:enctype=>"multipart/form-data"}
- #theModelHash.each do |key,value|
%br
%input{:type=>"checkbox", :name=>"#{key}", :value=>1, :checked=>value}
=key
%form{:action=>"/Upload",:method=>"post",:enctype=>"multipart/form-data"}
%input{:type=>"file",:name=>"file"}
%input{:type=>"submit",:value=>"Upload"}
And that appears to do what I want.
I think you should send the content as an hash instead.
This will give you the opportunity to set initial values in the form.
The hash #params will give you the result.
E.g. {"oranges"=>"1"}
#app.haml
%form{:method => 'post', :action => "/"}
- #models.each do |key,value|
%br
%input{:type=>"checkbox", :name=>"#{key}", :value=>1, :checked=>value}
=key
%input{:type => :submit, :value => "Save"}
#app.rb
require 'sinatra'
require 'haml'
get '/' do
#models = {"oranges" => true, "bananas" => false}
haml :app
end
post '/' do
#params.inspect
end
The link you provided linked to a rails solution where you have a function returning the proper html.
You can define this function yourself:
Input: key, value
Output: %input{:type=>"checkbox", :name=>"#{key}", :value=>1, :checked=>value}
def check_box(key, value)
...
end
and call it in haml with
=check_box(key,value)

Resources