1

I am having an annoying problem with my form_for method while creating forums. Everytime I try to submit a forum for creation, I receive this error from Rails.param is missing or the value is empty: forum

The problem is in my forums_param method:

def forum_params
  params.require(:forum).permit(:id, :name, :position)
end

The forum part does not exist. The code below is my form for the view:

well.span11
  .span7
    = form_for @forum, url: forums_path, html: { method: :post } do |f|
      = render partial: "form", locals: { f: f }
      .actions
        = submit_tag 'Create', { class: 'btn btn-primary btn-small' }
.clear

And the partial that it renders:

 %fieldset
  %div{class: 'control-group'}
    = label_tag :title, "Title (required)", class: 'control-label required'
    %div{class: 'controls'}
      = text_field_tag :name, nil, class: 'span8'
    - if @forum.errors[:name]
      %p{class: 'error'}#{@forum.errors[:name]}

  %div{class: 'control-group'}
    = label_tag :position, "Position", class: 'control-label'
    %div{class: 'controls'}
      = text_field_tag :position, nil, size: 5

  %div{class: 'control-group'}
    = label_tag :description, "Description", class: 'control-label'
    %div{class: 'controls'}
      = text_area_tag :description, nil, rows: 10, class: 'span10'

Below is the controller code:

  def new
    @forum = Forum.new
  end

  def create
    @forum = Forum.new(forum_params)
    if @forum.save
      redirect_to forums_path, flash: { success: t('.success') }
    else
      redirect_to forums_path, flash: { error: t('.error') }
    end
  end

I'm not sure what is going on here. I have already implemented the recommendations described under these posts.

Solution 1

Solution 2

Solution 3

What is the problem here? Help would be greatly appreciated.

1
  • What's the url / path of the page that you are getting this error on? Commented Jan 21, 2015 at 3:36

2 Answers 2

0

From what I can see, you're missing a bunch of stuff as it goes from view to controller. You have the description field, title etc.. and these aren't being factored into the forum_params

If the user can add these and change them, they must be included in strong params. I don't think id should be in there though... user shouldn't be allowed to change the id. That should be created by AR when the record is created.

Sign up to request clarification or add additional context in comments.

Comments

0

The issue here appears to be your usage of <foo>_tag instead of f.<foo>_field.

When you use <foo>_tag, a literal tag with the attributes you gave it appears in the DOM.

text_field_tag example:

text_field_tag 'title'
# => <input id="title" name="title" type="text" />

Based on an example from the docs. Source: the Ruby on Rails API docs for text_field_tag

Whereas, when you use f.<foo>_field, the name attribute is namespaced under the model name.

f.text_field example:

text_field(:post, :title, size: 20)
# => <input type="text" id="post_title" name="post[title]" size="20" value="#{@post.title}" />

Source: the Ruby on Rails API docs for text_field

Slightly more in-depth explanation

With the examples above, when the first is submitted, the params look like:

{ ..., "title" => "user's input", ... }

You can see from this that if your controller tries to get :post out of this parameters hash, it is nil, and it throws the error you encountered.

The params for the second example in the first section look like:

{ ..., "post" => {"title" => "user's input", ... }, ... }

When the controller tries to get :post out of this hash, it gets the sub-hash containing title (and any other form fields).

I hope this solves your problem!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.