Refactoring from nested ifs to link_to_if - ruby-on-rails

I had the following if statement in a shared view:
<% if activity.holder.user.profile_type == "Manager" %>
DAMANAGER
<% elsif profile == "managers" %>
<%= link_to activity.holder.name, managers_enterprise_path(activity.holder) %>
<% else %>
<%= activity.holder.name %>
<% end %>
profile is a local string variable that will be "enterprises" or "managers" depending which profile is logged in!
activity is a local variable too from an #each statement
The view is shared between only two users with differents profiles (Enterprise and Manager), with this constraint:
If the managers is the owner of activity it should display DAMANAGER
When I'm seeing as manager unless the holder is the manager it should display a link to show the enterprise that owns this activity.
When I'm seeing as enterprise it should display, who owns the activity, the holder name. Without links.
So I struggled myself and refactor from that to this, that's what I got by now:
<%= link_to_if (profile == 'managers'), activity.holder.name, managers_enterprise_path(activity.holder) %>
But this doesn't display DAMANAGER and furthermore it shows the enterprise's id equal to manager's id, since it pass the manager to managers_enterprise_path. The question is:
How I can improve it?
Thanks in advance, tell me if you need more code!

I don't see a maintainable way to do it without if ... else ... end. The elsif ... else ... end part can be replaced with a link_to_if:
<% if activity.holder.user.profile_type == "Manager" %>
DAMANAGER
<% else %>
<%= link_to_if profile == "managers", activity.holder.name, managers_enterprise_path(activity.holder) %>
<% end %>

Related

Can't get if and else statement to work

I've been playing around with my if and else statement but it's always just providing one.
First try
<% if #albumable == #user %>
<%= link_to "Edit", edit_community_album_path(#albumable, album), class: "album_edit" %>
<% else %>
<%= link_to "Ediiiit", edit_user_album_path(#albumable, album), class: "album_edit" %>
<% end %>
Second try
<% if #community == #community_id %>
Albumable will either be a user_id or community_id. When I play around with the code I only get 1 result from the two for both the user album edit page and the community album edit page. I don't understand how I'll be able to making an if statement if it isn't within the community page, it should produce the else statement. All help is appreciated, thank you.
I would suggest refactoring this code to remove the if/else statement altogether. For example, the following provides the exact same functionality:
<%= link_to "Edit", [:edit, #albumable, album], class: "album_edit" %>
You can read more about this at http://guides.rubyonrails.org/routing.html#creating-paths-and-urls-from-objects
Compare the id values instead. Depending on how you loaded those variables with records, Ruby might not think they're equal.

Rails if statement for current user profile view and other users

Hi I'm having some trouble getting different views to display based on whether or not the user being viewed is the logged in user.
I'm using Rails 3.2 and Devise
I've tried the code below but it always returns the template for the logged in user (where I have edit info links) rather than the one for the other users.
<% if user = current_user %>
<%= render 'users/myprofile' %>
<% else %>
<%= render 'users/viewprofile' %>
<% end %>
Thanks very much
The statement should be..
<% if user == current_user %>
The reason being, = is for assignment, == is for comparison.
Check the sign of comparison. You have used equals to sign.
<% if user == current_user %>
= sign assigns the current user to user variable. Hence the condition will always be true unless there is no current user.

Creating a formula in a Rails view?

I would like to create a small formula in my view/home page. Basically it should say if current user signed in (got that part right) AND USER CREATED A REGISTRY = TRUE then this
or that.
Here is the code below:
<% if user_signed_in? and?????? %>
<%= link_to "Show My Registry", current_user.registry %>
<% else %>
<%= link_to "Create a new registry", new_registry_path %>
<% end %>
Thanks for any help.
Assuming Registry is a model that belongs to a User, you could use <% if user_signed_in? && current_user.registry.exists? %>
I assume you all ready have the relationship between Users and Registries established. If so your if so you could create a method in your users controller that looks something like.
def user_registry
User.current.registry
end
Then if your view your if statement should look something like
<% if user_signed_in? and user_registry? %>
<%= link_to "Show My Registry", current_user.registry %>
<% else %>
<%= link_to "Create a new registry", new_registry_path %>
<% end %>
You might want to nest the two decisions. What happens if the user is not signed in? Do you still want the user be able to create a registry without signing in? Other option would be to consider using a before_filter to ensure user is always signed in before she gets to this decision point in your app.

if current_user.id = #game.user_id

I have links to the show pages for each game in my project and if the
games user_id matches the id of the currently signed in user then I want
it to display the edit button if they are not then it shouldn't display.
I currently have the following code set but it doesn't work. Every game
has the edit button display. The code is as followed:
<% if current_user.id = #game.user_id %>
<div id="text3"><%= link_to 'Edit', edit_game_path(#game) %></div><br />
<% end %>
Any ideas?
MrDanA's answer is most probably the error, but you may want to make this code better. Checking like that is not the Rails way of doing it. Instead, make a User instance method like :
def has_game?(game)
self.games.exists?(:id => game.id)
end
and then in your view :
<% if current_user.has_game?(#game) %> ...
(can even be better by further delegating exists into the game model, as a scope or so, if you like)
You want ==
So:
<% if current_user.id == #game.user_id %>

What is the best way using multiple lines of <% %> Tag or <% %> Tag with multiple lines?

Sorry if the title is not enough to understand what i am asking about.
I am rails developer and i used multiple lines of <% %> in my views but now i realized that it's not best practice so i came here and like to you all excellent guys what is the correct way in ROR?
For example if i required to something like following
<% user =User.all %>
<% name= [] %>
<% count = 0 %>
<% for user in users %>
<% name << user.name %>
<% count+=1%>
<% end %>
Can i do it as follows ?
<% user =User.all
name= []
count = 0
for user in users
name << user.name
count+=1
end
%>
I know better way of collecting element from array But above is just example.
But my question is, is it possible and if yes which is the correct way?
I think the correct way is something totally different: move logic out of views.
This blog post explains what I mean.
in start and end must has '<%' or '%>'
Like:
<% users = User.all
name= []
count = 0
for user in users
count+=1
end %>
Using just a single pair of tags per code block is certainly preferable if only because it makes the output smaller.
The code should really rather look like
<% names = User.all.map(&:name) %>
Note that "count" can be obtained via names.size.
If you need to mix <% and <%= you need to switch:
<% for user in User.all %>
<%= user.name %></br>
<% end %>

Resources