inimene
inimene

Reputation: 1650

Trying to list info from multiple models in one view

So I have this database where a user can borrow a book. We have books, users and people who borrow.

The code can be found here: https://gist.github.com/Veske/8490542

I am wondering, how can I make it so that I display all the books in borrows view, and then also be able to select thoes books for borrowing for my self or another user?

If I try to make a book instance variable in borrow controller for the view, hell gets loose. So I really have no idea now..

Edit: the @book in the view is not needed anymore as it did not work, i had a action in controller for it before.

This is my controller:

class BurrowsController < ApplicationController
  before_action :signed_in_user, only: [:index,:edit,:update, :destroy]
  before_action :admin_user,     only: :destroy

  def index
    @burrow = current_user.burrows.build
    @burrows = Burrow.all
  end

  def show
    @burrow = Burrow.find(params[:id])
  end

  def new
    @burrow = current_user.burrows.build
  end

  def create
    @burrow = current_user.burrows.build(burrow_params)
    if @burrow.save
      flash[:success] = "Burrowing a book was successful!"
      redirect_to @burrow
    else
      render current_user
    end
  end

  # Private section, makes the page unable to be seen for non logged in users
  private
  def burrow_params
    params.require(:burrow).permit(:user_id, :book_id)
  end
   def admin_user
      redirect_to(root_url) unless current_user.admin?
    end
  # Redirecting not logged in user etc.
  def signed_in_user
    unless signed_in?
      store_location
      redirect_to '/sessions/new', notice: "Please sign in!"
    end

  end

end

And this is my view for creating a new borrow entry:

<% provide(:title, "Burrow a book") %>

<b align="center">Choose the name of a book you want to burrow and enter 'Submit!'</b>

    <%= form_for(@burrow) do |f| %>
            <div class="forms">
                        <%= f.text_field :book_id, placeholder: "Type in the name of the book...", autofocus: true %>
                        <%= f.submit 'Submit!' %>
                </div>
        <% end %>

The view is bad currently, i am experimenting with absolutly everything all the time right now and I just don't understand what needs to be done.

Borrows index:

<% provide(:title, 'All burrowers') %>

<h2 align="center">All borrowers</h2><
<table align="center">
        <tr>
                <td align="left"><b>Who borrowed</b></td>
                <td align="left"><b>Borrowed what</b></td>
                <% if current_user.admin? && !current_user?(@user) %>
                <td align="left"><b>Admin functions</b></td>
                <% end %>
        </tr>
  <% @burrows.each do |burrow| %>
         <tr>
                <td align="left"><%= link_to burrow.user.name, burrow.user %></td>
                <td align="left"><%= link_to burrow.book.name, burrow.book %></td>   
                <% if current_user.admin? && !current_user?(@user) %>
                <td>
                <%= link_to "Delete this user", burrow, method: :delete, data: { confirm: "You sure?" } %>
                </td>
          <% end %>
         </tr>
 <% end %>
</table>

Upvotes: 0

Views: 62

Answers (1)

benjaminjosephw
benjaminjosephw

Reputation: 4417

One possible solution for this would be to use a select_tag list like this:

<%= form_for(@burrow) do |f| %>
  <div class="forms">
    <%= f.select("book_id", Book.all.collect {|b| [ b.name, b.id ] }, { include_blank: true }) %>
    <%= f.submit 'Submit!' %>
  </div>
<% end %>

Is that what you were looking for?

BTW - I think you mean 'borrow' rather than 'burrow'

Upvotes: 1

Related Questions