为什么我的application.html.erb中的if语句不能像我期望的那样工作?

时间:2016-06-22 06:04:44

标签: ruby-on-rails erb actionview ruby-on-rails-5

在我的application.html.erb中,我有以下内容:

      <% if (controller_name == "questions" && action_name == "index") || (controller_name == "jobs" && action_name = "index") %>
        <%= "This is the IF for #{controller_name} and #{controller.action_name}" %>
            <%= yield %>
      <% elsif current_page?(controller: "jobs", action: "show") %>
        <%= "This is the ELSIF for #{controller_name} and #{controller.action_name}" %>
          <div class="wrapper wrapper-content article">
            <%= yield %>
          </div>
      <% else %>
        <%= "This is the ELSE for #{controller_name} and #{controller.action_name}" %>
       <% end %>

然后我导航到Jobs#Show页面,在此处查看日志:

Started GET "/jobs/social-media-manager" for 127.0.0.1 at 2016-06-22 01:00:05 -0500
Processing by JobsController#show as HTML
  Parameters: {"id"=>"social-media-manager"}
  Job Load (1.5ms)  SELECT  "jobs".* FROM "jobs" WHERE "jobs"."slug" = $1 ORDER BY "jobs"."id" ASC LIMIT $2  [["slug", "social-media-manager"], ["LIMIT", 1]]
  Rendering jobs/show.html.erb within layouts/application
  User Load (1.6ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 1546], ["LIMIT", 1]]
  Role Load (1.7ms)  SELECT "roles".* FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = $1 AND (((roles.name = 'admin') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL)))  [["user_id", 1546]]
  Rendered jobs/show.html.erb within layouts/application (18.8ms)
/app/views/layouts/application.html.erb:21: warning: found = in conditional, should be ==
  Rendered shared/_navbar.html.erb (2.6ms)
  Rendered shared/_footer.html.erb (0.8ms)
Completed 200 OK in 228ms (Views: 218.1ms | ActiveRecord: 4.8ms)

然而,当我进入视图时,我看到了:

  </nav>
</div>

            This is the IF for jobs and show
                <div class="row">
    <div class="col-lg-10 col-lg-offset-1">

所以即使我的服务器日志告诉我我向Jobs#Show发送了一个请求,但IF语句似乎没有正确评估条件,所以我的布局坏了。

我原本希望看到:

This is the ELSIF for jobs and show,而不是IF种。

我在这里缺少什么?

3 个答案:

答案 0 :(得分:2)

你在第二个条件下错过了一个=

<% if (controller_name == "questions" && action_name == "index") || (controller_name == "jobs" && action_name = "index") %>

将其更改为

<% if (controller_name == "questions" && action_name == "index") || (controller_name == "jobs" && action_name == "index") %>

答案 1 :(得分:2)

这是因为您没有在第一个条件中检查var request = require('request'); var http = require('http'); var https = require('https'); module.exports = { main: function(req, res){ var rs = "Someone"; var options = { hostname: 'thomas-bayer.com', port: 80, path: '/sqlrest', method: 'GET' }; http.request(options, function(response) { sails.log.debug('log:'+response); rs = response; res.send(rs); }); } }; ,而是设置它。

action_name

另请参阅服务器日志:

  

警告:发现=有条件的,应该是==

答案 2 :(得分:1)

<GridView ItemsSource="{Binding Source={StaticResource cvs}}" ScrollViewer.HorizontalScrollBarVisibility="Visible"
          ScrollViewer.HorizontalScrollMode="Enabled">
    <GridView.GroupStyle>
        <GroupStyle>
            <GroupStyle.HeaderTemplate>
                <DataTemplate>
                    <TextBlock x:Name="Header" FontSize="15" FontWeight="Bold" Text="{Binding Key}" />
                </DataTemplate>
            </GroupStyle.HeaderTemplate>
        </GroupStyle>
    </GridView.GroupStyle>
    <GridView.ItemsPanel>
        <ItemsPanelTemplate>
            <ItemsWrapGrid Orientation="Vertical" />
        </ItemsPanelTemplate>
    </GridView.ItemsPanel>
</GridView>

应该是

<% if (controller_name == "questions" && action_name == "index") || (controller_name == "jobs" && action_name = "index") %>

请注意<% if (controller_name == "questions" && action_name == "index") || (controller_name == "jobs" && action_name == "index") %> action_name = "index"

中从分配到相等的更改