Question

Update: Zishe figured it out. Correct params.require code should be:

def adventure_params
  params.require(:adventure).permit(:story, :choice, :parent_id, :user_id)
end

with parenthesis instead of a bracket, of course.

Original question:

I am making a form_for that should be submitting 4 attributes to the Adventure model. The adventure model has ancestry. I keep getting wrong number of arguments (4 for 1) based on my params.require method. When I change to requirements down to 1, I see that all the attributes are blank in my database. I know they are in the params but for some reason they are not being saved. Here is my code:

Form

<div class="form">
  <%= form_for @adventure do |f| %>
    <%= f.label :choice %>
    <%= f.text_area :choice %>

    <%= f.label :story %>
    <%= f.text_area :story %>

    <%= f.label :parent_id %>
    <%= f.text_field :parent_id %>

    <%= f.submit "Post"%>
  <% end %>
</div>

Controller

class AdventuresController < ApplicationController
  before_filter :authenticate_user!

  def home
  end

  def index
  end

  def new
    @parent_id = params[:parent_id]
    @adventure = Adventure.new
  end

  def show
    @adventure = Adventure.find(params[:id])
  end

  def create
    @user = current_user
    @adventure = current_user.adventures.build(adventure_params)
    if @adventure.save
      flash[:success] = "Adventure created!"
      redirect_to @adventure
    else
      flash[:error] = "There was an error"
      redirect_to adventures_path
    end
  end

  private

    def adventure_params
      params.require(:adventure).permit[:story, :choice, :parent_id, :user_id]
    end
end

Model

class Adventure < ActiveRecord::Base
  has_ancestry
  belongs_to :user
  validates :user_id, presence: true
  validates :parent_id, presence: true
end

I have no idea why I am getting wrong number of arguments since the attributes show up in the params.

Was it helpful?

Solution

Change permit[...] to:

.permit(:story, :choice, :parent_id, :user_id)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top