Matt C
Matt C

Reputation: 4555

Ruby on Rails form_for creates empty object

I have a partial named _form.html.erb in my alerts/views/ folder. I render that partial from the show page in my agencies/views/ folder.

Everything renders properly, and the partial does create a new alert, but the alert is completely empty.

alerts_controller create method

def create
  @alert = Alert.new(body: params[:body],
                   severity: params[:severity],
                   agency_id: @current_member.id)

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

_form.html.erb

<%= form_for(Alert.new) do |f| %>
  <div class="input-group">
    <div class="field">
      <%= f.label :body %><br>
      <%= f.text_field :body %>
    </div>
    <div class="field">
      <%= f.label :severity %><br>
      <%= f.number_field :severity %>
    </div>
    <div class="action">
      <%= f.submit %>
    </div>
  </div>
<% end %>

There are no errors, except that the alert created is completely empty. The body, severity, and agency_id variables are all nil.

I have tried replacing the line

<%= form_for(Alert.new) do |f| %>

with this:

<%= form_for(@alert) do |f| %>

and adding this line:

@alert = Alert.new

to the show method in the agency controller.

But the same thing happens either way. What am I doing wrong?

EDIT

This is the log starting when I hit submit, and ending before loading the redirect in the alerts.create method.

Started POST "/alerts" for ::1 at 2016-03-31 18:29:43 -0400
Processing by AlertsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"CgpepnYUec9uUoE/Ys6SAkOlzmK+w2q9IN782sXNoXnB3UyuegiS6m+W+mW+nXu4EIL8P8xH6JdigU8FfmzhVw==", "alert"=>{"body"=>"This is the body text", "severity"=>"12345"}, "commit"=>"Create Alert"}
  Account Load (0.1ms)  SELECT  "accounts".* FROM "accounts" WHERE "accounts"."id" = ? LIMIT 1  [["id", 7]]
  Agency Load (0.1ms)  SELECT  "agencies".* FROM "agencies" WHERE "agencies"."id" = ? LIMIT 1  [["id", 2]]
   (0.1ms)  begin transaction
  SQL (0.2ms)  INSERT INTO "alerts" ("agency_id", "created_at", "updated_at") VALUES (?, ?, ?)  [["agency_id", 2], ["created_at", "2016-03-31 22:29:43.014846"], ["updated_at", "2016-03-31 22:29:43.014846"]]
   (135.8ms)  commit transaction
  Agency Load (0.1ms)  SELECT  "agencies".* FROM "agencies" WHERE "agencies"."id" = ? LIMIT 1  [["id", 2]]
Redirected to http://localhost:3000/agencies/2
Completed 302 Found in 141ms (ActiveRecord: 136.2ms)

When I comment out the alerts.create method and add this line:

render plain: params

This is the output:

{"utf8"=>"✓", 
"authenticity_token"=>"3OfteFX41SV/5NxpTcKbP7AKhLm/ZKah+NXVn84e2xwXMP9wWeQ+AH4gpzORkXKF4y225M3gJIu6imZAdb+bMg==", 
"alert"=>{"body"=>"This is the body text.", "severity"=>"12345"},
"commit"=>"Create Alert", "controller"=>"alerts", "action"=>"create"}

Upvotes: 1

Views: 1873

Answers (2)

Lucas Nelson
Lucas Nelson

Reputation: 2551

The root cause is evident from your params debugging. The params hash has an alert key in it. The alert value is a hash of body and severity.

In your controller, you reference params[:body] and params[:severity]. Those should be params[:alert][:body] and params[:alert][:severity].

Was there a reason not to use Rails' strong parameters? You could refactor to something like:

def create
  @alert = Alert.new(alert_params.merge(agency_id: @current_member.id)
  …
end
…

private

  def alert_params
    params.require(:alert).permit(:body, :severity)
  end

Upvotes: 1

7urkm3n
7urkm3n

Reputation: 6311

In the create method params supposed to be like this.

Because your params "alert"=>{"body"=>"This is the body text.", "severity"=>"12345"}

def create
  @alert = Alert.new(body: params[:alert][:body],
                   severity: params[:alert][:severity],
                   agency_id: @current_member.id)

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

Upvotes: 1

Related Questions