Ruby on rails - 尝试使用 if else 语句检查数据库中是否存在记录时遇到"SyntaxError in AssignmentsController#show"错误



我正在尝试在分配中使用show.html.erb中的ifelse语句来检查投标中的记录是否存在。但是我遇到了语法错误"AssignmentsController#show中的语法错误",该错误发生在以下代码的第一行:

<%= if Bid.exists?(user_id: current_user.id, assignment_id: @assignment.id) %>
  <p>it exists</p>
<%= else %>
  <p>it doesn't exists</p>

以下是整个文件 Assignment/show.html.erb:

<p id="notice"><%= notice %></p>
<p>
  <strong>Assignment:</strong>
  <%= @assignment.assignment %>
</p>
<p>
  <strong>Education:</strong>
  <%= @assignment.education.education %>
</p>
<p>
  <strong>Subject:</strong>
  <%= @assignment.subject.subject %>
</p>
<p>
  <strong>Gender prefer:</strong>
  <%= @assignment.gender_prefer %>
</p>
<p>
  <strong>Timeslot:</strong>
  <%= @assignment.timeslot %>
</p>
<p>
  <strong>Duration:</strong>
  <%= @assignment.duration %>
</p>
<p>
  <strong>Budget:</strong>
  <%= @assignment.budget %>
</p>
<p>
  <strong>Budget unit:</strong>
  <%= @assignment.budget_unit %>
</p>
<p>
  <strong>Assignment info:</strong>
  <%= @assignment.assignment_info %>
</p>
<p>
  <strong>Assignment id:</strong>
  <%= @assignment.id %>
</p>
<%= if Bid.exists?(user_id: current_user.id, assignment_id: @assignment.id) %>
  <p>it exists</p>
<%= else %>
  <p>it doesn't exists</p>
<%= end %>
<%= link_to "Create Bid", bids_path(:status => "Pending", :assignment_id => @assignment.id, :user_id => current_user.id), :method => :post %> |
<%= link_to 'Bid', {controller: "bids", action: "new", id: @assignment.id} %> |
<%= link_to 'Bid', bid_path, method: :post %> |
<%= link_to 'Bid', edit_bid_path %> |
<%= link_to 'Edit', edit_assignment_path(@assignment) %> |
<%= link_to 'Back', assignments_path %>

以下是整个文件 assignments_controller.rb:

class AssignmentsController < ApplicationController
  before_action :authenticate_user!
  before_action :set_assignment, only: [:show, :edit, :update, :destroy]
  # GET /assignments
  # GET /assignments.json
  def index
    @assignments = Assignment.all
  end
  # GET /assignments/1
  # GET /assignments/1.json
  def show
  end
  # GET /assignments/new
  def new
    @assignment = Assignment.new
  end
  # GET /assignments/1/edit
  def edit
  end
  # POST /assignments
  # POST /assignments.json
  def create
    @assignment = Assignment.new(assignment_params)
    respond_to do |format|
      if @assignment.save
        format.html { redirect_to @assignment, notice: 'Assignment was successfully created.' }
        format.json { render :show, status: :created, location: @assignment }
      else
        format.html { render :new }
        format.json { render json: @assignment.errors, status: :unprocessable_entity }
      end
    end
  end
  # PATCH/PUT /assignments/1
  # PATCH/PUT /assignments/1.json
  def update
    respond_to do |format|
      if @assignment.update(assignment_params)
        format.html { redirect_to @assignment, notice: 'Assignment was successfully updated.' }
        format.json { render :show, status: :ok, location: @assignment }
      else
        format.html { render :edit }
        format.json { render json: @assignment.errors, status: :unprocessable_entity }
      end
    end
  end
  # DELETE /assignments/1
  # DELETE /assignments/1.json
  def destroy
    @assignment.destroy
    respond_to do |format|
      format.html { redirect_to assignments_url, notice: 'Assignment was successfully destroyed.' }
      format.json { head :no_content }
    end
  end
  private
    # Use callbacks to share common setup or constraints between actions.
    def set_assignment
      @assignment = Assignment.find(params[:id])
    end
    # Never trust parameters from the scary internet, only allow the white list through.
    def assignment_params
      params.require(:assignment).permit(:assignment, :education_id, :subject_id, :gender_prefer, :timeslot, :duration, :budget, :budget_unit, :assignment_info)
    end
end

我已经尝试了简单的 if else 语句,它仍然返回错误:

<%= if 1 > 0 %>
  <p>it exists</p>
<%= else %>
  <p>it doesn't exists</p>
<%= end %>

您不得在 erb 标签中放置带有 ifelseend= 符号。尝试这样的事情:

<% if 1 > 0 %>
  <p>it exists</p>
<% else %>
  <p>it doesn't exists</p>
<% end %>

<%= %>会将您的行的结果打印到生成的 html 中。因此,对于您不想打印的所有行(如ifelsefor语句,以及end),请改用<% %>

  • 如果你想了解更多关于 erb 标签的细节,有这个 SO 答案:在 Rails 中的 ERB 中,<<%%=、<%# 和 -%> 有什么区别?
不允许

添加带有if的=

如果像这样使用

<% if 1 > 0 %>
  <p>it exists</p>
<% end %>

最新更新