2016-03-03 23 views
-1

我有嵌套在课程资源评价资源的form_for未提交:
的routes.rb:导轨形式使用使用POST方法

我想表明的形式给予的任何显示页面的评论课程。
comments_controller.rb:

class CommentsController < ApplicationController 
    before_action :require_user 
    before_action :set_course, only: [:new] 

    def new 
    @comment = Comment.new 
    end 

    def create 
    @comment = Comment.new(comment_params) 
    @comment.course = Course.find(params[:course_id]) 
    @comment.user = current_user 
    if @comment.save 
     flash[:success] = "Comment Posted" 
     redirect_to course_path(@comment.course) 
    else 
     flash[:error] = "Comment can't posted" 
     redirect_to course_path(@comment.course) 
    end 
    end 

    private 

    def set_course 
     @course = Course.find(params[:course_id]) 
    end 

    def comment_params 
     params.require(:comment).permit(:body ,:rating, :course_id, :user_id) 
    end 

end 

我已经把@comment = Comment.new在我的课程中控制器的表演动作。

我使用在视图中的形式为:
<%=的form_for([@当然,@comment],方法: “后”,:URL => course_comments_path(@course,@comment))做| F | %>

  <div class="rrating" style="width:140px;"> 
      <%= f.radio_button(:rating, 1) %> 
      <%= f.label(:rating_1, "1") %> 
      <%= f.radio_button(:rating, 2) %> 
      <%= f.label(:rating_2, "2") %> 
      <%= f.radio_button(:rating, 3) %> 
      <%= f.label(:rating_3, "3") %> 
      <%= f.radio_button(:rating, 4) %> 
      <%= f.label(:rating_4, "4") %> 
      <%= f.radio_button(:rating, 5) %> 
      <%= f.label(:rating_5, "5") %> 
      </div> 

      <%= f.text_field :body, :class => "form-control", id: "exampleInputEmail1", placeholder: "Add a comment" %> 

        </fieldset> 
     <%= f.submit("Post", class: 'btn btn-success') %> 
     <% end %> 
     </form> 

当我点击提交,GET请求被发送到同一页我在目前(与url中所有PARAMS)。
我试图把表单放在一个单独的新页面的意见,它仍然没有工作。 我在做什么错?

+0

尝试的form_for这样,'<(%)=的form_for([@当然,@comment],:URL => course_comments_path(@course,@comment),方法: “后”)做| F | %>'将Url作为第二个参数 – Sravan

+0

任何人都有任何解决方案...我仍然无法得到这个工作... –

回答

0

这可能会实现,显示页面:

<%= form_for([@course, @course.comments.build] , method: :post) do |f| %> 
      <fieldset class="form-group" style="margin-top:0px; !important"> 

         <div class="rrating" style="width:140px;"> 
         <%= f.radio_button(:rating, 1) %> 
         <%= f.label(:rating_1, "1") %> 
         <%= f.radio_button(:rating, 2) %> 
         <%= f.label(:rating_2, "2") %> 
         <%= f.radio_button(:rating, 3) %> 
         <%= f.label(:rating_3, "3") %> 
         <%= f.radio_button(:rating, 4) %> 
         <%= f.label(:rating_4, "4") %> 
         <%= f.radio_button(:rating, 5) %> 
         <%= f.label(:rating_5, "5") %> 
         </div> 

         <%= f.text_field :body, :class => "form-control", id: "exampleInputEmail1", placeholder: "Add a comment" %> 

           </fieldset> 
        <%= f.submit("Post", class: 'btn btn-success') %> 
    <% end %> 

评论控制器:

def create 
      @course = Course.find(params[:course_id]) 
      @comment = @course.comments.create(comment_params) 
      @comment.user = current_user 

    if @comment.save 
     flash[:success] = "Comment Posted" 
     redirect_to course_path(@comment.course) 
    else 
     flash[:error] = "Comment can't posted" 
     redirect_to course_path(@comment.course) 
    end 
    end 
+0

这个dosen't工作...它仍然给出了一个请求...加上你有一个语法errr,缺少结束的“)” ...... –

+0

更新了答案,固定语法错误和方法:添加后形成 –

+0

它仍然有语法错误......仍然这么想的工作 –

-1

既然你分配一个course在手动create actioncomment,您可以将@comment对象只评论表单,你可以去创建操作。

通过一个隐藏字段从形式发送的course_id创建行动

<%= form_for @comment, url: course_comments_path do |f| %> 
 
    <fieldset class="form-group" style="margin-top:0px; !important"> 
 
    <div class="rrating" style="width:140px;"> 
 
     <%= f.radio_button(:rating, 1) %> 
 
     <%= f.label(:rating_1, "1") %> 
 
     <%= f.radio_button(:rating, 2) %> 
 
     <%= f.label(:rating_2, "2") %> 
 
     <%= f.radio_button(:rating, 3) %> 
 
     <%= f.label(:rating_3, "3") %> 
 
     <%= f.radio_button(:rating, 4) %> 
 
     <%= f.label(:rating_4, "4") %> 
 
     <%= f.radio_button(:rating, 5) %> 
 
     <%= f.label(:rating_5, "5") %> 
 
     <%= f.hidden_field :course_id, value: @course.id % 
 
    </div> 
 
    <%= f.text_field :body, :class => "form-control", id: "exampleInputEmail1", placeholder: "Add a comment" %> 
 
    </fieldset> 
 
    <%= f.submit("Post", class: 'btn btn-success') %> 
 
<% end %>

现在你会得到course_id通过PARAMS和课程分配到评论。

+0

仍然没有工作... :-( –

+0

对不起,但你在说什么?这是一个选项散列,顺序无关紧要... –

+0

@ 2called-chaos,对不起,但是,因为我看到网址都在第二个参数中,而且我总是采用相同的方式,所以我让他试试它是否有效。我改变了我的答案。 – Sravan

0

好吧,我有这个问题,并认为我应该把它放在其他未来的参考。
问题在于form_for被包含在表单标记中。