HAML形式的IF语句不能按预期工作

时间:2014-05-26 18:21:01

标签: ruby-on-rails if-statement haml

我有一个“接受/拒绝”工作流程(通过Cocoon使用嵌套表单创建)。基本上,

如果 1)当前用户是受让人(current_user.email == :assignee),2)当前用户不是创建者(current_user.email != :creator),3)任务有未被接受(true)或被拒绝(false)即nil(:accepted != nil),

然后 1)显示创建者(:creator)和2)启用字段接受或拒绝(f.select :accepted

当我使用这些if语句时,无论条件是真还是假,都会显示字段(creator& accept / reject)。另外,我不能在if语句中使用current_user.email,即使我可以将它打印得很好。

这是我的代码:

_task_fields.html.haml(views> projects)

.nested-fields
    .field
        = f.label :description
        = f.text_field :description
    .field
        = f.label :done
        = f.check_box :done
    - if (:accepted != nil) && (:creator != nil)
        .field
            = f.label :creator
            = f.text_field :creator, :disabled => true
        .field
            = f.label :accepted, "Accept or Reject"
            = f.select :accepted, options_for_select([nil,['Accept',true],['Reject',false]])
    .field
        = current_user.email
        = f.label :priority
        - if :priority == nil
            = f.select :priority, options_for_select(["None","Low","High"],"None")
        - else
            = f.select :priority, options_for_select(["None","Low","High"],:priority)
    .field
        = f.label :assignee
        = f.select :assignee, User.pluck(:email), :prompt => "Select One"
    = link_to_remove_association "remove task", f, :class => "btn btn-default btn-sm"

_form.html.haml(views> projects)

= form_for @project do |f|
  .jumbotron
    %h3 Project Settings
    .field
      = f.label :title
      = f.text_field :title

    #category
    .form_row
      = f.label :category
      - if @project.category == "process"
        = f.radio_button :category, "process", :checked => 'checked'
        = "process"
        = f.radio_button :category, "checklist", :checked => @project.category == "checklist"
        = "checklist"
      - else @project.category == "checklist"
        = f.radio_button :category, "process", :checked => @project.category == "process"
        = "process"
        = f.radio_button :category, "checklist", :checked => 'checked'
        = "checklist"

    .field
      = f.label :description
      = f.text_field :description

  - if @project.category == "process"
    %h3 Steps
    #steps
      = f.fields_for :steps do |step|
        %ul.list-group
          = render 'step_fields', :f => step
      .links
        = link_to_add_association 'add step', f, :steps
  - if @project.category == "checklist"    
    #tasks
      = f.fields_for :tasks do |task|
        .jumbotron
          = render 'task_fields', :f => task
      .links
        .jumbotron
          = link_to_add_association 'add task', f, :tasks, :partial => 'projects/new_task_fields', :class => "btn btn-default btn-sm"
  = f.submit

projects_controller.rb(只是相关部分)

def update
    respond_to do |format|
      if @project.update(project_params)
        format.html { redirect_to @project, notice: 'Project was successfully updated.' }
        format.json { render :show, status: :ok, location: @project }
        format.js
      else
        format.html { render :edit }
        format.json { render json: @project.errors, status: :unprocessable_entity }
        format.js
      end
    end
  end

def project_params
      params.require(:project).permit(:title, :category, :description, tasks_attributes: [:id, :description, :done, :priority, :assignee, :creator, :created_at, :accepted, :completed_at, :_destroy])
    end

schema.rb(只是相关部分)

create_table "tasks", force: true do |t|
    t.string   "description"
    t.boolean  "done"
    t.integer  "project_id"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.datetime "due"
    t.boolean  "started"
    t.boolean  "active"
    t.string   "repeat"
    t.integer  "time_limit"
    t.string   "priority"
    t.string   "assignee"
    t.string   "creator"
    t.datetime "started_at"
    t.datetime "completed_at"
    t.datetime "paused_at"
    t.datetime "resumed_at"
    t.integer  "time_spent"
    t.boolean  "accepted"
  end

  add_index "tasks", ["project_id"], name: "index_tasks_on_project_id"

如果需要我可以添加更多文件,但我怀疑我的控制器/型号似乎是个问题。感谢并感谢任何意见。

1 个答案:

答案 0 :(得分:4)

- if (:accepted != nil) && (:creator != nil)

上述陈述将永远为真,因为您正在检查符号是否不等于nil,这总是正确的

您应该查看类似

的内容
- if (@project.accepted != nil && @project.creator != nil)