2012-07-23 33 views
5

我一直在嘗試使用Rails中索引html中的模態視圖創建和編輯對象。我在項目中使用twitter bootstrap。我很成功地使用模態創建對象。 對於工作,我必須在我的索引操作中創建一個名爲@post = Post.new的對象。在rails中使用模態創建和編輯對象

由於在顯示編輯模式之前,編輯對象必須準備就緒@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> 

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

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 

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

+0

您可以通過發佈代碼獲得更多有用的答案 – 2012-07-23 05:40:04

+0

已更新的問題與代碼片段 – random 2012-07-23 05:53:51

+0

@random你知道嗎? – 2013-10-21 02:11:25

回答

0

只要我明白你想在編輯模式視圖中使用實例變量@post。

不知道你的代碼庫,我能說什麼:

  1. 查找呈現在編輯模式視圖控制器相應的動作,創建一個如果沒有
  2. 初始化@post與模型發表ID發現,只是簡單地認爲複製並粘貼到適當的行動
  3. 視圖範圍

希望這回答你的問題中調用它

+0

由於模態顯示在索引html中,我認爲應該在索引操作中定義@post。但是當我點擊新的或編輯按鈕時,相應的動作被調用。 – random 2012-07-23 05:56:46

+0

在您的索引視圖中渲染'<%= render:partial =>'form'%>',您希望在其中創建對象。 – 2012-07-23 06:07:27

+0

它沒有幫助。任何其他建議? – random 2012-07-23 06:34:02

相關問題