Placing a span tag within an anchor in Haml - haml

I am new to Haml and am trying to simply place a span tag within an anchor link like so:
.text Here is some copy.
= link_to 'Visit website', 'https://websitelink.com/', class: 'link'
%span
This is throwing an error, "syntax error, unexpected keyword_ensure, expecting $end"
How Do I perform this simple task?

Rails’ link_to helper accepts either the link text as a parameter or as the result of a block.
In this case you probably want something like this:
= link_to 'https://websitelink.com/', class: 'link' do
%span Visit website
Note the do indicating a block, and that there are only two parameters to the method, the “Visit Website” text has been moved into the block.
This renders:
<a class="link" href="https://websitelink.com/"><span>Visit website</span>
</a>

Related

Why does HAML think I have content on the line and nested? (I don't)

My HAML reads:
%table.screenshots
%thead
%trow
%td{:colspan => 12} Screenshots for #{element.name}
%tbody
- screenshots.each do |set|
%tr
- set[1].each do |shot|
- if shot == :blank_cell
%td{:colspan => set[0]}.twelfth
- else
%td{:colspan => set[0]}.twelfth
= image_tag(shot[1]) # <= ERROR APPEARS HERE
- if #redacted
%h1.blur
%span Image blurred in
%br
%span demo report only
%p #{shot[0]}
There are no invisible spaces or tabs after .twelfth.
Why, then, do I get this error?
Illegal nesting: content can't be both given on the same line as %td and nested within it.
BTW, I get the same exception when I run:
haml --debug print.html.haml
The class and id identifiers (. and #) must come after the tag name, and before any attribute hash.
In your code the problem is the line:
%td{:colspan => set[0]}.twelfth
This is interpreted as a td element with a colspan attribute, containing the content .twelfth, which would look like this when rendered, if it were by itself:
<td colspan='7'>.twelfth</td>
However this line also has content nested below, which Haml doesn’t allow.
You can fix this by using an explicit class entry in the attribute hash as you have in your answer, or by moving the .twelth class specifier in front of the attribute hash, like this:
%th.twelfth{:colspan => set[0]}
Fixed it by changing the offending line to:
%td{:colspan => set[0], :class => "twelfth"}
Looks like there's a bug in the HAML interpreter

Use translation for submit button

I don't want to use the default
<%= f.submit %>
and have created a helper function for it, which also embeds an icon. The helper function expects a label to put on the newly created button.
I'm calling it like this:
<%= submit_button("icon-plus", I18n.translate("helpers.submit.create")) %>
But now on this text appears on the button:
%{model} toevoegen
Instead of:
Product type toevoegen
If I use the normal submit button then the correct text appears so my yml files are correct. How can I get the correct text to use in the helper?
Helper code:
def submit_button(icon, label)
link_to "javascript:void(0)", :class => 'btn btn-primary', :onclick => "$(this).closest('form').submit()" do
raw('<div class="') + icon + raw(' icon-white"> ') + label +raw('</div>')
end
end
As the I18n guide says, the translate function interpolates variables passed in the %{} brackets using its second argument (a hash).
In your case you need to tell it the model by doing this:
I18n.t("helpers.submit.create", model: "Product type")
If you want a generic option that would work for any model, you can see how Rails itself does it by looking at the source on GitHub - it's something like
I18n.t("helpers.submit.create", model: f.object.class.model_name.human)
As an aside, you don't need to (and probably shouldn't) use raw there. What you are trying to achieve could easily be done with the built-in helpers:
link_to ... do
content_tag :div, label, class: "#{icon} icon-white"
end

Rails: Template Error "dynamic constant assignment"

In my view I render a partial.
The name of the partial is constructed by the name of a product page.
<%= render :partial => "product_pages/" + selected.headline %>
In the test case the headline is "electronics".
So I also tried:
<%= render :partial => "product_pages/electronics"
For testing the partial view looks like this:
<p>Test</p>
Now I get this error I do not understand:
ActionView::Template::Error (/var/www/*****/app/views/product_pages/_Elektro
nik.html.erb:1: dynamic constant assignment
...r = #output_buffer;Elektronik = local_assigns[:Elektronik];;...
... ^):
1: <p>Test</p>
app/views/pages/_content.html.erb:13:in `_app_views_pages__content_html_erb__4
0580468132849538_266915680_1201196437383914942'
app/views/pages/index.html.erb:3:in `_app_views_pages_index_html_erb__43007964
38685262523_267219620_562910368159856764'
You need to downcase your headline:
selected.headline.downcase
This is because you're going to be rendering a partial like this: product_pages/Elektronik, and when you call render :partial it'll attempt to define a local variable that has the same name as the partial, which is why you're getting this error: the code isn't defining a local variable but is actually defining a constant.
If you downcase it, it will define a local variable rather than this constant.

Rails3 - How to send Javascript Variable to a controller's action with the link_to helper?

If i have the following javascript code
var myVar = 0;
function setNewValforVar(newValue){
myVar = newValue;
}
and i'm calling setNewValforVar function n times
so when I click on a link it'd send myVar value to the controller action in a remote link like
<%=link_to "My Link",{:action=>"myAction"},:data=>''sval='+myVar',:remote=>true%>
I Rails 2.3.x I'd do this with
<%=link_to_remote "My Link",:url=>{:action=>"myAction"},:with=>"'sval='+myVar"%>
and i was getting this on the controller's action with
params["myVar"]
how do I do this on Rails 3 with the link_to helper?
Rails 3 no longer supports this behavior. You will have to either create a form to submit the value, or modify the href of the link to include the value in the query string.
I found some solution that use callbacks. Link must be marked in some way, for example by adding the id:
<%=link_to "My Link", {:action=>"myAction"}, :remote=>true, :id => "mylink" %>
There is an example for prototype_ujs: the parameter is simply appended to the request's URL (the code is a bit simplified I assume that some parameters already exist).
<%= javascript_tag("document.on('ajax:create', 'a#mylink',
function(event) { event.memo.request.url += '&sval=' + myVar; })") %>
Some advantage of this (ugly a bit) solution is the possibility of using one function for a particular class of links instead of the indicated id.

syntax error, unexpected kENSURE

I have a problem in solving this.
= link_to "Bounced", bounced_email_path(#email)
Bounces
%span
= #email.bounces_count
I want the Bounces, span and other lines within the link tag.
The above code ends up in an error:
syntax error, unexpected kENSURE, expecting $end".
To use the block form of link_to, you must remove the body parameter ("Bounced") and use the do keyword to start the block:
= link_to bounced_email_path(#email) do
Bounces
%span
= #email.bounces_count