Ruby on rails 干式布局和视图
我有一个带有侧边栏的用户配置文件页面。我需要在配置文件中创建更多页面。例如,编辑密码、编辑个人资料信息、统计数据、购买历史记录列表等。我不知道如何在保持干燥的同时继续操作。除了主要内容外,我正在努力使所有内容都完全相同。在阅读一些教程时,我遇到了Ruby on rails 干式布局和视图,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,我有一个带有侧边栏的用户配置文件页面。我需要在配置文件中创建更多页面。例如,编辑密码、编辑个人资料信息、统计数据、购买历史记录列表等。我不知道如何在保持干燥的同时继续操作。除了主要内容外,我正在努力使所有内容都完全相同。在阅读一些教程时,我遇到了yield,但它主要用于application.html.erb渲染导航、页脚等。我不知道如何将其用于“子视图” 我现在做这件事的方式似乎是错误的 路线: as :user do # Routes to change password of
yield
,但它主要用于application.html.erb
渲染导航、页脚等。我不知道如何将其用于“子视图”
我现在做这件事的方式似乎是错误的
路线:
as :user do
# Routes to change password of signed in user
get 'user/password' => 'users/registrations#edit_password', as: 'edit_password'
# Routes to change user profile information of signed in users
get 'user/profile' => 'users/registrations#edit_profile', as: 'user_profile'
end
观点:
views\users\show.html.erb
:
views\users\registrations\edit\u profile.html.erb
:
views\users\registrations\edit\u password.html.erb
:
都包含这一行
<%= render 'users/shared/profile' %>
基本上,我想做的是保留所有周围的布局,但更改渲染内容。现在我需要补充更多,扩展这个if语句似乎真的是错误的 根据您提供的代码片段,最干燥的方法是移动
渲染后直接转到
show.html.erb
页面shared/profile
。其他视图也是如此。根据您提供的代码片段,最干燥的方法是移动
渲染后直接转到
show.html.erb
页面shared/profile
。其他视图也是如此。是的,这绝对不是一个好办法,但你能认识到这一点很好,所以不用担心。正如您所猜测的,实现这一点的方法包括使用布局和yield
。你可以阅读有关收益率的文章
默认情况下,整个Rails应用程序都可以使用类似于application.rb
的布局,但也可以定义嵌套在此布局中的布局。这在与上述相同的导轨中进行了说明
这样,整个应用程序中相同的内容在应用程序
布局中定义,用户配置文件中相同的内容在用户
布局中定义,特定于每个视图的内容在其中定义
旁注:由于用户
版面位于版面
文件夹中,我的行为就像您将\u侧边栏
部分移动到了那里,因为它实际上是属于版面的部分,应该在它附近
视图/layouts/users.html.erb
<%= render '_sidebar' %>
<%= yield :users_content %>
<%= render template: 'layouts/application' %>
<% content_for :users_content do %>
put the view code specific to users/show here
<% end %>
<% content_for :users_content do %>
put the view code specific to editing a user registration here
<% end %>
views/users/show.html.erb
<%= render '_sidebar' %>
<%= yield :users_content %>
<%= render template: 'layouts/application' %>
<% content_for :users_content do %>
put the view code specific to users/show here
<% end %>
<% content_for :users_content do %>
put the view code specific to editing a user registration here
<% end %>
将特定于用户的视图代码/显示在此处
视图/用户/注册/编辑.html.erb
<%= render '_sidebar' %>
<%= yield :users_content %>
<%= render template: 'layouts/application' %>
<% content_for :users_content do %>
put the view code specific to users/show here
<% end %>
<% content_for :users_content do %>
put the view code specific to editing a user registration here
<% end %>
将特定于编辑用户注册的视图代码放在此处
等等
您可能遇到的唯一问题是Rails使用控制器的名称来匹配嵌套的
用户
布局,如果这是另一个控制器,则注册可能会中断。您可以通过在这些控制器操作中显式调用渲染模板:“layouts/users”
来解决这个问题。是的,这肯定不是解决的办法,但是您应该认识到这一点,所以不用担心。正如您所猜测的,实现这一点的方法包括使用布局和yield
。你可以阅读有关收益率的文章
默认情况下,整个Rails应用程序都可以使用类似于application.rb
的布局,但也可以定义嵌套在此布局中的布局。这在与上述相同的导轨中进行了说明
这样,整个应用程序中相同的内容在应用程序
布局中定义,用户配置文件中相同的内容在用户
布局中定义,特定于每个视图的内容在其中定义
旁注:由于用户
版面位于版面
文件夹中,我的行为就像您将\u侧边栏
部分移动到了那里,因为它实际上是属于版面的部分,应该在它附近
视图/layouts/users.html.erb
<%= render '_sidebar' %>
<%= yield :users_content %>
<%= render template: 'layouts/application' %>
<% content_for :users_content do %>
put the view code specific to users/show here
<% end %>
<% content_for :users_content do %>
put the view code specific to editing a user registration here
<% end %>
views/users/show.html.erb
<%= render '_sidebar' %>
<%= yield :users_content %>
<%= render template: 'layouts/application' %>
<% content_for :users_content do %>
put the view code specific to users/show here
<% end %>
<% content_for :users_content do %>
put the view code specific to editing a user registration here
<% end %>
将特定于用户的视图代码/显示在此处
视图/用户/注册/编辑.html.erb
<%= render '_sidebar' %>
<%= yield :users_content %>
<%= render template: 'layouts/application' %>
<% content_for :users_content do %>
put the view code specific to users/show here
<% end %>
<% content_for :users_content do %>
put the view code specific to editing a user registration here
<% end %>
将特定于编辑用户注册的视图代码放在此处
等等
您可能遇到的唯一问题是Rails使用控制器的名称来匹配嵌套的用户
布局,如果这是另一个控制器,则注册可能会中断。您可以通过在这些控制器操作中显式调用呈现模板:'layouts/users'
来修复此问题