简体   繁体   English

Rails在显示之前检查对象存在的方式

[英]Rails way to check for object existence before display

I want to find a nice way to check my objects before i display them in the view so I wont get errors. 我想找到一个很好的方法来检查我的对象,然后我在视图中显示它们,所以我不会得到错误。

This is my controler 这是我的控制者

 @user = User.find_by_username(params[:username])
 @profile = @user.profile 
 @questions = @user.questions

and this is my view 这是我的观点

 <% unless @profile.blank? %><%= link_to 'Edit Profile', :controller => 'profiles', :action => 'edit' %><% end %>


     <% unless @user.blank? %>
        Username:<%= @user.username %><br />
    Member Since:<%= @user.created_at.strftime("%d %B %Y")  %><br />
    <% end %>

  <% unless @profile.blank? %>
    First Name: <%= @profile.first_name %><br />
    Last Name: <%= @profile.last_name %><br /><br />
    About: <%= @profile.body %><br /><br />
    Location: <%= @profile.location %><br />
    Birthday: <%= @profile.birthday.strftime("%d %B %Y") %><br />
    <% end %>

As you can see, I'm using more than one checking of each kind ( check unless @profile.blank? ) and I think there will be a better way to do that. 正如你所看到的,我正在使用不止一种检查(检查除非@ profile.blank?),我认为会有更好的方法来做到这一点。

Is there any Rails way to do something smarter than the one I've come up with ? 是否有任何Rails方式可以做出比我想出的更聪明的事情?

Also

<%= if @object.present? %>

looks much simplier for me than 看起来比我简单得多

<%= unless @object.blank? %>

especially when we have more than one conditional statement ( && \\ || \\ and \\ or ). 特别是当我们有多个条件语句( && \\ || \\ and \\ or )时。

api.rubyonrails.org api.rubyonrails.org

As I see, there is no way you can skip this @.blank? 正如我所见,你无法跳过这个@ .blank? validation as you dont want to display the records if they are empty but I have few suggestions 验证,因为你不想显示记录,如果它们是空的,但我没有什么建议

1 - Make following sections as partials 1 - 将以下部分作为部分

<% unless @user.blank? %>
    Username:<%= @user.username %><br />
    Member Since:<%= @user.created_at.strftime("%d %B %Y")  %><br />
<% end %>

and

<% unless @profile.blank? %>
  First Name: <%= @profile.first_name %><br />
  Last Name: <%= @profile.last_name %><br /><br />
  About: <%= @profile.body %><br /><br />
  Location: <%= @profile.location %><br />
  Birthday: <%= @profile.birthday.strftime("%d %B %Y") %><br />
<% end %>

it will keep your view cleaner and will give you the flexibility of using them in anyware in your app 它可以让您的视图更清晰,并且可以让您灵活地在应用程序的任何软件中使用它们

2 - take the below line 2 - 走下面的一行

<% unless @profile.blank? %><%= link_to 'Edit Profile', :controller => 'profiles', :action=> 'edit' %><% end %>

inside the profile display as its more appropriate 在配置文件显示内部更合适

cheers 干杯

sameera sameera

According to the rails documentation, you can see if any associated objects exist by using the association.nil? 根据rails文档,您可以使用association.nil查看是否存在任何关联对象? method: 方法:

if @book.author.nil?
  @msg = "No author found for this book"
end

How about building empty profile for user before saving it? 在保存之前如何为用户构建空配置文件? How about using analogy with exclamation mark which will raise ActiveRecord::RecordNotFound which in turn will display 404 page. 如何使用带有感叹号的类比来提升ActiveRecord::RecordNotFound ,而ActiveRecord::RecordNotFound又将显示404页面。

PS I'd also recommend trimming controller down to one instance variable. PS我还建议将控制器修剪为一个实例变量。

Probably the rails approach would be to use Object#presence 可能rails的方法是使用Object#presence

you should always unless @object.blank? 你应该总是unless @object.blank? as you did. 就像你做的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM