Rails – 如何将一些信息从一个表单发送到其他控制器

首先,我想描述一下我将要做的事情。 因此,我的应用程序是一组任务(例如,练习依赖于编写计算机程序),用户可以通过表单发送解决方案,服务器将编译它并返回进程结果。 我的问题是我有模型Solution ,如下所示:

 Solution(id: integer, exercise_id: integer, code: string, user_id: integer, created_at: datetime, updated_at: datetime, language_id: integer) 

exercise_id, user_id, language_id是外键和postgresql返回错误,它不能是空值…我用来发送此信息的表单如下所示:

 
Send solution { role: "form" } do |f| %>


"btn btn-default" %>

坦率地说,我不知道如何包含更多信息,如user.id或exercise.id。 参数看起来像:

 @_params {"utf8"=>"✓", "authenticity_token"=>"abwhTn3KrVneli0pCVccoWHyzlI7cRTFK5Wo7DdYRK+lL4GwZ8jUPqdzn8ZznCHsWTkfDAFS2RP6gTkWuk33iw==", "solution"=>{"code"=>"ds", "language_id"=>"C++"}, "commit"=>"Wyślij", "controller"=>"solutions", "action"=>"create", "exercise_id"=>"1"} 

SolutionController

 class SolutionsController < ApplicationController def index @solutions = Solution.last(20) end def new @solution = Solution.new end def create @solution = Solution.new(solution_params) if @solution.save redirect_to @solution else render :new end end def edit @solution = Solution.find(params[:id]) if @solution.update(solution_params) redirect_to @solution else render :edit end end def destroy end private def solution_params params.require(:solution).permit(:language_id, :code) end end 

编辑:

 
Send solution { role: "form" } do |f| %>


"btn btn-default" %>

您可以1)在表单中添加hidden_field以设置缺少的属性:

 
<%= f.hidden_field :exercise_id, value: @exercise.id %> <%= f.hidden_field :user_id, value: @user.id %> <%= f.submit "Send", :class => "btn btn-default" %>

确保将这些属性添加到solution_params允许的参数中:

 def solution_params params.require(:solution).permit(:language_id, :code, :exercise_id, :user_id) end 

更好的想法是在控制器中手动设置exerciseuser ,并按原样保留solution_params 。 这样你就不会允许练习和用户通过质量分配来设置,这可能是欺骗性的。

所以在你的控制器中做2):

 def create @solution = Solution.new(solution_params) @solution.exercise_id = params[:exercise_id] @solution.user_id = params[:user_id] if @solution.save redirect_to @solution else render :new end end 

我假设:exercise_id:user_id在URL中。 如果不是根据需要将它们添加为hidden_​​fields(但这次不在表单对象上):

 <%= hidden_field_tag :user_id, @user.id %> 

还记得在提交表单之前从下拉列表中选择一个language_id