【问题标题】:Rails: How to direct to results page on form 'submit'Rails:如何在“提交”表单上定向到结果页面
【发布时间】:2015-04-28 13:11:48
【问题描述】:

我想在用户单击 8 题多项选择测验的提交按钮时将他们引导至“结果”页面,同时还将他们的答案保存到数据库中。

目前我正在使用“form_for”并传入 current_user。当单击提交时,它因此被定向到用户/显示操作。我想转到详细说明用户结果的页面。我该怎么做呢?

这是我的(非常粗略的测试)表格,到目前为止,有一个选择题(由引导程序设计):

<%= form_for([current_user]) do |f| %>
  <p>
    <%= text_field(:user, :name) %>
  </p>
  <div class="btn-group-vertical clearfix form-group" data-toggle="buttons">
          <label class="btn btn-primary text-left">
            <input name="options" id="option1" type="radio" onclick="multiChoiceClick(1, 1)">A. Always. I'm the leader and should have the final say
          </label>
          <label class="btn btn-primary text-left">
            <input name="options" id="option2" type="radio">B. Sometimes, but I think the group should make decisions if possible
          </label>
          <label class="btn btn-primary text-left">
            <input name="options" id="option3" type="radio">C. I generally don't get involved. The group can make their own decisions without my help
          </label>
  </div>
  <p>
    <%= f.submit("Get my results!") %>
  </p>
<% end %>

我目前包含带有用户名的 text_field 只是作为测试。

我想知道如何将第一个多项选择题“连接”给用户,但在单击“获取我的结果”时显示结果页面。

我很可能在这段代码中有几处错误。如果我这样做了,并且您有答案,您能否指出我做错了什么并解释做那件事的正确方法?抱歉说得这么具体,但我之前有几个 cmet 只指出问题所在,别无选择。

谢谢。

编辑:

有人要了控制器,所以这里是:

class HomeController < ApplicationController
    def index
    end

    def whattypeofleader
        @user = current_user
        #@quiz_answer = current_user.quiz_answers(0).build
    end

    def post_params
        params.require(:quiz_answer).permit(:body, :user_id)
    end
end

我已将@quiz_answer 注释掉,因为我不确定如何将其与表单相关联。在用户控制器中,用户 has_many :quiz_answers 和 QuizAnswer belongs_to :user

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4 formbuilder


    【解决方案1】:

    重定向应该来自您的控制器。 管理数据后,一个简单的:

    redirect_to results_path

    您的结果路径应该在您的 config/routes.rb 中。

    不要犹豫,多分享一点,比如你的控制器,看看我们是否可以更深入:)

    [EDIT] 跟随 cmets

    免责声明:不确定 Rails 对测验的复数管理...

    您的表单应该由new 方法呈现,如下所示:

    class QuizzesController < ApplicationController
    
      ...
    
      def new
        @user = current_user     #you now have access to the @user variable in your view
      end
    
      ...
      def create
        # do your thing with your params
        # usually, you want to redirect to the results page if the action has been saved
        # so let's say that you built an @object beforehand with the params you received
        # extactly like @DarkMousse answer :
    
        if @object.save
          redirect_to results_path
          flash[:notice] = "Thanks for submitting these questions"
        else
          render 'new'
        end
    
        # this way, if all is good, go to results_path, else, return to the 'new'
    
      end
      ...
    
    end
    

    为了访问这个动作,你必须有一个到达它的路线,让我们去config/routes.rb

    YouApplicationName::Application.routes.draw do
      ...
      #it 'opens' the routes to access all method from your quizzes controller
      resources :quizzes
      resources :results
      ...
    end
    

    现在您可以访问测验/新。它将在views/quizzes/new 中呈现视图,您可以在其中拥有如下形式:

    form_for @user do |f|
      # ... your form ...
    end
    

    请注意,@user 可以通过 QuizzesController 中的 new 方法访问。

    现在我们在路由中添加了results,在终端中使用rake routes,您应该会发现results_path 导致您的(尚未创建:))ResultsControllerindex 方法。

    它可能看起来像这样:

    class ResultsController < ApplicationController
      def index
        # in order to access all the results in our view
        @results = Results.all
      end
    end
    

    这样,results_path 将导致此操作,默认情况下(感谢 rails)调用 views/results/index.html.erb 进行渲染。

    【讨论】:

    • 感谢您的回复!我在上面添加了 HomeController 以及一些关于 User 和 QuizAnswer 关系的细节。我认为我的大问题是我看不到这些 *_path 助手来自哪里。我将在哪里或如何生成 results_path?
    • 您的home_controller 不应管理您的用户。如果是为了管理您的测验,请继续创建一个 QuizzController。在new 方法中,您可以获取您的用户并将其传递给您的视图(@user = current_user)。您将在您的views/quizzes/new.html.erb 中拥有form_for @user。 Rails 表单助手会负责将提交数据发送到您的QuizzController 中的create 方法,您将在那里管理您的参数。我清楚了吗?你可以说不!
    • 啊!我认为一次有很多值得欣赏的地方。当您说“在新方法中”时,是 QuizAnswer 控制器的“新”方法吗?并且 - 当前表单是部分的,呈现为 home/whattypeofleaderareyou.html.erb 视图的一部分。但你是说表格应该在 quizzes/new.html.erb 中,对吧?我会试一试,看看会发生什么......
    • 我已经编辑了new 方法和路由的答案。
    • OK 它正在使用新的 QuizzesController。当我点击提交时,它仍然会更新并显示用户的个人资料。我如何将其发送到结果页面? (再次感谢您的回答——这感觉就像我在一周内取得的第一个真正进展!)。我还更改了“whattypeofleader”路径以在 routes.rb 中获取“whattypeofleader”=>“quizzes#new”;包括资源:测验与否没有任何区别。有问题吗?
    【解决方案2】:

    提交表单涉及POST 请求。因此,您正在向服务器发送数据。在 Rails 中,这可以通过Create Action 来完成。在你的Home Controller中做这样的事情

    def create
      @question = Question.new
      if @question.save
        redirect_to results_path
        flash[:notice] = "Thanks for submitting these questions"
      else
        render 'new'
      end
    end
    

    【讨论】:

      【解决方案3】:

      对象成功保存到数据库后,只需在相应的控制器中添加“redirect_to results_path”即可。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-02-26
        • 2019-09-04
        • 2020-09-13
        • 2013-07-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多