0

I have a class Shift and a form to create a new shift. I want to be able to create many shifts at once, back-to-back so that in the form I only have to choose a date and a start time and how many shift should be created. A shift is 30 minutes long.

I now have a custom controller method that calls the create method within a loop determined by the param :block that the user chooses. I have now stopped getting error messages, but it turn I don't know what happens when I submit the form, which seems to be nothing. My question is: how can I modify my create_block method so that it does what I want?

Shifts_controller:

  def create
    @shift = Shift.new(shift_params)
    if @shift.save
      #redirect_to shifts_url
    else
      render 'new'
    end
  end

  def create_block
    start = params[:start_time]
    stop = params[:start_time] + 30.minutes
    block = params[:block]
    for number in 1..block do
      Shift.create(date:params[:date], start_time:start, stop_time:stop)
      start = stop
      stop = stop + 30.minutes
    end
    redirect_to shifts_url
  end

_form.html.erb

<%= form_for @shift, :url => create_block_path(@shift) do |f| %>
<%= render 'shared/error_messages', object: f.object %>


<div class="field">
    <%= f.label :Datum %>
    <%= f.date_select :date %>
  </div>
  <br>
  <div class="field">
    <%= f.label :Börjar %>
    <%= f.time_select :start_time, {minute_step: 30} %>
  </div>
  <br>
  <div>
    <%= f.label :Antal %>
    <%= f.select :block, options_for_select(1..10) %>
  </div>
  <br>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

routes.rb

resources :shifts do
  member do
    patch 'book/' => 'shifts#book', as: 'book'
    patch 'un_book/' => 'shifts#un_book', as: 'un_book'
  end
end
get 'shifts/:id/book' => 'shifts#book'
get 'shifts/:id/un_book' => 'shifts#un_book'
match '/shifts/new' => 'shifts#new', as: 'create_block', via: [:post]

1 Answer 1

1

Your routes are routing the create_block action to ShiftsController#new. You need to route it to ShiftsController#create_block.

Try:

resources :shifts do
  member do
    patch 'book/' => 'shifts#book', as: 'book'
    patch 'un_book/' => 'shifts#un_book', as: 'un_book'
  end
  collection do
    post 'create_block' => 'shifts#create_block', as: 'create_block'
  end
end
get 'shifts/:id/book' => 'shifts#book'
get 'shifts/:id/un_book' => 'shifts#un_book'
Sign up to request clarification or add additional context in comments.

2 Comments

I'd add in that in the OP's example he's trying to call a post :new and that's "off the Rails". New already has a conventional meaning--preparing a single object for later creation. I think your answer is a great one. I will also add, I don't know why the two gets at the end aren't members of the shifts resource.
@jaydel, I agree with everything you say. The two routes at the end are already members of the :shifts route block as patches. I left them alone in my answer to clarify the one important change for create_block, and because I assumed they are there for testing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.