尝试在一个视图中列出多个模型的信息

时间:2014-01-18 13:34:14

标签: ruby-on-rails database view model

所以我有这个数据库,用户可以借书。我们有书籍,用户和借阅的人。

可以在此处找到代码:https://gist.github.com/Veske/8490542

我想知道,我怎样才能这样做,以便我在借阅视图中显示所有书籍,然后还可以为我自己或其他用户选择借书?

如果我试图在借用控制器中为视图创建一个book实例变量,那么地狱就会松动。所以我现在真的不知道......

编辑:视图中的@book不再需要了,因为它不起作用,我之前在控制器中有一个动作。

这是我的控制者:

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

这是我创建新借入条目的观点:

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

目前观点不好,我现在一直在试验绝对的一切,我只是不明白需要做些什么。

借入索引:

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

1 个答案:

答案 0 :(得分:1)

一种可能的解决方案是使用这样的select_tag列表:

<%= 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 %>

那是你在找什么?

顺便说一句 - 我认为你的意思是“借”而不是“挖洞”