I have to build a simple app that allows users to loan and borrow books. Simply put a User can create books, and they can pick another user to loan the book to.
I have three models User
, Book
and Loan
:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :books
has_many :loans, through: :books
has_many :borrowings, class_name: "Loan"
validates :username, uniqueness: true
validates :username, presence: true
end
class Book < ActiveRecord::Base
belongs_to :user
has_many :loans
validates :title, :author, presence: true
end
class Loan < ActiveRecord::Base
belongs_to :user
belongs_to :book
validates :user, :book, :status, presence: true
end
The LoansController looks like this:
class LoansController < ApplicationController
before_action :find_book, only: [:new, :create]
def new
@users = User.all
@loan = Loan.new
authorize @loan
end
def create
@loan = Loan.new
@loan.book = @book
@loan.user = User.find(loan_params[:user_id])
@loan.status = "loaned"
authorize @loan
if @loan.save
redirect_to :root
else
render :new
end
end
private
def loan_params
params.require(:loan).permit(:user_id)
end
def find_book
@book = Book.find(params[:book_id])
end
end
My form looks like:
<%= simple_form_for([@book, @loan]) do |f| %>
<%= f.input :user_id, collection: @users.map { |user| [user.username, user.id] }, prompt: "Select a User" %>
<%= f.submit %>
<% end %>
If I submit the form without selecting a user, and keep the "Select a User" prompt option, the form is submitted and the app crash because it can't find a user with id=
I don't know why the user presence validation in the form does not work...
you will change your Create method
def create
@loan = Loan.new
@loan.book = @book
@loan.user = User.find_by_id(loan_params[:user_id])
@loan.status = "loaned"
authorize @loan
if @loan.save
redirect_to :root
else
render :new
end
end