How do I create this link_to conditional in a DRY way? - ruby-on-rails

I want to do the following:
<% if current_user.has_role? :demo %>
<%= link_to profile_path(#selected_profile) do %>
<% else %>
<%= link_to profile_path(profile) do %>
<% end %>
What's throwing it off is the beginning of the block in the link_to, within the if statement.
So how do I achieve this without having to duplicate ALL of the code within this if block twice?
Edit 1
This is the error I get from the above code:
SyntaxError at /
syntax error, unexpected keyword_else, expecting keyword_end
'.freeze; else
^

You can do it like this:
<% chosen_profile = current_user.has_role?(:demo) ? #selected_profile : profile %>
<%= link_to profile_path(chosen_profile) %>
So this will not repeat your link_to tag which you need to do. As you have to redirect to the same path and just change the profile object then this will work. You may change the ternary to if else block if the line seems very long and not readable.
And as everyone mentioned that don't use a do after link_to until you need a block. So that will fix your error.

You can achieve this by defining method in you user.rb (Model)
def demo?
self.has_role?("demo")
end
Then you write in your view
<% if current_user.demo? %>
<%= link_to profile_path(#selected_profile) %>
<% else %>
<%= link_to profile_path(profile) %>
<% end %>
This may help you.

do should have end.
Here is the Ruby Doc reference for link_to
Here is more about do in Ruby
<% if current_user.has_role? :demo %>
<%= link_to profile_path(#selected_profile) do %>
selected profile
<% end %>
<% else %>
<%= link_to profile_path(profile) do %>
profile
<% end %>
<% end %>

You are getting error because of do, you are opening the block but not closing it, try this code
<% if current_user.has_role? :demo %>
<%= link_to 'Profile', profile_path(#selected_profile) %>
<% else %>
<%= link_to 'Profile', profile_path(profile) %>
<% end %>
or, you can do it in controller instead
#selected_profile = current_user.has_role?(:demo) ? #selected_profile : profile
and then in the view,
<%= link_to 'Profile', profile_path(#selected_profile) %>
Hope that helps!

Related

conditional in users/show page to display info or not based users having created it

This is the error I get:
Showing /home/ubuntu/workspace/UMUV/app/views/users/show.html.erb where line #6 >>raised:
undefined method `name' for nil:NilClass
This is my conditional in users/show:
<% if #profile_info.present? %>
<%= #profile_info.region.name %>
<% else %>
<%= #user.email%>
<% end %>
This is in the users/controller:
def show
#user = User.find(params[:id])
#profile_info = Profile.find_or_initialize_by(user_id: params[:id])
end
I basically want to know how to not have an error when i visit a user's profile page and the user hasn't updated their profile yet. Showing the page is working fine if i update the user's profile through "edit page" manually.
Please help with the conditional. I also have a feeling i can create method in user.rb or users/controller, but really dont know how to implement this conditional to do what i want it to.
Thank you
In your show page you can replace this :
<% if #profile_info.present? %>
<%= #profile_info.region.name %>
<% else %>
<%= #user.email%>
<% end %>
with this:
<% if #profile_info.present? %>
<%= #profile_info.region.try(:name) %>
<% else %>
<%= #user.try(:email)%>
<% end %>
and also go through this post: http://everydayrails.com/2011/04/28/rails-try-method.html
Well you can always test an initalized object vs a saved object using new_record? -- which would make your above view look more like:
<% if #profile_info.new_record? %>
<%= #user.email%>
<% else %>
<%= #profile_info.region.name %>
<% end %>
also you might be able to take advantage of try on part of your page, which fails gracefully if an attribute doesn't exist, like
#profile_info.region.try(:name)
Since your error is with the name #profile_info.region.name not being present you could change your conditional to:
<% if #profile_info.region.name.present? %>
<%= #profile_info.region.name %>
<% else %>
<%= #user.email%>
<% end %>
Or if you really want to test #profile_info.present? you could nest conditionals like this:
<% if #profile_info.present? %>
<% if #profile_info.region.name.present? %>
<%= #profile_info.region.name %>
<% end %>
<% else %>
<%= #user.email%>
<% end %>
I hope that helps!

Unable to get the value of a variable inside Rails erb tags

I want to understand the erb usage. In the below code I am unable to figure out how to get the value of (group.id) in the if clause using the erb tags.
This probably has a very basic solution but I am unable to get proper answers.
The below code gives me syntax error.
<% current_user.favorite_groups.to_a.each do |group| %>
<%= if (group.id).newfavorite_texts.exists?(id: text.id) %>
<%= group.name %>
<%= link_to # do something %>
<% else %>
<%= group.name %>
<%= link_to # do something else %>
<% end %>
<% end %>
Thanks in advance.
You should be get going with the below code
<% current_user.favorite_groups.to_a.each do |group| %>
<% if group.newfavorite_texts.exists?(id: text.id) %>
<%= group.name %>
<%= link_to # do something %>
<% else %>
<%= group.name %>
<%= link_to # do something else %>
<% end %>
<% end %>

Check if Spree Cart is empty

How can I check if the Spree cart is empty, so I can change the button if it isn't?
I thought there was a method line_items.count...
<% unless line_items.count > 0 %>
<%= link_to "<button>Empezar Pedido</button>".html_safe, "/shop" %>
<% else %>
<%= link_to "<button>Terminar Pedido</button>".html_safe, "/shop" %>
<% end %>
Thanks!
This is very simple I solved my this problem like this
In application_controller.rb
def load_cart
#order = current_order
end
In frontend in you case
<% unless #order.line_items.count > 0 %>
<%= link_to "<button>Empezar Pedido</button>".html_safe, "/shop" %>
<% else %>
<%= link_to "<button>Terminar Pedido</button>".html_safe, "/shop" %>
<% end %>
Thanks

Rails - view: Ruby code in link_tag

I'd like to have embededd ruby code as my link name. Right now I tried to implement it like this:
<% #user.each do |user| %>
<li><%= link_to '<%= user.familyname %>, <%= user.forename %> ', user %> </li>
<% end %>
But it's not working, Rails gives me a syntax error:
syntax error, unexpected $undefined, expecting ')'
...);#output_buffer.safe_concat('\', user %> </li>
... ^
What do I need to change in the syntax, so this Link will work?
you can not do <%= %> inside an <%= %>. you can try something like this:
<% #users.each do |employee| %>
<li><%= link_to "#{employee.familyname}, #{employee.forename}", employee %> </li>
<% end %>
Just simple
<%= link_to [user.familyname, user.forename].join(','), user_path %>

Rails if statement syntax

I've written the following ERB and am getting a syntax error at the question mark. This helper function from devise currently evaluates as false. What have I missed?
<%= if user_signed_in? %>
<%= render 'form' %>
<%= end %>
Try this :
<% if user_signed_in? %>
<%= render 'form' %>
<% end %>
If you do <%= ... %>, it will try to output the thing you put between the tags. But, if you do <% ... %>, then no output is processed, just the code is evaluated. If this is not working, then there is probably something wrong with your user_signed_in? helper method.
<%= will try to output your user_signed_in? helper, so try:
<% if user_signed_in? %>
<%= render 'form' %>
<% end %>
or even better (and less confusing):
<%= render 'form' if user_signed_in? %>
try this
<% if user_signed_in? %>
<%= render 'form' %>
<% end %>

Resources