rails使用form_for形式,不使用post方法提交

时间:2016-03-03 04:46:24

标签: ruby-on-rails ruby forms

我有一个嵌套在课程资源中的评论资源:
routes.rb:

    ..
    resources :courses do
      resources :comments, :only => [:new, :create]
    end
    ..

我想在任何课程的展示页面上显示表单以发表评论 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放在我的课程控制器的show动作中。

我在视野中使用的表格是:
                     &lt;%= form_for([@ course,@ comment],method:&#34; post&#34;,:url =&gt; course_comments_path(@course,@ comment))do | f | %GT;               

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

当我点击提交时,获取请求会被发送到我当前所在的同一页面(所有参数都在网址中)。
我尝试将表单放在一个单独的新页面中进行评论,它仍然没有工作。 我做错了什么?

3 个答案:

答案 0 :(得分:1)

好的,我遇到了问题,并认为我应该把它放在其他人的未来参考中 问题是form_for被包含在表单标签中。

答案 1 :(得分: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

答案 2 :(得分:-1)

由于您手动为course中的comment分配了create action,因此您只能将@comment对象发送到评论表单,然后您可以创建操作

通过表单中的隐藏字段发送course_id以创建操作

&#13;
&#13;
<%= 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 %>
&#13;
&#13;
&#13;

现在,您将通过参数获得course_id并将该课程分配给评论。