0

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...

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

I changed the User method to find_by_id and I do get model validations occurring and the new view rendered. Why wasn't it working with find? Also, by doing this, my @users array declared in the new action of LoansController is nil when the form is re-rendered, how can I get it to be set ?
I managed to get it set by calling again @users before rendering :new, everyhting's fine !

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.