Where has development? and build? gone in middleman v4 - environment

In version 3.0 of middleman, you've been able to use the variables development? and build? in templates. In version 4.0 these variables no longer exist.
How do I identify which environment middleman is currently running in?

Middleman v4.0 uses a new method of identifying which environment it is currently running in. This is because you can now have more than two environments.
Old syntax:
<% if development? %>
...
<% end %>
New syntax:
<% if config[:environment].eql?(:development) %>
...
<% end %>

Actually you can just do
<% if development? %> ... <% end %>
Reference https://github.com/middleman/middleman/blob/48c97b22ff25e22a2533fb5b4128337b5a97e080/CHANGELOG.md#418

Related

Rails collection render cache doesn't work when I use cached: true

My project is with Rails 4.2.9.
I found out that the cache doesn't work in the index pages. (I checked with the log.)
# This doens't cache.
<%= render partial: 'docs/doc', collection: #docs, cached: true %>
# This caches fine.
<% #docs.each do |doc| %>
<% cache doc do %>
<%= render 'docs/doc', doc: doc %>
<% end %>
<% end %>
According to the rails guide, the first example not only works but also is better. What am I doing wrong?
It looks like you're going to need to update your version of rails, to at least version 5, for this to work.
It looks like version 5 has this feature, but version 4.2 does not have this feature. It was added in this commit and you can read more about it at https://blog.bigbinary.com/2016/03/09/rails-5-makes-partial-redering-from-cache-substantially-faster.html.

erb indenting: should you indent html inside ruby control structures

When it comes to indentation, which is more prevalent
<% if something? %>
Test
<% else %>
Test
<% end %>
or
<% if something? %>
Test
<% else %>
Test
<% end %>
Use the first one. You'll stay consistent from pure Ruby to ERB to JS to Coffeescript. Much easier to follow with the eyes as well.
Better to use the first option as the concept of indentation is for making the code more readable and in the coffee-script it is must.

Displaying an image in erb that's hosted elsewhere online

I'm having a little trouble trying to display an image that is hosted on another site. Usually I would have the image in my assets pipeline, but on this project I am using an API to grab the image URL that's hosted on another site. I'm trying it this way below, but I feel like it's still pointing to my assets folder. How can I set it to display this image if it meets a conditional? Thanks!
...
<% if article.image_url %>
<%= image_url("<%= article.image_url %>") %><p>
<% end %>
...
Try <%= image_tag(article.image_url) if article.image_url %>
Hope this helps!
I believe you'd need to interpolate article.image_url like so:
<% if article.image_url %>
<%= image_tag article.image_url %><p>
<% end %>

Adding ruby global variable to a html hyperlink

I have the following code in my html.erb file in my ROR application.
<% $tmp_bug="something" %>
JIRA
I want to add that ruby variable to the end of the hyperlink. This does not seem to work.
<% $tmp_bug="something" %>
JIRA

How can I create a form in Rails without having to use form_for and a model instance?

First of all, I'm a Rails newbie. I can hold my own in Ruby, but Rails is a whole different story for me. I like the development speed Rails offers me, but I can't seem to make peace with the existing documentation.
For all of my forms so far, I used form_for, with an instance for the model I needed to create ( for example, submitting a new book ). I would really like to be able to just write something like :
<% form(:action => "whatever") %>
<% text_field ... %>
<% file_field ... %>
<% end %>
From the articles I read online, I understood that this was the way things got done in Rails < 2.0 . Is there anyway of doing this in Rails > 2.0, or something equivalent to it ? Can you please post a snippet ?
Take a look at form_tag.
<% form_tag '/posts' do %>
<div><%= submit_tag 'Save' %></div>
<% end %>

Resources