Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails:如何在表单';提交';_Ruby On Rails_Ruby_Ruby On Rails 4_Formbuilder - Fatal编程技术网

Ruby on rails Rails:如何在表单';提交';

Ruby on rails Rails:如何在表单';提交';,ruby-on-rails,ruby,ruby-on-rails-4,formbuilder,Ruby On Rails,Ruby,Ruby On Rails 4,Formbuilder,当用户单击8个问题的多项选择测验的提交按钮时,我想引导用户进入“结果”页面,同时将他们的答案保存到数据库中 目前,我正在使用“form\u for”并传入当前用户。因此,单击“提交”时,它会指向用户/显示操作。我想转到一个详细说明用户结果的页面。我该怎么做呢 这是我的(非常粗略的测试)表格,到目前为止,有一个多选题(由bootstrap设计): 总是这样。我是领导者,应该有最终决定权 B.有时,但我认为如果可能,小组应该做出决定 我一般不参与。团队可以在没有我帮助的情况下做出自己的决定

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

目前,我正在使用“form\u for”并传入当前用户。因此,单击“提交”时,它会指向用户/显示操作。我想转到一个详细说明用户结果的页面。我该怎么做呢

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



总是这样。我是领导者,应该有最终决定权 B.有时,但我认为如果可能,小组应该做出决定 我一般不参与。团队可以在没有我帮助的情况下做出自己的决定

我目前正在使用用户名包含text_字段作为测试

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

我可能在这段代码中有几处错误。如果我这样做了,你有答案,你能指出我错了什么并解释正确的方法吗?抱歉说得这么具体,但我之前有一些评论只指出了问题所在,别无选择

谢谢

编辑:

有人要控制器,这里是:

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

我把@quick\u答案注释掉了,因为我不知道如何将它与表单联系起来。在用户控制器中,用户有多个:测验答案,QuizAnswer属于:User

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

将\u重定向到结果\u路径

结果路径应位于
配置/routes.rb

不要犹豫,像您的控制器一样,再多分享一点,看看我们是否可以更深入:)

[编辑]在评论之后

免责声明:不确定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
form_for @user do |f|
  # ... your form ...
end
现在您可以访问测验/新建。它将在
视图/quizzes/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
form_for @user do |f|
  # ... your form ...
end
请注意,@user是可以访问的,这多亏了我们的QuizzesController中的
新方法

现在,我们在路由中添加了一个
results
,使用终端中的
rake routes
,您应该会找到一个
results\u路径
,指向您的(尚未创建:)
ResultsController
索引
方法

可能是这样的:

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

通过这种方式,
results\u路径
将导致此操作,默认情况下(感谢rails)调用
views/results/index.html.erb
进行渲染。

提交表单涉及
POST
请求。因此,您正在向服务器发送数据。在Rails中,这可以通过
创建操作来实现。在您的
家庭控制器中执行类似操作

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

一旦对象成功保存到数据库中,只需在相应的控制器中添加“重定向到结果路径”

谢谢你的回复!我已经在上面添加了HomeController以及一些关于用户和QuizAnswer关系的详细信息。我想我最大的问题是我不知道这些路径助手来自哪里。我将在何处或如何生成结果路径?您的
home\u控制器不应管理您的用户。如果是为了管理你的测验,那就创建一个测验控制器吧。在
new
方法中,您可以获取用户并将其传递给视图(
@user=current\u user
)。您将在
视图/quizzes/new.html.erb
中为@user
创建一个
表单。Rails表单助手将负责将提交数据发送到
QuizzController
中的
create
方法,您将在那里管理参数。我明白了吗?你可以说不!啊!我觉得有很多东西可以马上欣赏。当你说“在新方法中”时,这是QuizAnswer控制器的“新”方法吗?和-当前表单是部分表单,作为home/whattypeofleaderareyu.html.erb视图的一部分呈现。但是你是说表单应该在quizzes/new.html.erb中,对吗?我将试一试,看看会发生什么……我已经编辑了
新的
方法和路线的答案。好的,它正在使用新的QuizzesController。当我点击提交时,它仍然会更新并显示用户的个人资料。如何将其发送到结果页?(再次感谢你的回答——这感觉像是我一周来取得的第一个真正的进步!)。此外,我还更改了“whattypeofleader”路径,以在routes.rb中获得“whattypeofleader”=>“quizzes#new”;包括资源:测验与否没有任何区别。这是个问题吗?