Trying to put rel and class inside rails link_to - ruby-on-rails

I'm trying to put in a rel and class attribute inside a anchor tag but it keeps going into the img tag. I'm fairly new to Rails so I'm hoping can see what i'm doing wrong.
<% #series.uploads.each do |upload| %>
<%= link_to image_tag upload.upload.url(:thumb), :class => 'lightbox', :rel => 'lightbox'%>
<% end %>
Here is the output.
<a href="/project-gallery">
<img alt="alt" class="lightbox" rel="lightbox" src="img-path" />
</a>

This?
<%= link_to(image_tag(upload.upload.url(:thumb)), :class => 'lightbox', :rel => 'lightbox') %>

This is what I ended up doing to get it to work but it just doesn't look right?
<% #series.uploads.each do |upload| %>
<%= link_to(image_tag(upload.upload.url(:thumb)), '', {:rel => "lightbox", :class => "lightbox"})%>
<% end %>

Related

Display Acts_as_taggable Tags seperatly

I'm trying to Display each Tag a Question has in a Span. I use acts_as_taggable_on.
I managed to get the below Code working, but it's a Tag cloud. Meaning that ALL the tags are displayed.
<% tag_cloud Question.tag_counts, %w[s m l] do |tag, css_class| %>
<span class="label label-default">
<%= link_to tag.name, tag_path(tag.name), class: "css_class" %>
</span>
<% end %>
To retrieve the Tags:
question.tag_list
Can someone help me refactor that code so only the CURRENT TAGS ON THE QUESTION are shown ?
question.tag_list will return you a string, and you can not loop through it.
question.tags will return an array,
<% question.tags.each do |tag| %>
<span class="label label-default">
<%= link_to tag.name, tag_path(tag.name), class: "css_class" %>
</span>
<% end %>
I am not aware of the plugin, but one problem I see in your code-snippet in general is that you are not operating on a specific object #question, but on the class Question. If I had to take a guess, I would say that this is the source of your problem.
Edit:
So, I just checked out the documentation for the gem and I found this code-snippet directly there:
<% tag_cloud(#tags, %w(css1 css2 css3 css4)) do |tag, css_class| %>
<%= link_to tag.name, { :action => :tag, :id => tag.name }, :class => css_class %>
<% end %>
As you can see, this is pretty much what I just told you. Instead of working on the class you have to work on a specific object or, like in the shown case, a collection of objects.

Link to current post from an array of records

Just trying to get my head around the following, probably basic I know. I am looping through an array of records using .each and would like to view the post i click via an ajax request on the same page
<h2>Recent News</h2>
<ul>
<% #tynewyddpost.reverse.each do |t| %>
<li>
<% single = t.photos.first %>
<a class="photo" href="#"><%= image_tag(single.avatar.url(:thumbnail_news_images)) %></a>
<p><%= link_to t.title, tynewyddnews_path(:type => 'tynewyddnews'), :remote => true %></p>
<p class="date"><%= date_output(t.published_on) %></p>
</li>
<% end %>
</ul>
So when i click the title it will render the same post no matter which record i click.
The partial i render
<div class="post-item">
<% #tynewyddpost.reverse.each do |t| %>
<h2><%= t.title %></h2>
<div id="work-samples">
<% for photo in t.photos %>
<%= image_tag(photo.avatar.url(:news_images), :class => 'work-sample') %>
<% end %>
</div>
<p class="post-description"><%= t.comments.html_safe %></p>
<div class="post-item-panel">
<ul>
<li class="date">
<p><%= date_output(t.published_on) %></p>
</li>
</ul>
</div>
</div>
<% end %>
Controller
def tynewyddnews
#title = 'Ty Newydd News'
tynewyddpost = Post.tynewydd_posts.reverse
tynewyddpost.pop
#tynewyddpost = tynewyddpost
#tynewyddpostlatest = Post.tynewydd_posts.first
end
Scope
scope :tynewydd_posts, :include => :department, :conditions => {"departments.name" => "Ty Newydd"}, :order => "posts.published_on DESC"
My question is how to get the particular post i have clicked. I cant do
<%= #tynewyydpost.title %>
As i get undefined method title for array. Bit of theory here i know but how to get an individual record from an array in this instance
Any help appreciated
You need to pass the id of the post you're clicked on:
<p><%= link_to t.title, tynewyddnews_path(:type => 'tynewyddnews', :post_id => t.id), :remote => true %></p>
so in your controller you can do
#theposticlickedon = Post.find(params[:post_id])
or
#theposticlickedon = Post.tynewydd_posts.find(params[:post_id])
However, you also may want to define a different path to show the individual post, instead of the tynewyddnews_path you have in your link.
You need to specify in every link ID of this post.
For example:
<%= link_to t.title, tynewyddnews_path(:type => 'tynewyddnews'), :post_id=>t.id, :remote => true %>
And than specify that in controller action you're calling , by finding this by id
#tynewyddnews=Post.find(params[:post_id])
Than you're partial instance #tynewyddnews will be clicked post

Cleanup erb files by abstracting/moving ruby code

I was wondering if it would be possible to cleanup or reorganize my views so that I have less Ruby code. The HTML often becomes cumbersome to work with because it has so much Ruby code.
I thought about moving all the Ruby stuff into helpers and assign each function (links, tags etc.) to methods.
Example. Problem becomes much worse with a more complicated layout.
<div class="sidebar">
<div id="art_nav">
<%= link_to "Previous", art_path(#previous), :remote => true, :class => "prev" unless #previous.nil? %>
<%= link_to "Next", art_path(#next), :remote => true, :class => "next" unless #next.nil? %>
</div>
</div>
Would become:
<div class="sidebar">
<div id="art_nav">
<%= link_to_previous %>
<%= link_to_next %>
</div>
</div>
Helper:
def link_to_previous
link_to "Previous", art_path(#previous), :remote => true, :class => "prev" unless #previous.nil?
end
def link_to_next
link_to "Next", art_path(#next), :remote => true, :class => "next" unless #next.nil?
end
This seems to work with simple examples.. but I am wondering how I should organize stuff when I have to do loops or similar.
UPDATED: Loop example added
<% arts.each do |art| %>
<h3><%= art_title %></h3>
<p><%= art_description %></p>
<div id="comments_<%= art.id %>">
<%= render :partial => "/comments/index", :locals => {:resource => art} %>
</div>
</div>
<% end %>
What would you do?
Here is a pattern you could use in your code, to clean it up further:
def menu(*options, &block)
params = options[0]
active = (params[:active] if params) || false
first = (params[:first] if params) || false
"
<div class=\"menu\">
<div class=\"menu-left #{active ? "active" : "inactive"} #{first ? "first" : "not-first"}\">
</div>
<div class=\"menu-center\">
<div class=\"menu-content #{active ? "active" : "inactive"}\">
#{capture(&block)}
</div>
</div>
<div class=\"menu-right #{active ? "active" : "inactive"}\">
</div>
</div>
".html_safe
end
With this helper, code gets very simple:
<%= menu(:active => #active_menu == :menu1 ? true : false, :first => true) do %>
<%= link_to "Les produits et les prix", menu1_path %>
<% end %>
At the center of this pattern, there is the call
#{capture(&block)}
Sorry this example is not "generic" at all, I did a copy-and-paste from my own code. But I'm sure you get the idea.

How do I add a span in a link_to with an image

So I have this:
<%= link_to(image_tag(#model.picture.url(:thumb), :alt => ''), "/pages/you/#{something.id}", {:id => "y_link_#{something.id}"}) %>
Which works, but I need a span in between also like this:
<a id="y_link_2" href="/pages/you/2" class="">
<span>Apples</span>
<img src="another_small.jpg?1236340989" alt="">
</a>
How do I add
<span>Apples</span>
to the link_to?
Feed a block to your link_to call:
<% link_to("/pages/you/#{something.id}", {:id => "y_link_#{something.id}"}) do %>
<%= content_tag(:span, 'Apples') %>
<%= image_tag(#model.picture.url(:thumb), :alt => '') %>
<% end %>
Alternatively:
<% link_to("/pages/you/#{something.id}", {:id => "y_link_#{something.id}"}) do %>
<span>Apples</span>
<%= image_tag(#model.picture.url(:thumb), :alt => '') %>
<% end %>
image_tag and content_tag return basic strings, so they can be concatenated using the + operator easily:
<%= link_to(content_tag(:span, "Apples") + image_tag(#model.picture.url(:thumb), :alt => ''), "/pages/you/#{something.id}", {:id => "y_link_#{something.id}"}) %>
However, as you can see, it gets quite messy - might be worth moving it into a helper method.
For a path, use the structure like so
<%= link_to edit_section_path(#section) do %>
Edit
<span class="fa fa-list pull-right"></span>
<% end %>
Haml lends itself well to situations like these. For example:
= link_to "/pages/you/#{something.id}", id: "y_link_#{something.id} do
%span Apples
= image_tag(#model.picture.url(:thumb), alt: '')
Alternatively:
%a[something, 'y_link']{href: "/pages/you/#{something.id}"}
%span Apples
%img{src: #model.picture.url(:thumb)}
It's worth learning if you want to write your views faster and read them better.

How do I set a unique ID for checkboxes in a multi-record Rails form?

I've set up a Rails form roughly following the instructions in this Railscast.
Here's the code for the form:
<% form_tag complete_todos_path, :method => :put do %>
<ul>
<div id="incomplete_todos">
<% #incomplete_todos.each do |todo| %>
<%= render :partial => todo %>
<% end %>
</div>
</ul>
<%= submit_tag "Mark as completed" %>
<% end %>
And here's the code for the todo partial:
<div class="todo">
<li>
<%= check_box_tag "todo_ids[]", todo.id %>
<%=h todo.name %>
<%= link_to 'edit', edit_todo_path(todo) %>
<%= link_to 'delete', todo, :confirm => 'Are you sure?', :method => :delete %>
</li>
</div>
It's working great, but I'm looking to start implementing AJAX and I need each checkbox to have a unique id. Right now, the input tags generated look something like this:
<input id="todo_ids_" name="todo_ids[]" type="checkbox" value="7" />
Every check box has the same id ("todo_ids_"), which is a problem. I suspect the solution is embarrassingly simple, but I'm not seeing it. Any tips?
<%= check_box_tag "todo_ids[]", todo.id, false, :id => "todo_id_#{todo.id}" -%> or whatever you want the id to be.
I consider this a bug with check_box_tag caused by the seemingly hackish nature of manually giving it the name todo_ids[] and the method code calling sanitize_to_id(name). I just ran into this yesterday and I'm contemplating a patch.
I ended up using a solution similar to Ryan's, but as I wrote in the comment I had to make a further change. In the form:
<%= check_box_tag "todo_ids[#{todo.id}]", todo.id %>
In the action called by the form:
Todo.update_all(["completed_at = ?", Time.now], :id => params[:todo_ids].keys)
Note the "params[:todo_ids].keys" at the end, which was a workaround to deal with the odd way the parameters were formatted:
"todo_ids" => {"5"=>"5"}
Can you try this and let us know if it works:
check_box_tag "todo_ids[#{todo.id}]", todo.id %>
This is the expected behaviour of check_box_tag, as this comment on a rejected fix explains.
You can use collection_check_boxes like this (haml syntax, sorry):
# Accumulate todos in a params hash like { todos: { to_complete: [] } }
= collection_check_boxes(:todos, :to_complete, #incomplete_todos, :id, :name) do |todo_builder|
= todo_builder.label do
# This is the result of calling :name on the todo, as specified
# calling the helper
= todo_builder.text
= todo_builder.check_box
Of course you can use partials inside the block, just pass and use the builder inside.
Check more options in the API docs.

Resources