2015-04-02 27 views
1

我有2個模型發佈和評論,其中帖子有許多評論和意見belongs_to發佈。 每一件事情都可以正常工作,併爲該帖子創建帖子和評論。無法使用導軌應用程序彈出模態對話框

現在我有一個要求,當點擊posts/show page中的「創建新評論」時,我想顯示評論/ _form in modal。

comments_controller.rb:

class CommentsController < ApplicationController 
    before_action :set_comment, only: [:show, :edit, :update, :destroy] 

    # GET /comments 
    # GET /comments.json 
    def index 
    @comments = Comment.all 
    respond_with(@comments) 
    end 

    # GET /comments/1 
    # GET /comments/1.json 
    def show 
    respond_with(@comments) 
    end 

    # GET /comments/new 
    def new 
    @post = Post.find(params[:post_id]) 
    @comment = @post.comments.build 
    end 

    # GET /comments/1/edit 
    def edit 
    @post = Post.find(params[:post_id]) 

    end 

    # POST /comments 
    # POST /comments.json 
    def create 
    @post = Post.find(params[:post_id]) 
    @comment = @post.comments.create(comment_params) 
    respond_to do |format| 
     if @comment.save 
     format.html { redirect_to(@comment, :notice => 'Article was successfully created.') } 
     format.xml { render :xml => @comment, :status => :created, :location => @comment } 
     format.js 
     else 
     format.html { render :action => "new" } 
     format.xml { render :xml => @comment.errors, :status => :unprocessable_entity } 
     format.js 
     end 
    end 
    end 

    # PATCH/PUT /comments/1 
    # PATCH/PUT /comments/1.json 
    def update 
    @post = Post.find(params[:post_id]) 
    @comment.update(comment_params) 
    redirect_to post_path(@post) 
    end 

    # DELETE /comments/1 
    # DELETE /comments/1.json 
    def destroy 
    @comment.destroy 
    respond_to do |format| 
     format.html { redirect_to comments_url, notice: 'Comment was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_comment 
     @comment = Comment.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def comment_params 
     params.require(:comment).permit(:commenter, :description) 
    end 
end 

帖子/ show.html.erb:

<%= link_to 'New Coment', new_post_comment_path(@post), :id => 'create_comment' %> 

的意見/ new.html.erb:

<div id="content"> 
    <h1>New Comment</h1> 
    <%= render 'form' %> 
</div> 

的意見/ _form.html .erb:

<%= form_for([@post, @comment], :remote => true) do |f| %> 
    <div class="field"> 
    <%= f.label :commenter %><br> 
    <%= f.text_field :commenter %> 
    </div> 
    <div class="field"> 
    <%= f.label :description %><br> 
    <%= f.text_field :description %> 
    </div> 

    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 

的意見/ create.js.erb:

<%- if @comment.errors.any? %> 
console.log('Error'); 
$('#dialog-form').html('<%= escape_javascript(render('form')) %>'); 
<%- else %> 
console.log('Created'); 
$('#dialog-form').dialog('close'); 
$('#dialog-form').remove(); 
$('table').append('<%= escape_javascript(render(@comment)) %>'); 
<%- end %> 

我不知道我在哪裏出了錯。當我點擊「創建評論」時,它將重定向到新頁面而不是彈出式模型,甚至無法創建評論。

請幫忙。

+0

請檢查Bootstrap 3它會引導您如何在應用程序中使用模態 – 2015-04-02 20:22:39

+0

@AmitSharma:您能否提供任何有用的鏈接 – venkat 2015-04-03 04:43:42

回答

2
  1. 創建一個顯示評論表單的部分。
  2. 將此部分渲染爲隱藏的後顯示頁面。
  3. 顯示隱藏的部分。

例子:

<%= link_to 'Show Modal', "#", data: {toggle: "modal", target: "#modal"} %> 

隱藏部分

<div class="modal hide fade" id="modal" title="My modal"> 
    /* your comment form */ 
    /* render comment form */ 
</div> 

只要確保目標是你的隱藏部分ID。

相關問題