unwanted formatting / line breaks in emacs - ruby-on-rails

I'm experiencing problems in Emacs when I edit web template code, i.e mixed php/html code or mixed ruby/html code. Emacs makes line breaks when it should not. The line breaks occurs when I enter a space somewhere on the line, very annoying...
How can I disable this behaviour?
Below is the kind of code I'm working with. If I would enter a space after one of the commas there, then Emacs will make a line break, sometimes multiple line breaks.
<% if #item.id %>
<b>Congratulations!The item was saved!
<%= button_to 'Preview the ad',#item,:method=>:get,:class => "btn add" %>
<% end %>
thanks!

It looks like for some reason auto-fill-mode is active (which you can check by looking for "Fill" in your modeline). If this is the case, you should turn it off:
M-xauto-fill-modeRET

To save this process for further sessions, you can put in your .emacs (after where it loads the mode specific stuff):
(auto-fill-mode nil)
Another thing is that you could embrace this feature; it is often good to have a limit on the amount of code you want to have in your screen.
Use M-x set-fill-column RET to set the amount of characters allowed on a line before it automatically breaks the line.

Related

suggest .erb 'functions' in sublime

I'm barely beginning to understand Ruby on Rails and there are a lots of things I don't get to remember correctly, so I wonder if there's a way to enable suggestion and autocomplete for functions in html.erb files.
Right now it suggest me things when I'm working on ruby files, but this is not the case in html.erb files so I make a mess each time I try to remember how to do something until I find that I'm looking for 'yield' or 'provide'... I know is not a big help but I would love if sublime could suggest me this when I type 'y' or 'p' between <% %> tags.
there are a few packages you can check for sublime text 3 ,
https://github.com/matthewrobertson/ERB-Sublime-Snippets
https://github.com/CasperLaiTW/ERBAutocomplete
some convenience you can get are, just write
er and tab which will print <% %>
ep and tab which will print <%= %>
If this is about the autocomplete popup not showing while typing, rather than completions not working when pressing the Tab, you need add the scope to you auto_complete_selector setting. By default, Sublime Text shows this popup only when working in the source scope, even though you can force showing the popup when pressing Ctrl+Space. To change this, open your user preferences (Preferences > Settings User) and adjust the auto_complete_selector by adding the text.html scope (or the more limited text.html.erb).

Rails 4: how to insert line breaks in text_area?

I have created a blog in rails. I'm a beginner and got quite far, but now I'm stuck with a seemingly minor detail: I can't seem to format the posts (articles).
Here's the relevant part of my show.html.erb:
<p>
<strong>Content:</strong>
<%= simple_format (#article.content) %>
</p>
When I write something and insert html-tags, they are not recognized as such. What am I doing wrong?
Rails will automatically remove html tags to prevent someone from injecting code into your webpage (e.g. malicious javascript)
If your users cannot enter data into #article.content and it's always safe then you can flag it as safe usng the html_safe method.
<%= (simple_format (#article.content)).html_safe %>
Can you post the article content for reference? If I had to guess, I'd imagine Rails is escaping the html tags and inserting them as plain text (so the output looks like: Article content !
Take a look at Rails' helper methods like content_tag (http://apidock.com/rails/ActionView/Helpers/TagHelper/content_tag) and concat (http://apidock.com/rails/ActionView/Helpers/TextHelper/concat) and consider using those to help with generating the appropriate html tags.
An issue to be concerned with is who's going to be supplying the content. For example, if you're writing an application that other people will use, you want to make sure any html give you is escaped to avoid XSS attacks. In that case, you'll want to spend some time reading about how to properly sanitize user input.
You can now specify the tag it gets wrapped in (defaults to p) like so:
<%= simple_format (#article.content, {}, wrapper_tag: "div") %>
or
add white-space: pre-line style.
It will display \r or \n (enter) in user input as a new line.
for more info:
http://apidock.com/rails/v4.0.2/ActionView/Helpers/TextHelper/simple_format

How do I automatically direct the cursor in keyboard shortcuts for sublime text?

I work a lot with ruby on rails so naturally I code a lot in erb. Typing <%= %> is always a pain so I wrote a keyboard shortcut script for in Key Bindings. Here is my code:
{"keys": ["ctrl+shift+e"], "command": "insert_snippet","args": {"contents": "<%= %>"}}
This is okay however this simply creates <%= %> and the cursor ends up after the > sign. How do I make it so that it creates <%= %> and the cursor ends up in between <%= and %> so I can start typing right away?
Thank you guys in advance!
{"keys": ["ctrl+shift+e"], "command": "insert_snippet","args": {"contents": "<%="$0"%>"}}
As per the documentation provided here placing the $0 would make the cursor move to that position. I also suggest you create an autocomplete mentioned here in this page.
https://sublime-text-unofficial-documentation.readthedocs.org/en/latest/extensibility/completions.html

Rails button_to with multiple line text

I'm writing a Rails application and I have button that I create as follows:
<%= button_to t('.upload_html'), ... %>
where the ... stand for the other options. So the button should have the text pointed to by .upload_html and it does. Now I want that text to have two lines. In the YAML file I had
upload_html: "turn off"
and now I want
upload_html: "turn<br>off"
with a line break inside the button label text. But this does not work. It displays the string literally including the HTML tag without linebreaking. What can I do to force the line break?
Thank you!!
Best, Patrick
did you try "turn<br>off".html_safe ?

Correct coding convention for embedded code on web page templates

I had come experience with PHP a time ago and now I'm learning to use Ruby on Rails. But one simple question bothered me in both these languages, so I think I can cross-post it to both tags.
As you know, one of the concepts there is that one can embed PHP or Ruby code into web page template. Then these statements are executed and result of its execution is inserted in certain places of the page, marked with "brackets" <%= ... %>.
Or... wait. We program Ruby/PHP, but not HTML. Maybe we should treat template as Ruby/PHP code, into which sometimes HTML markup is inserted? So the process is treated like that HTML are inserted into ruby code into the "brackets" %> ... <%.
These are two different approaches:
HTML page is the primary entity, and it is affected by code execution; or
code is the primary entity, and it is executed, while HTML snippets are inserted in certain places.
This philosophy leads to different possibilities in coding conventions: result of code execution influences the page If we adhere the first insight, then the code would look like this:
<p>
<% (1..10).foreach do |i| %>
Iteration number <strong><%= i %></strong>. <br/>
<% end %>
</p>
But if we stick to the second alternative, the code would be formatted like this:
<%
%><p><%
(1..10).foreach do |i|
%>Iteration number <strong><%
%><%= i %><%
%></strong>. <br/><%
end
%>
How should the concept of templates be construed? What concepts do you, way more experienced Web developers, account for the coding convention?
If this is in your View layer (and it should be), then the HTML is the primary entity. It's the most pertinent part of that layer -- marking up your data to display in meaningful ways to the user.
Even aside from that, your second example is nearly unreadable. I see what you're doing, but it took me a minute to wrap my brain around it. I've also never, ever seen View-layer code like your second example (and I would make it one of my priorities to change it wherever I saw it if it was in a project I was working on).
To be more concise: you're putting the emphasis on the wrong thing. In my opinion, readability trumps just about everything else. The coding style that produces the most readable code is therefore the most superior (ceteris paribus and YMMV, of course).
Maybe you should look into Haml? I don't know if there's a php equivalent, but as far as Rails goes, it's somewhere in between the two schemes. It's not quite code centric. But when used right, all the raw html is prepared programatically.
In short everything is considered text to be directly outputted, unless prefixed with either a %, - or =. Which translate to html-tag, ruby code that doesn't output. Ruby code that does output. Haml then uses whitespacing to nest things properly, much like python does. Raw html outputs untouched but using % to specify a tag handles closing tags.
Sample:
#outer-div
- #items.each do |i|
%span.item
= i
%br
Outputs
<div id="outer-div">
<span class="item">
item
</span>
<br>
</div>
See the haml tutorial for more information.
To answer the central question. The bulk of any page is going to be HTML or raw text. We reduce the bulk of that text with includes and helpers, but it's still there. If there were a truly code centered approach my use of it would depend on the ratio of program logic to html. Personally I'd rather go with the html centered approach.
If you are interested in a code-oriented view, this is something you might try implementing as a pure Ruby DSL:
tag :p, :class => 'iterations-container' do
(1..10).each do |i|
text "Iteration number "
tag :strong { text i }
text "."
tag :br
end
end
Or perhaps instead of tag :p do ... end, you may favor tag.p do ... end.
I recommend doing only very simple logic in your template files. That way designers who can edit HTML can easily edit even those files to alter the layout if need be.

Resources