【问题标题】:rails form using form_for not submitting using post method使用form_for的rails表单不使用post方法提交
【发布时间】:2016-03-03 04:46:23
【问题描述】:

我有一个嵌套在课程资源中的 cmets 资源:
路线.rb:

.. 资源:课程做 资源 :cmets, :only => [:new, :create] 结尾 ..

我想在任何课程的显示页面中显示表格以发表评论。
cmets_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 放入我的课程控制器的显示操作中。

我在视图中使用的形式是:
course_cmets_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>

当我点击提交时,获取请求会发送到我当前所在的同一页面(所有参数都在 url 中)。
我尝试将表单放在 cmets 的单独新页面中,它仍然不起作用。 我做错了什么?

【问题讨论】:

  • 试试form_for这样,&lt;%= form_for([@course, @comment],:url =&gt; course_comments_path(@course, @comment),method: "post" ) do |f| %&gt;把Url作为第二个参数
  • 任何人都有任何解决方案......我仍然无法让它工作......

标签: ruby-on-rails ruby forms


【解决方案1】:

好的,我遇到了问题,我想我应该把它放出来以供其他人将来参考。
问题是 form_for 被包含在表单标签中。

【讨论】:

    【解决方案2】:

    这可能有效,显示页面:

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

    cmets 控制器:

     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
    

    【讨论】:

    • 这不起作用...它仍然提供获取请求...而且您的语法错误,缺少关闭 ')'...
    • 更新了答案,修正了语法错误和方法::post added to form
    • put 方法::post 在 form_for() 括号内
    【解决方案3】:

    由于您手动将course 分配给create action 中的comment,因此您只能将@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 并将该课程分配给评论。

    【讨论】:

    • 抱歉,您在说什么?这是一个选项哈希,顺序无关紧要......
    • @2call-chaos,很抱歉,因为我看到的每个 url 都在第二个参数中,我总是采取同样的方式,所以我让他试试它是否有效。我改变了我的回答。
    猜你喜欢
    • 1970-01-01
    • 2017-03-18
    • 2019-05-31
    • 1970-01-01
    • 2010-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多