使用rails中的模态创建和编辑对象

ran*_*dom 5 ruby-on-rails modal-dialog twitter-bootstrap

我一直在尝试使用Rails中的索引html中的模态视图来创建和编辑对象.我正在项目中使用twitter bootstrap.到目前为止,我成功地使用模态创建了对象.为了工作,我必须在索引操作中创建一个名为@post = Post.new的对象.

因为在显示编辑模式之前,编辑对象必须准备好为@post = Post.find(params [:id]),但它在编辑操作中发生.

他们可以使用哪种方式在显示之前将@post初始化为我的编辑模态视图?

这是我的代码:

index.html.erb

<div class="page-header">
    <h1>Posts</h1>
</div>


<% @posts do |post| %>
  <tr>
    <td><%= post.name %></td>
    <td><%= post.description %></td>

    <td><%= link_to 'Edit', edit_post_path(post), :class => 'btn btn-mini btn-min-standard-width',  :data => {:toggle => "modal", :target => "#editItemModal"} , :remote => true %>
        <%= link_to 'Destroy', post, confirm: 'Are you sure?', method: :delete, :class => 'btn btn-mini btn-danger btn-min-standard-width'  %></td>
  </tr>
<% end %>


<%= link_to 'New Item', new_post_path, 
        :class => 'btn btn-primary btn-standard-width' ,  :data => {:toggle => "modal", :target => "#newItemModal"} , :remote => true %>





<div id="newItemModal" class="modal hide fade" >
    <button type="button" class="close" data-dismiss="modal">×</button></h1>
    <div class="page-header">
        <h1>New item </h1>
    </div>

    <%= render :partial => 'form' %>
</div>


<div id="editItemModal" class="modal hide fade" >
    <button type="button" class="close" data-dismiss="modal">×</button></h1>
    <div class="page-header">
        <h1>Edit item </h1>
    </div>

    <%= render :partial => 'form' %>
 </div>
Run Code Online (Sandbox Code Playgroud)

_form.html.erb

<%if @post%>

    <%= form_for(@post, :html => { :class => 'form-horizontal', :remote => true } ) do |f| %>


     <div class="control-group">
        <%= f.label :name, :class => 'control-label' %>
        <div class="controls">
           <%= f.text_field :name %>
       </div>
     </div>
    <div class="control-group">
        <%= f.label :description, :class => 'control-label' %>
        <div class="controls">
            <%= f.text_area :description %>
        </div>
     </div>


    <div class="form-actions">
       <%= f.submit nil, :class => 'btn btn-primary' %>
     </div>


    <% end %>
<% end %>
Run Code Online (Sandbox Code Playgroud)

PostController中

class PostsController < ApplicationController

 def index
    @post = Post.new
    @posts = Post.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @posts }
    end
  end

 def show
    @post = Post.find(params[:id])

    respond_to do |format|
       format.html # show.html.erb
       format.json { render json: @post }
     end
  end

 def new
    @post = Post.new
 end

  def edit
    @post = Post.find(params[:id])
    respond_to do |format|
      format.html
     format.json { render json: @post }
    end
  end
Run Code Online (Sandbox Code Playgroud)

............

Jac*_*han 0

据我了解您想在编辑模式视图中使用实例变量@post。

在不知道你的代码库的情况下,我能说什么:

  1. 在渲染编辑模态视图的控制器中找到对应的action,如果没有就创建一个
  2. 使用 id 找到的模型 Post 初始化 @post,只需将其复制并粘贴到正确的操作中
  3. 在视图范围内调用

希望这能回答你的问题