그래서이 사용자는 책을 빌릴 수있는이 데이터베이스를 가지고 있습니다. 우리는 책, 사용자 및 빌린 사람들이 있습니다. 보기를 빌려에서 나는 모든 책을 표시 할 수 있도록 https://gist.github.com/Veske/8490542이하나의보기에서 여러 모델의 정보를 나열하려고합니다.
가 내가 궁금가, 어떻게 그것을 만들 수 있습니다 하고 또한 내 자신에 대한 대출에 대한 thoes 책을 선택할 수 있습니다 :
코드는 여기에서 찾을 수 있습니다 또는 다른 사용자?
뷰의 컨트롤러를 빌려 책 인스턴스 변수를 만들려고하면 지옥이 느슨해집니다. 그래서 지금은 정말로 모른다. ..
편집 :보기에서 @ 책은 더 이상 작동하지 않아서 필요하지 않다. 나는 이전에 그것을 위해 컨트롤러에서 행동했다.
이
내 컨트롤러 :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 %>
뷰, 난 절대적으로 모든 것을 항상 현재 나쁜 실험을하고있다 지금 당장해야 할 일이 무엇인지 이해하지 못합니다.
는 차용 지수 :
<%= 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 %>
당신이 찾고 있던 무엇인가요 :
<% 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>
코드에 연결하지 마십시오. 질문에 넣어. – Mischa
원래 게시물을 편집했습니다. 뷰 부분은 항상 나를 변화시키고 있으므로 구조를 대략적으로 추정하는 것입니다. 그러나 그래 .. 그것은 모든 책들 사이에서 선택하고 빌릴 사람을 선택하는 어떤 방법을 가지고있다. – Kaspar
"비공개 섹션은 로그인하지 않은 사용자가 페이지를 볼 수 없도록 만듭니다"는 '비공개'와는 전혀 다릅니다! – Mischa