-4

I am having a syntax error on my API I dont understand. Inside my rooms_controller I have a multiple_create method responsible for creating multiple rooms :

  def multiple_create
    i = 0
    while i < params[:room_number]
      Room.create!(room_params)
      i++
    end
    if i == params[:room_number]
      render json: {message: "All rooms where successfully created"}
    else
      render json: {message: "There was a problem during room creation. Some rooms might not have been created"}
    end
  end

I am testing this method with a curl request :

curl -i -X POST                                      \
       -H 'Content-Type: application/json'              \
       -H 'X-User-Email: leon_kuhlman@zulauf.net'               \
       -H 'X-User-Token: 7B5RGzsosxekH-5p2tt5'          \
       -d '{ "room_number": 10, "room": {"content": "plein sud"} }'    \
      http://localhost:3000/api/v1/hotels/1/rooms/multiple_create

but I am getting a syntax error :

> syntax error, unexpected keyword_end
/Users/davidgeismar/code/davidgeismar/quick_bed_api/app/controllers/api/v1/rooms_controller.rb:68: syntax error, unexpected end-of-input, expecting keyword_end

app/controllers/api/v1/rooms_controller.rb, line 34
---------------------------------------------------

``` ruby
   29       i = 0
   30       num = 5
   31       while i < num
   32         Room.create!(room_params)
   33         i++
>  34       end
   35       if i == params[:room_number]
   36         render json: {message: "All rooms where successfully created"}
   37       else
   38         render json: {message: "There was a problem during room creation. Some rooms might not have been created"}
   39       end
``

I dont get why I am getting an error here. Here's my all controller if you can find it !

class Api::V1::RoomsController < Api::V1::BaseController

before_action :set_room, only: [ :show, :update]

  def index
    if params[:search].blank?
      @rooms = policy_scope(Room)
    else
      @slots = policy_scope(Room)
      .where('name ILIKE ?', "%#{params[:search]}%")
    end
  end

  def show
  end

  def update
    if @room.update(room_params)
      render :show
    else
      render_error
    end
  end


  ## input hotel + number of rooms

  def multiple_create
    i = 0
    num = 5
    while i < params[:room_number]
      Room.create!(room_params)
      i++
    end
    if i == params[:room_number]
      render json: {message: "All rooms where successfully created"}
    else
      render json: {message: "There was a problem during room creation. Some rooms might not have been created"}
    end
  end

  def create
    #ATTENTION LA LIGNE CI-DESSOUS DOIT ETRE CORRIGÉ POUR LIER ACCOUNT ET ROOM
    @room = current_user.rooms.build(room_params)
    authorize @room
    if @room.save
      render :show
    else
      render_error
    end
  end

private

  def set_room
      @room = Room.find(params[:id])
      authorize @room
  end

  def room_params
    params.require(:room).permit(:hotel_id, :content)
  end

  def render_error
    render json: { errors: @room.errors.full_messages }, status: :unprocessable_entity
  end

end
David Geismar
  • 3,152
  • 6
  • 41
  • 80
  • What is your question? – sawa Jan 27 '16 at 18:46
  • well I am asking if someone can see the syntax error. I thought it was pretty clear – David Geismar Jan 27 '16 at 18:48
  • 1
    Your code has some serious issues. What happens if `params[:room_number]` is 5 and you get an error on the 3rd iteration? You now have two rooms created in a failed request. You should think about using transaction, and removing some of the logic from the controller. Also, using something `params[:room_number].to_.each` – Mohamad Jan 27 '16 at 18:51
  • @Mohamad what is transaction? – David Geismar Jan 27 '16 at 18:54
  • @DavidGeismar transactions help you maintain your data integrity in case of failure; if anything goes wrong before the entire operation is complete, all changes will be rolled back, and the state of the database will be restored to how it was before the request was made. So you won't end up with two rooms if my scenario above comes to fruition, as they will be rolled back and won't be committed in to the database. https://www.google.com.br/search?q=database+transaction&gws_rd=cr,ssl&ei=exOpVsDGLoKHwQTWpLOYDg – Mohamad Jan 27 '16 at 19:01

2 Answers2

3

I believe your error is being caused by your use of i++ which is not valid ruby syntax. Try replacing that with i += 1

T J
  • 1,312
  • 7
  • 7
0

The problem is i++. Replace it with i += 1 or i = i + 1. Ruby doesn't support ++ operator.

Atri
  • 5,511
  • 5
  • 30
  • 40