How to add attribute for HAML block? - ruby-on-rails

I would like to add class attribute for HAML = link_to "#" do block?
= link_to "#" {class: "class-name"} do
image_tag "image_path.ext"
Link Sample Text
The code above does not work.

You're missing a comma between the target link and the options hash. Try this
= link_to '#', { class: 'btn btn-primary' } do
= image_tag 'image_url'

Related

How to make the rails link_to include other tags

i'm creating a like model, so here's a code:
- if policy(bonus).liked_by?
= link_to(image_tag("heart--filled--green.png", class: "Dislike"),
bonus_like_path(bonus, bonus.user_like(current_user)), method: :delete,
data: { remote: true, behavior: "fragments" })
- else
= link_to(image_tag("heart.svg", class: "Like"),
bonus_likes_path(bonus), method: :post,
data: { remote: true, behavior: "fragments" })
- if bonus.likes_count.zero?
span Like
-else
span.has-tip data-tooltip="" title="#{ bonus.liked_by }" Like
span class="like_count" #{ bonus.likes_count }
And it generates something like this:
The problem is that if I want to like something, I would like to press on the heart(like the given image), but I need to give the opportunity to press everywhere including the span Like and like's count. How can I solve my problem?
To make the image along with the spans part of the link, wrap them inside link_to using a block.
= link_to bonus_likes_path(bonus), method: :post, data: { remote: true, behavior: "fragments" } do
= image_tag("heart.svg", class: "Like"
- if bonus.likes_count.zero?
span Like
- else
span.has-tip data-tooltip="" title="#{ bonus.liked_by }" Like
span class="like_count" #{ bonus.likes_count }
You can have a bigger block in link_to using:
<%= link_to desired_path do %>
<div class="class-name">
</div>
<% end %>

rails error message String can't be coerced into Fixnum

I'm trying to make variable in .each do loop.
<article class='lecture-list' >
<% #lectures.each do |lecture| %>
<div class='lecture-list-wrapper'>
<p class='activity'>
<% name = lecture.id + "th - "+ lecture.title %>
<span><%= link_to name, lecture, class: 'btn-lightgreen' %></span>
<span><%= link_to 'Edit', edit_lecture_path(lecture), class: 'btn-darkyellow' %></span>
<span><%= link_to 'Destroy', lecture, method: :delete, class: 'btn-darkorange', data: { confirm: 'Are you sure?' } %></span></p>
</div>
<% end %>
</article>
<% name = lecture.id + "th - "+ lecture.title %> part makes
String can't be coerced into Fixnum
error. How can I solve it?
Instead of:
<% name = lecture.id + "th - "+ lecture.title %>
Try:
Note: If you want it as variable, ideally use _ underscore not - dash. As code below.
<% name = lecture.id.to_s + "th_"+ lecture.title %>
OJ1987 answer is fine, though I prefer using interpolation in such cases:
<% name = "#{lecture.id}th - #{lecture.title}" %>

How would I go about adding a button id so I can tell which button has been pressed in Rails

I've got this bunch of code
<%= link_to admin_conference_statuses_path(conference_id: #conference.id), class: "btn btn-primary", method: :post, remote: true do %>
<span id="span">Comm invoiced out Venue</span>
<% end %>
<%= link_to admin_conference_statuses_path(conference_id: #conference.id), class: "btn btn-primary", method: :post, remote: true do %>
<span id="span">Cross charged to Client</span>
<% end %>
And I have this in my controller
def create
conference_id = params[:conference_id] #Keep the same
#conference_status = ConferenceStatus.find_by_conference_id(conference_id)#Keep the same
#conference_status = ConferenceStatus.new unless #conference_status#Keep the same
#conference_status.conference_id = params[:conference_id]
#conference_status.invoiced_out_user_id = current_user.id
#conference_status.invoiced_out_datetime = DateTime.now
if #conference_status.save
# Success
else
# Failure
end
end
Now, when one button is pressed it grabs the id and puts it into a database.
How would I go about adding it so that when button 2 (opposed to button 1) is pressed it puts current user id into a column called "cross_charged_user_id"
If you have the answer could you post it and explain what it does, so I know for next time?
Thanks
Sam
You can pass one extra parameter to second link. Then depending on this extra parameter you can assign the current user as cross_charged_user.
The html code look like:
<%= link_to admin_conference_statuses_path(#conference), class: "btn btn-primary", method: :post, remote: true do %>
<span id="span">Comm invoiced out Venue</span>
<% end %>
<%= link_to admin_conference_statuses_path(#conference, cross_site_to_client: true), class: "btn btn-primary", method: :post, remote: true do %>
<span id="span">Cross charged to Client</span>
<% end %>
And the controller just check the params[:cross_site_to_client] and assign the current user
if params[:cross_site_to_client].present?
#conference_status.cross_site_to_client_id = current_user.id
end
Even You can cleanup your code as well
#conference_status = ConferenceStatus.find_or_create_by_conference_id(params[:conference_id])
if params[:cross_site_to_client].present?
#conference_status.cross_site_to_client = current_user
else
#conference_status.invoiced_out_user = current_user
end
#conference_status.invoiced_out_datetime = DateTime.now
#conference_status.save

How concatenate the link_to in helper?

How to make it work ?
I need puts the two links.
Concatenation << with link_to does not.
module ItemHelper
def edit_links
if user_signed_in? && #item.user_id == current_user.id
html << link_to edit_item_path(#item), class: 'ui button small' do
"<i class='icon edit'></i> Edit"
end
html << link_to item_photos_path(#item), class: 'ui button small' do
"<i class='icon photo'></i> Photo"
end
html
end
end
end
The << operator is actually pushing an object onto an array. It also looks like the html variable is not defined yet. You create the array before your first link, then join it after your last, you should have what you need.
def edit_links
if user_signed_in? && #item.user_id == current_user.id
html = []
# ... existing logic
html.join
end
end
You'll need to start with something before you can append to it with <<, and then you'll need to call #html_safe to prevent Rails from escaping the HTML.
if user_signed_in? && #item.user_id == current_user.id
html = ""
html << link_to "<i class='icon edit'></i> Edit", edit_item_path(#item), class: 'ui button small'
html << link_to "<i class='icon photo'></i> Photo", item_photos_path(#item), class: 'ui button small'
html.html_safe
end
def show_link(link_text, link_source)
link_to link_source, { class: 'ui button small' } do
"#{content_tag :i, nil, class: 'iicon photo'} #{link_text}".html_safe
end
end
Create a helper method in application_helper and using that you can create your link_to tag.
Try this:
def edit_links
if user_signed_in? && #item.user_id == current_user.id
link_1 = link_to edit_item_path(#item), class: 'ui button small' do
"<i class='icon edit'></i> Edit".html_safe
end
link_2 = link_to item_photos_path(#item), class: 'ui button small' do
"<i class='icon photo'></i> Photo".html_safe
end
link = link_1 + link_2
end
end

Using font awesome with formtastic submit button

I have a submit button on my form:
= semantic_form_for record do |form|
= form.actions do
= form.submit 'Save'
I want to add an icon from FontAwesome, with link_to I can use a block:
= link_to record do
= fa_icon 'save'
Save
But this doesn't work with Formtastic's form.submit.
I also tried:
= form.submit fa_icon('save')
= form.submit fa_icon('save').html_safe
But both renders escaped HTML.
How do I add a font-awesome icon to the <button> tag?
Try using capture to set a label, as in:
- label = capture do
= fa_icon 'save'
Save
= form.button label
This only seems to work for form.button, but not for form.submit. You'll have to use form.button label, type: :submit.

Resources