Ruby on rails 参数丢失或值为空-是否向现有模型添加字段?

Ruby on rails 参数丢失或值为空-是否向现有模型添加字段?,ruby-on-rails,ruby,forms,parameters,Ruby On Rails,Ruby,Forms,Parameters,我有一个具有许多属性的目标模型 一旦一个目标完成,它会在按下按钮时标记为完成,然后重定向到一个新表单,该表单可以向目标模型添加其他属性 对我来说,将它们存储在目标模型上是有意义的,因为它们只链接到目标,但是,我无法让它正常工作,因为我总是得到param缺失或值为空:goal 目标参数用作标准CRUD参数 目标完成后,将使用目标审查参数 这是否可以在“更新”操作之外执行 控制器: class GoalsController < ApplicationController before_a

我有一个具有许多属性的目标模型

一旦一个目标完成,它会在按下按钮时标记为完成,然后重定向到一个新表单,该表单可以向目标模型添加其他属性

对我来说,将它们存储在目标模型上是有意义的,因为它们只链接到目标,但是,我无法让它正常工作,因为我总是得到param缺失或值为空:goal

目标参数用作标准CRUD参数

目标完成后,将使用目标审查参数

这是否可以在“更新”操作之外执行

控制器:

class GoalsController < ApplicationController
  before_action :authenticate_user!, except: [:index, :show, :completedgoals]
  before_action :set_goal, only: [:show, :edit, :update, :destroy, :mark_completed, :goal_completed ]

 def goal_completed
    @authorize
    if @goal.update(goal_review_params)
        format.html { redirect_to @goal, notice: 'Goal review was successfully updated.' }
        format.json { render :show, status: :ok, location: @goal }
      else
        format.html { redirect_to goal_completed_goal_path(@goal) }
        format.json { render json: @goal.errors, status: :unprocessable_entity }
      end
  end



private
    def set_goal
      @goal = Goal.find(params[:id])
    end

#category_ids:[] needs to be added back to goal_params when they're added back in
    def goal_params
      params.require(:goal).permit(:goalname, :goaldesc, :goalhow, :goalwhy, :goalreward, :goalduedate, :goalstatus )
    end

    def goal_review_params
      params.require(:goal).permit(:goaldifficult, :goallearned, :goalnext)
    end
def goal_completed
  if request.patch?
    respond_to do |format|
      if @goal.update(goal_review_params)
        format.html { redirect_to @goal, notice: 'Goal review was successfully updated.' }
        format.json { render :show, status: :ok, location: @goal }
      else
        format.html { redirect_to goal_completed_goal_path(@goal) }
        format.json { render json: @goal.errors, status: :unprocessable_entity }
      end
    end
  end
end

您仍在使用更新方法

要使用不同的方法,请按照


@goal的表单:url=>url(:controller=>'goal',:action=>'goal\u completed')

您的
参数丢失或值为空:goal
错误,因为您的目标参数确实丢失

当您将创建/更新的目标重定向到“goal_completed”视图表单时,您正在执行一个“get”请求。正如您在路由中定义的,请求将读取控制器中的命名方法,并在此行停止:

if @goal.update(goal_review_params)
在请求的这一点上,所有的
goal\u review\u参数都丢失了,因为您有一个如下的GET:

开始获取“/goals/4/goal_completed”用于…
GoalsController的处理#goal_以HTML形式完成
参数:{“id”=>“4”}

为了实现您想要的,您可以创建两个自定义路由,一个用于“获取”,一个用于“修补”,或者如果您真的只需要一个操作,则可以像这样处理多个请求:

resources :goals do
  member do
    get 'goal_completed'  #<== to get the view form
    patch 'goal_completed' #<== to handle the update fields
  end
end
并在视图表单中指定提交的自定义路径,以防止基本的“更新”操作:


#形式

在执行您提到的
创建
更新
操作后,是否可以转储控制台日志(使用
参数
)?这听起来像是strong参数和表单发布的问题。请提供GoalsController#goals(已完成的操作#goals)的完整Rails日志好吗?可能需要添加
method::get
,给出发布的路线或推荐路线动词ChangeThank@Mark。我已将其更改为以下内容:
url\u for(:controller=>'goal',:action=>'goal\u completed')do | form |%>
,但仍会收到相同的错误。
resources :goals do
  member do
    get 'goal_completed'  #<== to get the view form
    patch 'goal_completed' #<== to handle the update fields
  end
end
def goal_completed
  if request.patch?
    respond_to do |format|
      if @goal.update(goal_review_params)
        format.html { redirect_to @goal, notice: 'Goal review was successfully updated.' }
        format.json { render :show, status: :ok, location: @goal }
      else
        format.html { redirect_to goal_completed_goal_path(@goal) }
        format.json { render json: @goal.errors, status: :unprocessable_entity }
      end
    end
  end
end
<%= form_for @goal, url: goal_completed_goal_path(@goal) do |form| %>
  # the form
<% end %>