2016-06-22 71 views
2

在我application.html.erb,我有以下幾點:爲什麼我的application.html.erb中的if語句不能像我所期望的那樣工作?

 <% 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,I F語句似乎沒有正確評估條件,所以我的佈局被破壞了。

我本來期望看到:

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

我在這裏錯過了什麼?

+0

存在這樣。可能是因爲日誌的警告。 'warning:found = in conditional,should be =='。在你的第一個條件中,你設置了'action_name =「index」'。 – IngoAlbers

+0

@IngoAlbers DUDE !!!我完全錯過了。非常感謝。就是這樣。你可以添加答案,我會接受它。 – marcamillion

回答

2

這是因爲您在第一種情況下不檢查action_name,而是對其進行設置。

action_name = "index"

參見服務器日誌:

警告:發現在條件=應==

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
<% 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"變化至action_name == "index"

相關問題