Reputation: 131
I'm currently making a website in Rails that will allow the user to create and account and then when logged in he/she will be get a list of all the "rooms" in the database. And then the user will be able to book a one of the rooms. Like a booking system.
So first when the user logges in it will get redirected to the controller booker index. That will get all the rooms from the database and display the information in a table.
The model situation is built up like the User has_many reservations, and the reservation belongs_to User.
So what I want to do is that next to the table (can be on the same row) there should be a click button. And when the user click the button the reservations is created and linked to the user without leaving the current page. Atm this is what I came up with (in the index.html.erb)
<%= form_for(@new_reservation) do |new_res| %>
<% @all_rooms.each do |r| %>
<tr>
<td><%= r.room %></td>
<td><%= r.room_size %></td>
<td><%= r.building %></td>
<td><%= r.room_description %></td>
<td><%= new_res.submit "Create" %></td>
</tr>
<% end %>
<% end %>
and my controller looks like
class BookerController < ApplicationController
before_action :require_user, only: [:index, :show]
def index
@new_reservation = Reservation.new
@all_rooms = Room.all
end
def create
end
end
and route
Rails.application.routes.draw do
#Log in / Logout functionality
get '/login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
#The start screen
root 'welcome#index'
#The signup
get '/signup' => 'users#new'
get '/book' => 'booker#index'
resources :users
resources :reservations
Upvotes: 1
Views: 1486
Reputation: 343
You need a few things to make this happen:
remote: true
BookerController#create
method, it needs to be accessible with a HTTP POST Working example:
app/views/booker/index.html.erb
<% @all_rooms.each do |r| %>
<tr>
<td><%= r.room_size %></td>
<td><%= r.building %></td>
<td><%= r.room_description %></td>
<td>
<%= form_for(Reservation.new, remote: true, format: :js, url: {controller: "booker", action: "create"}) do |new_res| %>
<%= new_res.hidden_field :room_id, value: r.id %>
<%= new_res.hidden_field :user_id, value: current_user.id %>
<%= new_res.submit "Book" %>
<% end %>
</td>
</tr>
<% end %>
Here, replace current_user.id
by whatever gives you the currently logged in user
app/controllers/booker_controller.rb
class BookerController < ApplicationController
def index
@all_rooms = Room.all
end
def create
new_reservation = Reservation.new(reservation_params)
new_reservation.save
respond_to do |format|
format.json { head :no_content }
end
end
private
def reservation_params
params.require(:reservation).permit(:room_id, :user_id)
end
end
app/config/routes.rb
Rails.application.routes.draw do
get '/book' => 'booker#index'
post '/book' => 'booker#create'
end
Upvotes: 1
Reputation: 9109
And when the user click the button the reservations is created and linked to the user without leaving the current page.
You need to learn up on how to make Ajax request in Rails.
Basically you will need to something like this
<%= form_for(@new_reservation, remote: remote) do |new_res| %>
Which will send data asynchronosly, you will create the reservation , and render a view for display
Upvotes: 0