0

I'm trying to use an if else statement in show.html.erb in the Assignment to check if a record in Bid exists. But I encountered a syntax error "SyntaxError in AssignmentsController#show" that occurs on the first line of the code below:

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

The following is the entire file 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 %>

The following is the entire file 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

I've tried simple if else statements which stills returns an error:

<%= if 1 > 0 %>
  <p>it exists</p>
<%= else %>
  <p>it doesn't exists</p>
<%= end %>
Undo
  • 25,204
  • 37
  • 102
  • 124
Benjamin
  • 293
  • 1
  • 2
  • 9

2 Answers2

2

You must not put the = sign in your erb tags with if, else or end. Try something like this :

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

<%= %> will print the result of your line into the generated html. So for all the lines that you don't want to print (like the if , else or for statements, and the end too), use <% %> instead.

Community
  • 1
  • 1
Caillou
  • 1,321
  • 7
  • 17
-1

you are not allowed to add = with if.

Use if like this

<% if 1 > 0 %>
  <p>it exists</p>
<% end %>
sohail khalil
  • 720
  • 9
  • 24