我在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发布于 2016-05-15 17:23:50
你可以这样做:
在循环结束后的索引页面中,添加一个空的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 %>请学习rails的ajax rendering。我在我的代码里放了一些圆孔。如果你学习的话,你会发现的。)快乐编码。
https://stackoverflow.com/questions/37233868
复制相似问题