dynamically change routes in a partial - ruby-on-rails

I have a filter on my page like so.
<ul>
<li>
<%= link_to "hot", root_path(:filter => ["hot"]) %>
</li>
<li>
<%= link_to "recent", root_path(:filter => ["recent"]) %>
</li>
<li>
<%= link_to "most popular", root_path(:filter => ["popular","day"]) %>
</li>
<li>
<%= link_to "most liked", root_path(:filter => ["liked","day"]) %>
</li>
</ul>
And the almost exact same filter(aside from the path) on another page (for followed users):
<ul>
<li>
<%= link_to "hot", followed_index_path(:filter => ["hot"]) %>
</li>
<li>
<%= link_to "recent", followed_index_path(:filter => ["recent"]) %>
</li>
<li>
<%= link_to "most popular", followed_index_path(:filter => ["popular","day"]) %>
</li>
<li>
<%= link_to "most liked", followed_index_path(:filter => ["liked","day"]) %>
</li>
</ul>
Whats the best way to roll these into one partial? Right now I have a partial for each version which seems really messy to me.
EDIT:
I got this to work as follows:
<ul>
<li>
<%= link_to "hot", url_for(:action => "index", :filter => ["hot"]) %>
</li>
<li>
<%= link_to "recent", url_for(:action => "index", :filter => ["recent"]) %>
</li>
<li>
<%= link_to "most popular", url_for(:action => "index", :filter => ["popular","day"]) %>
</li>
<li>
<%= link_to "most liked", url_for(:action => "index", :filter => ["liked","day"]) %>
</li>
</ul>

You can try to implement helper method.

Related

how to add 'glyphicon glyphicon-user' and 'glyphicon glyphicon log-in' in this code?

My code which is coming wrong:
This is what i have tried but it is coming wrong
<ul class="nav navbar-nav navbar-right">
<li> <span class="glyphicon glyphicon-user"> <%= link_to 'SignUp', {:controller =>'users', :action => 'new'} %></span> </li>
<li> <span class="glyphicon-log-in"> <%= link_to 'LogIn', {:controller =>'sessions', :action => 'new'} %></span> </li>
</ul>
Try this:
<ul class="nav navbar-nav navbar-right">
<li> <%= link_to raw('<span class="glyphicon glyphicon-user"></span>'), {:controller =>'users', :action => 'new'} %> </li>
<li> <%= link_to raw('<span class="glyphicon glyphicon-log-in"></span>'), {:controller =>'sessions', :action => 'new'} %> </li>
</ul>
Your links should wrap your icons, not the other way around. If it's still not working, it's likely due to you not including the relevant icon font or css files correctly in your project. If it's still not working, update your question with a screenshot of what it looks like, and also the relevant code where you're including Bootstrap's css and fonts.

how to convert this ruby on rails code to include html as well

How can I take this..
<li>
<%= link_to image_tag(product.photo.url(:small)), product_path(product.id, :forum_id => params[:forum_id]) %>
<% if params[:forum_id] %>
<%= link_to "Add", addtoforum_user_path(:products => [product.id],:forum_id => params[:forum_id], tp: "add") %>
</li>
to be like this.. (right now it is only inlcluding the image)
<li>
<a href="">
<figure>
<img src="image-url....">
</figure>
<span>product name</span>
</a>
</li>
Thanks
Not sure I understand the question but I guess the issue is to output the figure, img tags etc inside the a tag?
This can be achieved by passing a block to the link_to method, like this:
<li>
<%= link_to product_path(product.id, :forum_id => params[:forum_id]) do %>
<figure>
<%= image_tag(product.photo.url(:small)) %>
</figure>
<span><%= product.name %></span>
<% end %>
</li>

Render different partials with ajax - rails 3

I am trying to get a link_to to display different partial via jQuery ajax, for every nav tab link.
But can't seem to get it to work, it redirects every link to the root index.
I would like to click the links in my nav menu and display the partials: _grundskola.html.erb , _gymnasium.html.erb, _universitet.html.erb for that link in the profile-data div.
Example:
I understand that for example nav link "universitet" should call the action def universitet which calls universitet.js.erb which renders the partial _universitet.html.erb in the div.
The nav-menu - in the show.html.erb
<ul class="nav nav-tabs">
<li><%= link_to "Grundskola", :action => 'grundskola', :remote => true %></li>
<li class="active"><%= link_to "Gymnasium", :action => 'gymnasium', :remote => true %></li>
<li><%= link_to "Universitet & Högskola", :action => 'universitet', :remote => true %></li>
</ul>
user_controller.rb
def universitet
respond_to do |format|
format.js
end
end
universitet.js.rb
$("profile-data").html("<%= escape_javascript(render(:partial => 'universitet')) %>");
_universitet.html.erb
<% groups = #user.friends.group_by(&:college_name) %>
<% sorted_groups = groups.sort_by{|key, values| values.count}.reverse %>
<% sorted_groups.each do |collegename, friends| %>
<% next if collegename.blank? %>
<div class="contentbox">
<div class="box-header">
<h3><%= collegename %></h3>
<div class="meta-info">
<p><i class="icon-map-marker"></i> Malmö</p>
<p><i class="icon-user"></i><span class="count"> <%= friends.count %></span> vänner</p>
</div>
</div>
<ul class="friends-list">
<% friends.map do |friend| %>
<li><%= image_tag(friend.image) %>
<% end %>
</ul>
</div>
<% end %>
I get the wrong link structure to, the html output:
<li>Universitet & Högskola</li>
Any help would be appreciated.
Try adding format to the link
example
<li><%= link_to "Grundskola", {:action => 'grundskola',:format=>:js}, :remote => true %></li>

rails css class="current"

I am trying to add a class="current" to the current page in the navigation
to the first <li>. So the html should look like
<li id="nav-home" class="current"> but only to the current page.
<% subjects.each do |subject| %>
<li id="nav-<%= subject.name %>" >
<a <%= link_to(subject.name, :action => 'show') %></a>
<ul>
<% subject.pages.visible.sorted.each do |page| %>
<li ><%= link_to(page.name, :action => 'show', :id => page.permalink) %></li>
</ul>
<% end %>
<% end %>
You can use current_page? to check if the page you are linking to is the current page.
<li id="nav-<%= subject.name %>" class="<%= current_page?( page_i_am_linking_to ) ? 'current' : '' %>" >
This will set the class to "current" or leave it empty.
I did not really understand how you build your link/ to what you are linking, that is why I used "page_i_am_linking_to". You need to replace this with something valid.
See also the Rails API
You want to have an instance variable #current_page_id that is set to the page that your rendering, then just add the class to the element you want to have it, like so (I chose to put it on the list element):
<% subjects.each do |subject| %>
<li id="nav-<%= subject.name %>" >
<a <%= link_to(subject.name, :action => 'show') %> </a>
<ul>
<% subject.pages.visible.sorted.each do |page| %>
<li class="<%= #current_page_id == page.id ? 'current' : '' %>"><%= link_to(page.name, :action => 'show', :id => page.permalink) %></li>
</ul>
<% end %>
if you want to put it on the link instead:
<li ><%= link_to(page.name, :action => 'show', :id => page.permalink, :class => #current_page_id == page.id ? 'current' : '') %></li>

Rails: making a table from a collection

I'm using the Forem blogging gem. It renders the posts of a particular topic this way.
<%= render :partial => "forem/posts/post", :collection => #posts %>
Is there a way to make the output appear in a table? I've put <td> markup around the sections in the partial below that I want to break up into a table.
The row names I would want are Name, Posted At, Message, In Reply to, and Actions.
I thought I might be able to do something like
#posts.each do |p|
<tr>
p
</tr>
but I can't figure out how to get it to work
Partial
<a name='post-<%= post.id %>'></a>
<div id='post_<%= post.id %>' class='post <%= cycle('odd', 'even') -%>'>
<td>
Name:<%= link_to_if Forem.user_profile_links, post.user, [main_app, post.user] %>
</div>
<div class='icon'><%#= avatar(post.user, :size => 60) %></div>
</td>
<td>
<a href='#post-<%= post.id %>'>
<time datetime="<%= post.created_at.to_s(:db) -%>"><%= "#{time_ago_in_words(post.created_at)} #{t("ago")}" %></time>
</a>
</td>
<td>
<%= forem_format(post.text) %>
</td>
<td>
<% if post.reply_to %>
<span class='in_reply_to'>
<%= link_to "#{t("forem.post.in_reply_to")} #{post.reply_to.user}", "#post-#{post.reply_to.id}" %>
</span>
<% end %>
</td>
<td>
<ul class='actions'>
<% if forem_user %>
<% if can?(:reply, #topic) %>
<% if #topic.can_be_replied_to? %>
<li><%= link_to t('reply', :scope => 'forem.topic'), new_topic_post_path(#topic, :reply_to_id => post.id) %></li>
<% end %>
<% if #topic.can_be_replied_to? %>
<li><%= link_to t('quote', :scope => 'forem.topic'), new_topic_post_path(#topic, :reply_to_id => post.id, :quote => true) %></li>
<% end %>
<% end %>
<% if post.owner_or_admin?(forem_user) %>
<% if can?(:edit_post, #topic.forum) %>
<li><%= link_to t('edit', :scope => 'forem.post'), edit_topic_post_path(#topic, post) %></li>
<% end %>
<li><%= link_to t('delete', :scope => 'forem.topic'), topic_post_path(#topic, post), :method => :delete, :confirm => t("are_you_sure") %></li>
<% end %>
<% end %>
</ul>
</td>
</div>
</div>
Have you tried adding instead of at the very beginning of the code for the partial? And perhaps a before the loop over posts?

Resources