How to show if rhomobile array is empty - ruby-on-rails

I have a bit of code in RhoMobile that shows results of a search. I want to display a message if no results are found but as a Ruby n00b I'm not getting the message I want output.
<ul data-role="listview">
<% #employees.each do |employee| %>
<li>
<a href="<%= url_for :action => :show, :id => employee.object %>">
<%= employee.name %>
</a>
</li>
<% end %>
<% "<li>No results found</li>" if #employees.empty? %>
</ul>
How to fix this?

You're missing the =, it should be:
<%= "<li>No results found</li>" if #employees.empty? %>
Though that might not work either because the string isn't marked as HTML safe. That said, it's probably best to wrap everything in a conditional to make it more clear and avoid having HTML in a string:
<ul data-role="listview">
<% if #employees.any? %>
<% #employees.each do |employee| %>
<li>
<%= link_to employee.name, {:action => :show, :id => employee.object} %>
</li>
<% end %>
<% else %>
<li>No results found</li>
<% end %>
</ul>
I've also replaced your hand-coded link with a call to link_to.

Related

How to avoid same instance variables for footer

I have a footer in my rails application that shows all recent posts and categories produced by two instances variables in the welcome_controller.rb
But then if i go to another section that has a different controller responsible i get an error as the #posts and #categories instance variables are not there to loop through and list the latest posts and categories.
welcome_controller.rb (code extract)
#categories = Category.all
#posts = Post.order("created_at DESC")
_footer.html
<div class="row footer-black">
<div class="large-3 columns text-left">
<h4>Categories</h4>
<ul>
<% #categories.each do |c| %>
<% unless c.header? %>
<% if c.restriction %>
<% if check_for_free_questions_in_restricted_category(c) %>
<li> <%= link_to c.title, category_random_free_question_path(c),'data-no-turbolink' => true %> </li>
<% else %>
<li> <%= link_to c.title, new_subscription_path,'data-no-turbolink' => true %> </li>
<% end %>
<% else %>
<li> <%= link_to c.title, category_random_question_path(c),'data-no-turbolink' => true %> </li>
<% end %>
<% end %>
<% end %>
</ul>
</div>
<div class="large-5 columns text-left">
<h4>Latest Posts</h4>
<ul>
<% #posts.each do |p| %>
<li> <%= link_to truncate(p.title, length:70), blog_post_path(p) %> </li>
<% end %>
</ul>
</div>
<div class="large-4 columns text-left social-links">
<h4>Social</h4>
<a href="#">
<i class="fa fa-facebook-square fa-2x"></i>
</a>
<a href="#">
<i class="fa fa-twitter-square fa-2x"></i>
</a>
<a href="#">
<i class="fa fa-google-plus-square fa-2x"></i>
</a>
</div>
</div>
is there a way to show categories and posts in footer without instantiating same variables over and over again?
The short answer is 'no'. Every variable must be instantiated (initialized) before it can be used (in this case, in your view).
Now, there are some more efficient ways of instantiating these variables than writing:
#categories = Category.all
#posts = Post.order("created_at DESC")
in multiple controllers. But, that's a different question. If you're interested in that, then ask a new question or refine this one.

Rails view returning database field after loop

In my view I do this:
<ul class="list-group">
<%= JSON.parse(current_printer.stripe_managed_account.fields_needed).each do |f| %>
<li class="list-group-item">
<%= f.gsub!(/[._]/, ' ') %>
</li>
<% end %>
</ul>
It lists everything out like it should, but then at the end it returns the value of current_printer.stripe_managed_account.fields_needed. I'm not sure why it does this, or how to prevent it from doing this.
This is a screenshot:
Replace <%= %>, on your each line by <% %>.
Because <%= #your each %> is same that : <% puts #your each %>.
Try this:
<ul class="list-group">
<% JSON.parse(current_printer.stripe_managed_account.fields_needed).each do |f| %>
<li class="list-group-item">
<%= f.gsub!(/[._]/, ' ') %>
</li>
<% end %>
</ul>
Thanks #vee

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>

What is `stringify_keys' in rails and how to solve it when this error comes

In a partial file of my application I have the following code snippet for displaying user navigation(through Devise):-
<ul class="nav pull-right">
<% if user_signed_in? %>
<li>
<%= current_user.email do %>
<i class=" icon-user icon-black"></i>
<% end %>
</li>
<li>
<%= link_to "Your Links", profiles_index_path do %>
<i class=" icon-user icon-black"></i>
<% end %>
</li>
<li>
<%= link_to "Sign out", destroy_user_session_path, :method => 'delete' do %>
<i class=" icon-user icon-black"></i>
<% end %>
</li>
<% else %>
<li>
<%= link_to "Login", new_user_session_path do %>
<i class=" icon-lock"></i>
<% end %>
</li>
<li>
<%= link_to "Sign up", new_user_registration_path do %>
<i class=" icon-home"></i>
<% end %>
</li>
<% end %>
</ul>
But I'm getting an error saying:-
undefined method `stringify_keys' for "/users/sign_in":String
Now my questions are:-
What is `stringify_keys' in general??
How do I resolve this in my code???
Thanks...
1) stringify_keys is a method that is called on a hash to convert its keys from symbols to strings. It's added by Rails - it's not a standard Ruby method. Here it is in the docs.
{:a => 1, :b => 2}.stringify_keys # => {"a" => 1, "b" => 2}
2) This means that your code is passing "/users/sign_in" somewhere that is expecting a hash. Closer inspection reveals that you are mixing and matching two forms of link_to:
# specify link contents as an argument
link_to "The text in the link", "/path/to/link", some: "options"
# specify link contents in a block
link_to "/path/to/link", some: "options" do
"The text in the link"
end
As you can see you are trying to do both:
<%= link_to "Sign out", destroy_user_session_path, :method => 'delete' do %>
<i class=" icon-user icon-black"></i>
<% end %>
and Rails expects the second argument in the block form to be the options hash, so it is calling stringify_keys on it which is causing your error.
Change those links to look like this instead:
<%= link_to destroy_user_session_path, :method => 'delete' do %>
<i class=" icon-user icon-black"></i> Sign out
<% end %>
as per the documentat stringify_keys will try to convert all keys to a string. So in simple works the method expects something with key values and convert its keys to strings.
In your case most probably your User object could be empty or a plan string. is this doesnot
work try posting the complete error log

Whats wrong with this else if statement?

Basically my controller is just grabbing all members: #members = Member.all and Im looping through them while checking to see if they have a profile picture uploaded and if not then the default should be loaded:
<% #members.each do |member| %>
<% unless member.image.nil? %>
<li style="float:left; width:100px;">
<%= image_tag(member.image.url(:tiny)) %>
<%= link_to member.email, member_path(member) %>
</li>
<% else %>
<li style="float:left; width:100px;">
<%= image_tag("default_member_small.jpg") %>
<%= link_to member.email, member_path(member) %>
</li>
<% end %>
<% end %>
It seems to think every member has a profile image, and the image tag is calling "images/tiny/missing.png" for the missing images.
What gives?
I am guessing you are using paperclip, if you are, you should not use nil?, you should use present?:
<% #members.each do |member| %>
<% if member.image.present? %>
<li style="float:left; width:100px;">
<%= image_tag(member.image.url(:tiny)) %>
<%= link_to member.email, member_path(member) %>
</li>
<% else %>
<li style="float:left; width:100px;">
<%= image_tag("default_member_small.jpg") %>
<%= link_to member.email, member_path(member) %>
</li>
<% end %>
<% end %>
And instead of having an if you should just have this image named as paperclip expects it, there should not have any ifs in your code for this kind of handling.

Resources