30

I have a Result that belongs to a Website. After I create the website I also create the result and redirect to its edit page. Here I want to add some more values.

My problem is: When I try to update my Result, then I get:

param is missing or the value is empty: result


    Request

    Parameters:

    {"utf8"=>"✓",  "_method"=>"patch",  "authenticity_token"=>"GRN/y/04Qbsm9DzlUAbUYF8ZSv2EMHnRZgBZY/6GMDlOBdq8V5Uncij9VRp51uydC6M/qc61jPWwpUehSuc5xA==", "data"=>["//html/body/div[position() = 3]/ul/li[position() = 16]/ul/li[position() = 2]/child::text()",  "//html/body/div[position()
    = 3]/ul/li[position() = 16]/ul/li[position() = 2]/p/a/child::text()",  "//html/body/div[position() = 3]/ul/li[position() = 16]/ul/li[position() = 4]/child::text()",  "//html/body/div[position()
    = 3]/ul/li[position() = 16]/ul/li[position() = 5]/a/child::text()"],  "commit"=>"Update Result",  "id"=>"66"}

This is how my Result params looks like

def result_params
      params.require(:result).permit(:data)
    end

My model:

class Result < ActiveRecord::Base
  belongs_to :website
  attr_accessor :website_id
  attr_accessor :data

  serialize :data, Array
end

Here is my controller code:

    class ResultsController < ApplicationController
  before_action :set_result, only: [:show, :edit, :update, :destroy]

  # GET /Results
  # GET /Results.json
  def index
    @results = Result.all
  end

  # GET /Results/1
  # GET /Results/1.json
  def show
  end

  # GET /Results/new
  def new
    @result = Result.new
  end

  # GET /Results/1/edit
  def edit
    @result = Result.find(params[:id])
  end

  # POST /Results
  # POST /Results.json
  def create
    @result = Result.new(result_params)

    respond_to do |format|
      if @result.save
        format.html { redirect_to @result, notice: 'Result was successfully created.' }
        format.json { render :show, status: :created, location: result }
      else
        format.html { render :new }
        format.json { render json: @result.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /Results/1
  # PATCH/PUT /Results/1.json
  def update
    respond_to do |format|
      if @result.update(result_params )
        format.html { redirect_to @result, notice: 'Result was successfully updated.' }
        format.json { render :show, status: :ok, location: @result }
      else
        format.html { render :edit }
        format.json { render json: @result.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /Results/1
  # DELETE /Results/1.json
  def destroy
    @result.destroy
    respond_to do |format|
      format.html { redirect_to results_url, notice: 'Result was successfully destroyed.' }
      format.json { head :no_content }
    end
  end


  private
    # Use callbacks to share common setup or constraints between actions.
    def set_result
      @result = Result.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def result_params
      params.permit(:data => [])
    end
end

My view:

<%= form_for(@result) do |f| %>
  <% if @result.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@result.errors.count, "error") %> prohibited this result from being saved:</h2>

      <ul>
      <% @result.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>


    <div class="field">
      <% if @result.website.url != nil %>
      <%= atts = get_all_elements(@result.website.url)%>
          <% atts.each do |p| %>
              <div>
                <%= check_box_tag "data[]", get_xpath_from_node(p)%>
                <%= p.text %>
              </div>
          <%end%>
      <% end%>
    </div>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

And this is the place where i call the edit result page:

def update
    respond_to do |format|
      if @website.update(website_params)
        format.html { redirect_to @website, notice: 'Website was successfully updated.' }
        format.json { render :show, status: :ok, location: @website }
      else
        format.html { render :edit }
        format.json { render json: @website.errors, status: :unprocessable_entity }
      end
    end
  end

Ive allready tryed every solution I could find, but none of them seemed to work for me.

user2926430
  • 391
  • 1
  • 7
  • 15

2 Answers2

57

The problem lies here:

params.require(:result).permit(:data)

From require documentation,

require ensures that a parameter is present. If it's present, returns the parameter at the given key, otherwise raises an ActionController::ParameterMissing error.

You are requiring result parameter but it's missing from the params. All your values are inside data param. Removing require should do the trick.

params.permit(:data)

If you want to keep require, wrap data inside result in forms.

Nitish Parkar
  • 2,838
  • 1
  • 19
  • 22
  • Thanks that did fix that error. Now I can update without any errors beeing thrown, but my data is not beeing saved. Do you have any idéa why that would be the case now? – user2926430 Jun 14 '15 at 13:57
  • In your logs do you see `Unpermitted parameters: data`? or something like that? – Nitish Parkar Jun 14 '15 at 14:03
  • Yes. I got: Unpermitted parameters: utf8, _method, authenticity_token, data, commit, id – user2926430 Jun 14 '15 at 14:19
  • What is data in unpermitted parameters? What did you end up using inside `result_params`? – Nitish Parkar Jun 14 '15 at 14:24
  • My result_params looks like you sugested: params.permit(:data). My data looks like the request in my question – user2926430 Jun 14 '15 at 14:33
  • Are you using ActiveAdmin? http://stackoverflow.com/questions/29275809/found-unpermitted-parameters-utf8-authenticity-token-only-for-update-method – Nitish Parkar Jun 14 '15 at 14:42
  • Ive fixed it. I forgot to do specify my :data as array in result_params. So it ended up looking like this: params.permit(:data => []). Now it works. Thanks for your help! – user2926430 Jun 14 '15 at 14:43
  • Awesome! Glad I could help :) – Nitish Parkar Jun 14 '15 at 14:44
  • 8
    Is this really a solution or a hack? I am facing this in AngularJS when posting empty forms to Rails API. Without the .permit of course it goes on submitting, but I don't find it a solution really. – Radolino Dec 11 '15 at 13:37
  • @Radolino In `permit` you whitelist the attributes you wish to update. it's not a hack. – Nitish Parkar Dec 12 '15 at 17:43
  • 5
    +1 on @Radolino's comment. Wouldn't this whitelist anything that's going to be passed in the params hash? – dimitry_n Dec 18 '16 at 21:18
  • This error also occurs if you try to have a form submit without any fields. If you just want the single value you're passing through a button param you can use a hidden_field as a work around. (Added on behalf of @jacktrades). – LMC Feb 07 '18 at 02:04
12

I know, what you met. if you strong params is

def result_params
  params.require(:result).permit(:data)
end

Your parameters should has the format like this

Parameters: {"result"=>{"data"=>"string"}}

and your parameters is just

Parameters: {"data"=>"string"}

so you should remove the "result"

TorvaldsDB
  • 766
  • 9
  • 8