简体   繁体   English

Rails:如何在表单'submit'上指向结果页面

[英]Rails: How to direct to results page on form 'submit'

I would like to direct users to a 'results' page when they click the submit button of an 8 question multi-choice quiz, while also saving their answers to the database. 我想在用户单击8个问题多项选择测验的提交按钮时将用户引导至“结果”页面,同时还将他们的答案保存到数据库中。

At the moment I'm using 'form_for' and passing in current_user. 目前我正在使用'form_for'并传入current_user。 When submit is clicked it is therefore directed to the User/show action. 单击提交时,它将指向User / show操作。 I would like to go to a page that details a user's results. 我想转到详细说明用户结果的页面。 How would I go about doing that? 我该怎么做呢?

Here's my (very rough test) form with, so far, one multi-choice question (styled by bootstrap): 这是我的(非常粗略的测试)形式,到目前为止,一个多项选择问题(由引导程序设计):

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

I'm currently including the text_field with the user's name just as a test. 我目前正在将text_field包含在用户名中作为测试。

I'd like to know how to 'wire up' the first multi-choice question to a User, but display a results page when 'get my results' is clicked. 我想知道如何将第一个多项选择问题“连线”给用户,但在点击“获取我的结果”时显示结果页面。

I may well have several things wrong in this code. 我可能在这段代码中有几个错误。 If I do, and you have an answer, could you please point out what I've got wrong AND explain the correct way to do that thing? 如果我这样做,并且你有答案,请你指出我的错误并解释做正确的方法吗? Sorry to be so specific, but I've had a few comments previously that ONLY point out what's wrong, with no alternative. 很抱歉这么具体,但我之前有过一些评论只能指出什么是错的,没有别的选择。

Thanks. 谢谢。

Edit: 编辑:

Someone asked for the controller, so here it is: 有人要求控制器,所以这里是:

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

I have the @quiz_answer commented out because I wasn't sure how to link it up with the form. 我已将@quiz_answer注释掉,因为我不确定如何将其与表单相关联。 In the User controller, User has_many :quiz_answers and QuizAnswer belongs_to :user 在用户控制器中,用户has_many:quiz_answers和QuizAnswer belongs_to:user

The redirection should come from your controller. 重定向应该来自您的控制器。 After you managed your datas, a simple : 管理数据后,一个简单的:

redirect_to results_path

Your results path should be in your config/routes.rb . 您的结果路径应该在config/routes.rb

Don't hesitate to share a little bit more, like your controller, to see if we can go deeper :) 不要犹豫与控制器分享更多一点,看看我们是否可以更深入:)

[EDIT] following the comments [编辑]在评论之后

disclaimer : not sure about the plural management of quizzes by Rails... 免责声明:不确定Rails的测验复数管理......

Your form should be rendered by the new method, like so : 您的表单应该由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

In order to access this action, you have to have a route to get to it, let's go to config/routes.rb 要访问此操作,您必须有一个到达它的路由,让我们转到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

Now you can access quizzes/new. 现在您可以访问测验/新。 It will render the view in views/quizzes/new , in which you could have a form like so : 它将在views/quizzes/new呈现视图,您可以在其中使用如下形式:

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

Note that @user is accessible thanks to our new method in our QuizzesController. 请注意,由于我们的QuizzesController中的new方法,@ user是可访问的。

Now that we added a results to our routes, with a rake routes in the terminal you should find a results_path leading to your (yet to create :) ) ResultsController index method. 现在我们在路由中添加了一个results ,在终端中有一个rake routes ,你应该找到一个results_path导致你的(尚未创建:)) ResultsController index方法。

It could be looking like this : 它可能看起来像这样:

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

This way, the results_path is leading to this action, which by default (thanks to rails) is calling views/results/index.html.erb to render. 这样, results_path将导致此操作,默认情况下(感谢rails)调用views/results/index.html.erb进行渲染。

Submitting a form involves a POST request. 提交表单涉及POST请求。 Therefore, you are sending data to the server. 因此,您要将数据发送到服务器。 In Rails, this can be accomplished through the Create Action . 在Rails中,这可以通过Create Action完成。 Do something like this in your Home Controller 在您的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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM