根据rails中的路由参数加载部分

时间:2015-06-19 14:09:44

标签: html ruby-on-rails ruby

是否可以根据通过路线的参数加载部分?我已经创建了一个登陆页面模板,我想基于行业稍微调整主要消息。更复杂的是,参数必须是可选的。

例如,如果URL是

  • www.domain.com/medical我想在landing/medical中呈现部分layouts/header.html.erblayouts/application.html.erb
  • www.domain.com/legal我想在landing/legal中呈现部分layouts/header.html.erblayouts/application.html.erb
  • www.domain.com呈现默认的部分landing/default.html.erb

以下是我当前观点的一个例子:

着陆/ medical.html.erb

<div class="heading">
  <h1>Are you HIPAA Compliant?</h1>
  Find out more by signing up below...
</div>

着陆/ legal.html.erb

<div class="heading">
  <h1>Your Legal Practice needs help</h1>
  <p class="leadFind out why by by signing up below...
</div>

着陆/ default.html.erb

<div class="heading">
  <h1>What are you doing?</h1>
  Probably nothing...
</div>

布局/ header.html.erb

<div class="heading">
  <%= render 'the/optional/partial' %>
</div>

布局/ application.html.erb

<!DOCTYPE html>
<html>
<head>
  <title><%= full_title(yield(:title)) %></title>
  <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
  <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
  <%= csrf_meta_tags %>
  <%= render 'layouts/shim' %>

  <% if content_for?(:head) %>
    <%= yield(:head) %>
    <% end %>

</head>
<body>

    <div class="site-wrapper">
        <div class="site-wrapper-inner">

        <%= render 'layouts/header' unless @disable_heading %>

            <div class="cover-container">

                <div class="inner cover">
                    <%= yield %>
                </div>

            </div>
        </div>
        <%= render 'layouts/footer' %>
    </div>

    <%= debug(params) if Rails.env.development? %>

</body>
</html>

这可能吗?

1 个答案:

答案 0 :(得分:3)

我认为缺少一些信息,可能会导致不同的方法。

您可以在路线中指定参数:

<强>的routes.rb

get '/(:industry_param)' => 'welcome#index'

有了这个,假设您的网站根目录即将转到该路由,您现在可以访问可用于有条件地呈现相应模板的变量params[:industry_param]

<强> welcome_controller.rb

@industry = params[:industry_param] || 'default'

layouts / header.html.erb

<div class="heading">
  <%= render "landing/#{@industry}" %>
</div>

我想你可以从这开始。有一些安全考虑因素,如过滤控制器中的参数,只接受某些值等,