我列出了我所有的#34;书籍#34;在books.index视图上。我希望能够在同一视图中编辑它们。当我点击其中一本书时,有没有办法渲染编辑表格?
截至目前,我正在为每本书打印出部分内容,并且我为每本书添加了单独的表单。但是,每当有人点击一本书时,我就必须隐藏所有这些并附加它们。
我认为每当有人点击它时,我应该有某种方式呈现相同的表单,并给它一个书籍ID,然后它将有希望填充正确的信息。
希望这是有道理的。 谢谢!
控制器书籍:
class BooksController < ApplicationController
def index
@Books = Book.all
@book = Book.new
end
def show
@book = Book.find(params[:id])
@chapter = chapter.new
end
def new
@book = Book.new
if !current_user.admin?
redirect_to @book
end
end
def edit
@book = Book.find(params[:id])
if !current_user.admin?
redirect_to @book
end
end
def create
@books = Book.all
@book = Book.new(book_params)
if @book.save
respond_to do |format|
format.js
format.html { render action: "index", notice: 'Book was successfully created.' }
format.json { render json: @book, status: :created, location: @book }
end
else
respond_to do |format|
format.js
format.json { render json: @book.errors, status: :unprocessable_entity }
format.html { render action: "index", notice: 'feil' }
end
end
end
def update
@book = Book.find(params[:id])
if @book.update(book_params)
redirect_to @book
else
render 'edit'
end
end
def destroy
@book = Book.find(params[:id])
@book.destroy
redirect_to books_path
end
private
def book_params
params.require(:book).permit(:title, :pages)
end
end
查看,books.index
<div id="wrapper">
<section id="page_wrap">
<h1>books</h1>
<table id="all_exps">
<tr>
<th>Title</th>
<th>abbr</th>
<th>pages</th>
<th>chapter</th>
<th class="edits"></th>
<%unless !session[:user_id]%>
<% if current_user.admin? %>
<th class="edits"></th>
<th class="edits"></th>
<% end%>
<%end%>
</tr>
<% @books.each do |book| %>
<%= render 'book', book: book %>
<% end %>
</table>
<%unless !session[:user_id]%>
<% if current_user.admin? %>
<div class="makenew">
<a href="" class="add_new"> Add Book</a>
</div>
<div id="somedialog" class="dialog">
<div class="dialog__overlay"></div>
<div class="dialog__content">
<div class="close"><%= image_tag('icons/delete2.png') %></div>
<h2>Add Book</h2>
<%= render 'form' %>
</div>
</div>
<% end%>
<% end%>
</section>
</div>
查看部分_book
<tr>
<td><%= book.title %></td>
<td><%= book.abbr %></td>
<td>
</td>
<td><%= book.chapters.length %></td>
<td class="edits show"><%= link_to image_tag('icons/eye.png'), book_path(book) %></td>
<%unless !session[:user_id]%>
<% if current_user.admin? %>
<td class="edits"><%= link_to image_tag('icons/edit3.png'), edit_book_path(book) %></td>
<td class="edits"><%= link_to image_tag('icons/delete2.png'), book_path(book),
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
<% end%>
<%end%>
</tr>
<%= form_for book do |f| %>
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.label :pages %>
<%= f.text_field :pages %>
<%= f.submit %>
<% end %>
预订模型
class Book < ActiveRecord::Base
has_many :chapters, dependent: :destroy
validates :title, presence: true,
length: { minimum: 3 }
end
答案 0 :(得分:0)
你可以这样做:
在循环结束后的图书索引页面中,添加一个空的div
。
<% @books.each do |book| %>
<%= link_to "Edit", edit_book_path(book) %>
<% end %>
<div id="book-edit-partial"></div>
接着,
在books_controller.rb
上创建一个新方法,并在route.rb
中定义。假设方法的名称是inline_edit
。
<强>的routes.rb 强>
resources :books do
member do
get :inline_edit
end
end
现在它将生成类似books/:id/inline_edit
的路线。
<强> books_controller.rb 强>
def inline_edit
@book = Book.find(params[:id])
respond_to do |format|
format.js { render :file => "/path/to/inline_edit.js.erb" } # create a file named inline_edit.js.erb
end
end
现在在新创建的js.erb
文件中:
$('#inline_edit.js.erb').html('render the form of edit with local variable @book') #I guess you can do that
然后将index
页面编辑书链接更改为:
<%= link_to "Edit", inline_edit-method-url, remote: true %>
请了解ajax rendering
栏目。我在代码中添加了一些漏洞。如果你学习,那么你会发现。 :)
快乐的编码。