Rack-Flash sinatra issues - ruby

I've got a small sinatra app that I'm using rack-flash for a 'not logged in' alert. The setup is quite simple, checking for a session and returning an error message in no session is found.
I've been finding the whole thing quite temperamental though. the flashes show on my local server but not at all on my remote. I've been through the code, and examples numerous times and can't seem to find a bug. The route and template are below:
get '/item/new' do
if session?
erb :new_item
else
flash[:error] = 'not logged in'
redirect '/'
end
end
and view is
<% if flash[:error] %>
<div id="flash-message">
<%= flash[:error] %>
</div>
<% end %>
I have rack-flash and sessions set up as such:
use Rack::Flash, :sweep => true
use Rack::Session::Cookie
any ideas much appreciated.

Have you tried using sinatra-flash? I haven't really tried rack-flash but sinatra-flash has always worked for me.

When you retrieve a value from the flash, it erases it. So when you do "if flash[:error]" you are perhaps retrieving it? Use has? to check if something is there.

Since <FlashHash #values={} #cache={}> from rack-flash is basically a Hash you can just use #has? to check whether a key/message is present. Checkout the example:
<% if flash.has?(:notice) %>
<div class="alert alert-block">
<%= flash[:notice] %>
</div>
<% end %>
I know this comes a bit late, but i hope it helps.

Related

Errno::EACCES error when testing code in development on localhost

Get this error when running this code:
<% content_for :game do %>
<% gamefolder = Dir.open(`#{Rails.root}/public/browserGames/#{#browserGame.slug}`) %>
<% gameFolder.each do |file| %>
<% if file.match?(/.js/) %>
<script src=<%= `#{Rails.root}/public/browserGames/#{#browserGame.slug}` + file %>></script>
<% end %>
<% end %>
It stops me on line 2 and gives the Errno::EACCES error. I am simply running this on localhost in development mode. Wanting to make sure everything works before I try to deploy and find similar issues.
What I am trying to accomplish with this code is to load the js files for the specific game trying to be played in the window.
I am also open to trying other ways of achieving the goal, but I have different games on here and want to make sure I am only loading the specific files for the selected game.
Any help is greatly appreciated.
Use
Dir["#{Rails.root}/public/browserGames/test/*.js"]
instead of opening open for dir.
and use <script src="<%= file %>"></script>

Is there a good rails gem to output the view equivalent of rake routes?

What I'm looking for is a gem that could output a complete webpage version of each view a rails app has.
I realize I might not have correctly worded this. What I mean is I would like to see each route that has html output, but what the view actually looks like, not just a line saying what the view is.
In development (rails 4.x) you can simply go to any non-existent page and it will return all of the routes.
Example Screenshot: https://dl.dropboxusercontent.com/u/23115266/Screen%20Shot%202014-02-27%20at%209.21.03%20AM.png
The sextant gem allows you to see all your application's views in development by navigating to the /rails/routes URL.
https://github.com/schneems/sextant
Please correct me if I'm wrong, but it looks like you want a listing of view templates. Not sure there is a gem that can do this, but you could easily do it yourself with the following:
module ShowViewsHelper
def all_views_from_path(path)
helpers = Array(path).flat_map do |_path|
extract = /^#{Regexp.quote(_path.to_s)}\/?(.*)_.erb$/
names = Dir["#{_path}/**/*.erb"].map { |file| file.sub(extract, '\1') }
names.sort!
end
helpers.uniq!
helpers
end
end
create a template somewhere with the following:
<ul>
<% all_views_from_path("app/views").each do |v| %>
<li><%= v %></li>
<% end %>
</ul>

What does <%anything%> do?

In Rails tutorials and vids, it seems totally different from regular ruby. For example, I see <%render%> or <%end%>. When am I supposed to use <%%> and what is it for?
<% code %> is used in erb, which stands for "Embedded Ruby". It's typically used in HTML generating templates. It's brother is <%= code %> which outputs the return value of the expression inside it.
<h1>Hello, <%= #user.name %>!</h1>
# potentially renders: <h1>Hello, Bob</h1>
# potentially renders: <h1>Hello, Sue</h1>
The non-outputting <% code %> version of this tag is useful for executing code, but not writing anything to the template. This is useful for conditionals (as well as other things).
<h1>
Hello
<% if #user.sex == 'male' %>
Mister
<% else %>
Miss
<% end %>
<%= #user.name %>!
</h1>
# potentially renders: <h1>Hello Mister Bob!</h1>
# potentially renders: <h1>Hello Miss Sue!</h1>
In pure ruby, this would be a syntax error. But within an erb template, these tags allow you to control how the template renders by executing ruby to control the template flow, and by writing out the result of ruby expressions.
Rails uses erb by default for it's views, which are mostly html generating templates. So you see this a lot in Rails examples. Just keep in mind that erb is just one option for your templates in Rails. There is a great many options, which may use different syntax entirely.
That is used in HTML code to display ruby code inside the tags.
<%= render 'folder/partial_form' %>
That will render a form partial.
I suggest you have a nice long read through the links below:
http://www.guides.rubyonrails.org
http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials
The <%= %> and <% %> syntax is used to write ruby in a .erb file. Think of ERB as an html file that always allows ruby code to be run. The <% %> syntax runs the ruby but does nothing by default while the <%= %> syntax(notice the =) outputs the result of the ruby code to the screen.

Rails, Twitter Bootstrap, Simple Form, Heroku - Odd error when using ":wrapper => :prepend do" to get an add-on span

I have the following code in a form partial:
<%= f.association :client %>
<%= f.input :url, :wrapper => :prepend do %>
<%= content_tag :span, "http://www.", :class => "add-on" %>
<%= f.input_field :url, :class => 'span4' %>
<% end %>
Trying to load anything to do with this form partial e.g. new/edit etc causes Heroku to error. It works flawlessly on localhost.
The only obvious error in the Heroku logs is:
ActionView::Template::Error (undefined method `source' for nil:NilClass):
I'm afraid I don't really understand that but thought it might help anyone trying to answer this.
If I change the form partial code it works on Heroku no problem at all e.g.
<%= f.input :url, :input_html => {:class => 'span4' } %>
Obviously I lose my nice bootstrap add-on span so I'd rather find out what the issue is rather than running around it.
Shot in the dark, but maybe an initializer that sets up the prepend wrapper (like initializers/simple_form.rb or initializers/simple_form_bootstrap.rb) is not being run by Heroku, either because it's not checked into git, or because of some environment-specific logic in the initializers or environment files.
Well after testing and some help from Dan I finally got to the bottom of it. Unfortunately, it seems to be quite localised to my situation though.
But just in case anyone stumbles across this, the error I had was the following:
ActionView::Template::Error (undefined method `source' for nil:NilClass):
That was in the log and was THE SAME when I followed Dan's advise to turn on error reporting on the production site (set consider_all_requests_local = true in config/environments/production.rb).
I thought that the error was being caused in the template, however, it was some validation rules in the model. The validation rules for me where some regex to stop the user starting the input with a certain string. They worked perfectly fine on my local, but Heroku really wasn't liking it!
I removed those validation rules and voila, my nice bootstrap add-on span was working perfectly.

Commenting something out in Ruby ERB within the UI?

I have a .html.erb page and I am trying to comment something out using traditional HTML comments:
<!--
User Id (testing MySQL call): <%= #User.uid %>
-->
But since its a Ruby reference that I am commenting out, it isn't getting commented, and is generating ruby errors. How could I comment out such a thing? I also tried putting a # before that line, but that didn't work either.
In your ERB tags, to do a comment, use:
<%-# #User.uid %>
You'll still need the HTML comment tags wrapping the other text too.
You can comment out an ERB expression by changing the <%= into a <%#. This will not hide the HTML containing it from view, but you can combine HTML comments with the ERB comment to keep your application from throwing an error and hiding the surrounding HTML bits.
<!--
User Id (testing MySQL call): <%# #User.uid %>
-->
You can also comment a block with =begin and =end like this:
<%
=begin %>
<%= link_to "Sign up now!", signup_path, :class => "signup_button round" %>
<%
=end %>

Resources