重构我的代码,我怎么能在一个块中写这些行?

时间:2022-06-25 20:44:08
<% if user_signed_in? %>
    <li><%= link_to('Logout', destroy_user_session_path, :method => :delete) %>  </li> 
    <li><%= link_to('Edit registration', edit_user_registration_path) %> </li>
    <li><%= link_to 'Add Post', :controller=>'/posts', :action=>'new' %></li>
<% else %>
    <li><%= link_to('Login', new_user_session_path)  %></li>
<% end %>

Is it possible to write these line of code inside one block ? Can anyone post a shortcut way? Too much <% %> , <% end %>

是否可以在一个块内编写这些代码行?任何人都可以发布快捷方式吗?太多<%%>,<%end%>

2 个解决方案

#1


6  

What's wrong with doing it like this?

这样做有什么不对?

<% if user_signed_in? %>
    <li><%= link_to('Logout', destroy_user_session_path, :method => :delete) %>  </li> 
    <li><%= link_to('Edit registration', edit_user_registration_path) %> </li>
    <li><%= link_to 'Add Post', :controller=>'/posts', :action=>'new' %></li>
<% else %>
    <li><%= link_to('Login', new_user_session_path)  %></li>
<% end %>

#2


3  

Your code is pretty normal for ERB. If you would like to reduce some extra formating you can use HAML:

对于ERB,您的代码非常正常。如果您想减少一些额外的格式化,可以使用HAML:

- if user_signed_in?
  %li= link_to('Logout', destroy_user_session_path, :method => :delete)
  %li= link_to('Edit registration', edit_user_registration_path)
  %li= link_to 'Add Post', :controller=>'/posts', :action=>'new' 
- else 
  %li= link_to('Login', new_user_session_path)

To use or not HAML imho is a matter of taste.

使用或不使用HAML imho是一个品味问题。

#1


6  

What's wrong with doing it like this?

这样做有什么不对?

<% if user_signed_in? %>
    <li><%= link_to('Logout', destroy_user_session_path, :method => :delete) %>  </li> 
    <li><%= link_to('Edit registration', edit_user_registration_path) %> </li>
    <li><%= link_to 'Add Post', :controller=>'/posts', :action=>'new' %></li>
<% else %>
    <li><%= link_to('Login', new_user_session_path)  %></li>
<% end %>

#2


3  

Your code is pretty normal for ERB. If you would like to reduce some extra formating you can use HAML:

对于ERB,您的代码非常正常。如果您想减少一些额外的格式化,可以使用HAML:

- if user_signed_in?
  %li= link_to('Logout', destroy_user_session_path, :method => :delete)
  %li= link_to('Edit registration', edit_user_registration_path)
  %li= link_to 'Add Post', :controller=>'/posts', :action=>'new' 
- else 
  %li= link_to('Login', new_user_session_path)

To use or not HAML imho is a matter of taste.

使用或不使用HAML imho是一个品味问题。