Elegant Way to Only Show Records If They Exist in Rails Erb

Elegant way to only show records if they exist in rails ERB

Move the logic into an helper :

def name_for(user)
user.name if user.name.present?
end

And call that helper in your views :

<%= name_for(@user) %>

Rails: What is the best way to only display a line when not empty in html.erb?

Few more options if you want to make the div conditional too;

<%= content_tag :div do -%>
@sponsor.address2
<% end if @sponsor.address2? -%>

or

<%= content_tag :div, @sponsor.address2 if @sponsor.address2? %>

Rails: An elegant way to display a message when there are no elements in database

If you use the :collection parameter to render e.g. render :partial => 'message', :collection => @messages then the call to render will return nil if the collection is empty. This can then be incorporated into an || expression e.g.

<%= render(:partial => 'message', :collection => @messages) || 'You have no messages' %>

In case you haven't come across it before, render :collection renders a collection using the same partial for each element, making each element of @messages available through the local variable message as it builds up the complete response. You can also specify a divider to be rendered in between each element using :spacer_template => "message_divider"

Is there a more elegant way? (accessing a rails controller from a static home page?)

You want a controller behind every view and you don't want views crossing controller boundaries in order to present information. Consider having a welcome controller (or whatever you prefer to call it). It can have an index action:

def index
@party = Party.find(:first, :order => "begins_on")
end

In config/routes.rb, make it the root controller action:

root :to => "welcome#index"

Also, to DRY that up add a .nextparty class method to the Party model and call that from both of your controller actions instead of the find method.

What's an elegant way to conditionally add a class to an HTML element in a view?

I use the first way, but with a slightly more succinct syntax:

<div class="<%= 'ok' if @status == 'success' %>">

Though usually you should represent success with boolean true or a numeric record ID, and failure with boolean false or nil. This way you can just test your variable:

<div class="<%= 'ok' if @success %>">

A second form using the ternary ?: operator is useful if you want to choose between two classes:

<div class="<%= @success ? 'good' : 'bad' %>">

Finally, you can use Rail's record tag helpers such as div_for, which will automagically set your ID and class based on the record you give it. Given a Person with id 47:

# <div id="person_47" class="person good">
<% div_for @person, class: (@success ? 'good' : 'bad') do %>
<% end %>

Rails way to render different actions & views based on user type?

Depending on how different the view templates are, it might be beneficial to move some of this logic into the show template instead and do the switching there:

<% if current_user.is_a? Admin %>
<h1> Show Admin Stuff! </h1>
<% end %>

But to answer your question, you need to specify which template to render. This should work if you set up your controller's @action_name. You could do this in your render_by_user method instead of using a local action variable:

def render_by_user
self.action_name = "#{current_user.class.to_s.downcase}_#{self.action_name}"
if self.respond_to?(self.action_name)
instance_variable_set("@#{current_user.class.to_s.downcase}", current_user) # e.g. set @model to current_user
self.send(self.action_name)
else
flash[:error] ||= "You're not authorized to do that."
redirect_to root_path
end
end

Best way to deal with an error caused by a method returning no values?

you can also check if that value is present with if

<tr class="event-row" >
<td><%= event.description %></td>
<td><%= event.contact.name if event.contact %></td>
<td><%= event.start.strftime('%H:%M') if event.start %></td>
<td><%= event.end.strftime('%H:%M') if event.end %></td>
<td><%= link_to "edit", edit_event_path(event) %></td>
<td><%= link_to "delete", event, method: :delete, data: {confirm: "Are you sure?"} %>
<td><%= link_to "show", event_path(event) %></td>
</tr>

You can use try
Try will returns nil rather than raising an exception

event.try(:description) 
event.try(:contact).try(:name)
event.try(:start)
event.try(:end)

For more clarification, have a look at this

try public method



Related Topics



Leave a reply



Submit