Wednesday 15 July 2015

erb - Rails view conditionals if/elsif on user fields -


I am trying to show a table of user data on my site - that's why I'm using a loop It displays the user and their information before, it was really simple, but I wanted to add in a situation that handled matters where they did not have any city, state or even entry. When I have the same case registered, then it works fine when I have it, it works fine User Location is defined in the model of the city and state in a joint form. So do not worry there, but when I do not have it, it does not show '---' which I want. What am I missing here?

  & lt; Tr & gt; & Lt;% @ user.each do | User | & Gt%; & Lt; Td> & Lt;% = link_to user.username, public_profile_path (user.username)% & gt; & Lt; Br / & gt; & Lt; / Td> & Lt;% If user.state! = Zero and user.city! = Zero% & gt; & Lt; Td> & Lt;% = user.location% & gt; & Lt; / Td> & Lt;% elsif user.state! = Zero and user.city == zero%> & Lt; Td> & Lt;% = user.state% & gt; & Lt; / Td> & Lt;% elsif user.state == zero and user.city! = Zero% & gt; & Lt; Td> & Lt;% = user.city% & gt; & Lt; / Td> & Lt;% else% & gt; & Lt; TD & gt; --- & lt; / TD & gt; & Lt;% end% & gt; & Lt;% If user.phone! = Nil% & gt; & Lt; Td> & Lt;% = user.phone% & gt; & Lt; / Td> & Lt;% else% & gt; & Lt; TD & gt; - & lt; / TD & gt; & Lt;% end% & gt; & Lt; Td> & Lt;% = mail_to user.email.to_s, "Email this user" ,: cc = & gt; "Michaelomchenry@gmail.com" ,: Subject = & gt; "Contact overflow member"%> & Lt; / Td> & Lt; / TR & gt; & Lt;% end% & gt;  

I will add a helpful method, called location_display All the arguments around what should look what handles.

  def location_disk (user) return user.location if user.state & amp; Amp; User.city return user.state user.state & amp; Amp; User.city.blank? Return to User.city and use user.city & amp; Amp; User.state.blank? "----" End   

Again, in your opinion, just change all those lines like this:

   & Lt;% = location_display (user)%> & Lt; / Td>    

No comments:

Post a Comment