根据对象属性在视图中创建条件?

时间:2016-06-05 20:49:51

标签: javascript ruby-on-rails ruby

如果一个人勾选布尔值:someday我们如何触发条件?

<%= simple_form_for(@challenge, html: { data: { modal: true } })  do |f| %>
<%= f.check_box :someday %> # If checkmarked show first condition else show second condition
<% if :someday == true %> # How to Write this line?
  <%= link_to 'Visit London', create_challenge_path(challenge: {name: 'Visit London', categorization: 'adventure', category: 'goal'}), class: "featured-challenge" %>
<% else %>
  <%= button_tag(type: 'submit', class: "btn", id: "challenge-button")  do %>
    Save
  <% end %>
<% end %>

1 个答案:

答案 0 :(得分:1)

当用户选中该复选框时,您希望显示<%= link_to 'Visit London', create_challenge_path(challenge: {name: 'Visit London', categorization: 'adventure', category: 'goal'}), class: "featured-challenge" %>,否则您想要显示<%= button_tag(type: 'submit', class: "btn", id: "challenge-button") %>,是否正确?

我认为&#39; Save&#39;按钮将始终可见,我假设您的复选框的默认状态为“假”&#39;。

您可以在屏幕上隐藏.featured-challenge,如果选中该复选框则检查jQuery:

.featured-challenge { display: none; }

你的JS中的某个地方(特色是challenge.js):

$(document).ready(function(){
    $('input[type="checkbox"]').click(function(){
        $(".featured-challenge").toggle();
    });
});