Ruby variable each loop substitution - ruby

I need to replace X_VARIABLE in 2 places.
For the first X_VARIABLE I want to keep the text 'remove'
For the second X_VARIABLE I want to keep 'd_cars_path'
<% #cars.each do |x| %>
<% #a = #b.send(x) %>
<% if #a == true %>
<%= button_to "removeX_VARIABLE", X_VARIABLEd_cars_path(:id => #user.id), class: "btn btn-large btn-primary" %>
<% end %>
<% end %>
I am looking for some help with the variable substitution syntax. Thanks.

I'd write:
<% #cars.each do |x| %>
<% if #b.send(x) %>
<%= button_to "remove#{x}",
send(:"#{x}d_cars_path", id: #user.id),
class: "btn btn-large btn-primary" %>
<% end %>
<% end %>

Related

Middleman shows syntax error when using a link_to block

I'm using Middleman 4.2 with Middleman-blog 4.0.2.
When I have:
<% blog.tags.each do |tag, articles| %>
<%= link_to "#{tag.titleize} - #{articles.size}", tag_path(tag) %>
<% end %>
I get the desired <a> element output:
Test Tag - 1
But when I change the link_to to a block:
<% blog.tags.each do |tag, articles| %>
<%= link_to tag_path(tag) do %>
<%= tag.titleize %> - <%= articles.size %>
<% end %>
<% end %>
I get a syntax error:
/source/blog/index.html.erb:43: syntax error, unexpected ')' ...<< ( link_to tag_path(tag) do ).to_s; #_out_buf << '
I can't seem to figure out why I'm not able to get the same output here.
Any pointers?
I just realized I had the wrong erb tag on the line with the link_to helper.
The correct code looks like this:
<% blog.tags.each do |tag, articles| %>
<% link_to tag_path(tag) do %>
<%= tag.titleize %> - <%= articles.size %>
<% end %>
<% end %>

How would I get all people, that are following my in Ruby

I am using the act_as_follower gem, and I get all the users, that I follow, but now I want all the users, that are following me, how would I do that. I checked and I think something like this
current_user.followers, but I cant get it working. My code for people that I am following is this:
<% #users = User.all %>
<% #users.each do |user| %>
<% unless user == current_user %>
<div class="container people-lists list-following">
<%= image_tag("/assets/" + user.avatar + ".png", alt: "Logo", class: "people-image") %>
<%= user.username %>
<% if user_signed_in? %>
<% unless user == current_user %>
<% if current_user.following?(user) %>
<%= button_to("Un-Follow", user_follow_path(user.to_param, current_user.get_follow(user).id), :method => :delete, :remote => true, class: 'btn btn-primary button') %>
<% else %>
<%= button_to("Follow", user_follows_path(user.to_param), :remote => true, class: 'btn btn-primary button') %>
<% end %>
<% end %>
<% end %>
</div>
<% end %>
<% end %>
I found I the User.all returns all of them, and I can use the all_following, and follows like this and it works.
<% #users = current_user.all_following %>
And like this
<% #users = current_user.followers %>

Tell <%= ... %> not to add a space

I use the following code in a template to render a date:
<% if job.end_month do %>
<%= "#{job.end_month}/" %>
<% end %>
<%= "#{job.end_year}" %>
This results in 3/ 2014 and not in 3/2014. How can I force Phoenix not to add a space after "#{job.end_month}/"?
One solution would be to create an external helper that did this for you:
e.g.
def job_string(%Job{end_month: nil}), do: job.end_year
def job_string(job), do: "#{job.end_month}/#{job.end_year}"
Then just use that in your view:
<%= job_string(job) %>
Alternatively if you don't want to do all that and would prefer a messier sort of look:
<% if job.end_month do %>
<%= "#{job.end_month}/#{job.end_year}" %>
<% else %>
<%= "#{job.end_year}" %>
<% end %>

cached votes not updating with validates_presence_of in model

I have acts_as_taggable installed and working for my products with cached votes.
After I tried to finish my validations inside the Product model with validates_presence_of the cached votes are not getting updated anymore.
Anyone had the same problem?
Any hint is appreciated.
Basic validation inside the product model. If this line is not there, the cached_votes work:
validates_presence_of :original_url, :format => URI::regexp(%w(http https))
How I trigger the votes:
<% if user_signed_in? %>
<% if current_user.liked? likeable %>
<%= form_tag unlike_path(likeable_type: likeable.class.to_s, likeable_id: likeable.id), method: :post, remote: true do %>
<% button_tag class: 'btn btn-block liked' do %>
<%= fa_icon 'heart-o' %> unlike
<% end %>
<% end %>
<% else %>
<%= form_tag like_path(likeable_type: likeable.class.to_s, likeable_id: likeable.id), remote: true do %>
<% button_tag class: 'btn btn-block' do %>
<%= fa_icon 'heart' %> like
<% end %>
<% end %>
<% end %>
<% else %>
<%= link_to new_user_registration_path do %>
<% button_tag class: 'btn btn-block' do %>
<%= fa_icon 'heart' %> like
<% end %>
<% end %>
<% end %>

Do I need an Enumerator for this?

I want to do this:
<div class="menu">
<%- render_menu do |title,path,children| %>
<%= link_to title, path %>
<div class="submenu">
<%= render_menu(children) do |title,path,children| %>
<%= link_to title, path %>
<%= children %>
<%- end %>
</div>
<% end %>
</div>
The method render_menu would look something like this:
def render_menu(children=nil)
children = Paths.roots if children.nil?
children.collect do |child|
[ child.title, child.path, child.children ]
end
end
I'm not sure what the render_menu needs to return to get the three params..
The render_menu will grab the default menu items if no argument is given..
You have to use yield and replace each for collect inside render_menu:
def render_menu(children=nil)
children = Paths.roots if children.nil?
children.each do |child|
yield([child.title, child.path, child.children])
end
end
You should also modify your template to not display the value returned by render_menu:
<div class="submenu">
<% render_menu(children) do |title,path,children| %>
<%= link_to title, path %>
<%= children %>
<% end %>
</div>

Resources