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

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>

Related

How I set another syntax inside a tag? With editor sublime text 3

I have difficulty understanding how to define a new sytanx inside a tag, I did not understand with some small examples.
I would like to modify the syntax HTML (Rails) with file .html.erb and what should I add code to define a javascript syntax inside a tag <% content_for :js do %> until close <% end %> ??
Thank you for your attention.
If I understand you right, you want to make the code that is between <% content_for :js do %> and <% end %> highlighted like JS code.
In this case, you have to use include: "scope:scope.name", like so:
%YAML 1.2
---
# See http://www.sublimetext.com/docs/3/syntax.html
file_extensions:
- test
scope: source.test
contexts:
main:
- match: "\\$.*"
scope: comment.test
- match: "<% *content_for :js do *%>"
push: javascript
javascript:
- meta_scope: embeded.js
- match: "<% *end *%>"
pop: true
- include: 'scope:source.js'
I use a context (JavaScript) for more clarity.
In the main context, the two first lines basically match comments (start with a $) just to check that the syntax was working. Then I check for <% content_for :js do %> and if it matches one, it pushes the context javascript (so it's going to stop reading the main one).
So, now, we're in the JavaScript context, the name I give to the scope is embeded.js (meta_scope), and then, if it matches <% end %>, the pop: true says: stop reading this context, go back to where you were before, so it goes back to main.
The last line is the line that tell sublime that in this context, we're using the syntax highlighting javascript.
Here's something to be aware of:
As soon as it finds a <% content_for :js do %>, the everything is highlighted, even if there is no <% end %>. The <% end %> just stop the sublime from highlighting the code using the source.js definition.
I hoped it helped you, if you have any trouble about this, you can find me on the sublime text forum

Middleman Blog: Custom layout not loading

In Middleman, I am trying to set up a blog site, using custom layout for the blog. My problem is that the main layout is loading, but the blog layout for my articles is not. The article files are being served in with their plain body.
In source/layouts/ I have two files: layout.erb and article_layout.erb.
My intent is to use article_layout.erb for my blog articles.
In config.rb I have the following:
activate :blog do |blog|
blog.sources = "articles/{category}/{year}-{month}-{day}-{title}.html"
blog.layout = "article_layout"
end
I have also tried moving article_layout.erb to source/articles/ as well as prepending the config.rb file like this: blog.layout = "layouts/article_layout"
Another failed approach is to comment out the above option and configure the layout by adding this line instead: page "/articles/*", layout: "article_layout".
So far none of these approach show a difference. Since the default layout is not being rendered I would expect some sort of error message if the path to the layout cannot be found, but nothing shows up.
I managed to replicate your problem with my own Middleman blog setup.
The docs are unclear on this because there is a broken link in the layout section of Blogging.
You need to use the nested layout feature of Middleman and wrap your custom layout in:
<% wrap_layout :layout do %>
<% end %>
So your article_layout.erb would look something like this:
<% wrap_layout :layout do %>
<div class="article-container">
<article>
<h2 class="article-title"><%= current_page.title %></h2>
<%= yield %>
</article>
</div>
<% end %>
And keep your custom layout in the source/layouts folder.
Here are the docs for Nested Layouts for your reference.
I hope this helps.

MissingTemplate Frustrating behavior

I am completing this rails tutorial
Everything was going along smoothly even though im working on windows, until i hit the partials section.
I pretty much copy pasted everything from the tutorial. I made sure that the underscore character in the partial filenames is an actual underscore.
I have placed the partials in the
app/views/layouts/
path as the tutorial suggests and included the
<%= render 'layouts/shim' %>
in my application.html.erb file. This results in the
ActionView::MissingTemplate in StaticPages#home
Showing E:/ruby/my_projects/sample_app/app/views/layouts/application.html.erb where line #9 raised:
Missing partial layouts/shim with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in:
* "E:/ruby/my_projects/sample_app/app/views"
Extracted source (around line #9):
"data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
<%= csrf_meta_tags %>
<%= render 'layouts/shim' %> #this is line number 9
</head>
<body>
<%= render 'layouts/header' %>
I have tried placing the files directly in views folder as well as in the static pages folder.
I restarted the rails server multiple times during this process.
Anybody has any ideas?
Ok I guess its a ridicules mistake but it took me so long to figure it out. It should be obvious for me that windows hides extensions so after i misnamed my file the first time and tried to fix it (rename it) it already hid the .erb extension. So it had an .erb.erb extension and because of that it didnt work.
Thanks everyone that tried to help.

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.

Rack-Flash sinatra issues

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.

Resources