Ruby on Rails form_for创建空对象

时间:2016-04-01 02:00:54

标签: ruby-on-rails ruby-on-rails-4 form-for

我在alerts / views /文件夹中有一个名为_form.html.erb的部分名称。我在我的agencies / views /文件夹中的show页面中渲染了部分内容。

所有内容都正确呈现,而部分内容确实会创建新警报,但警报完全为空。

alerts_controller创建方法

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 %>

除了创建的警报完全为空外,没有错误。 body,severity和agency_id变量都是零。

我试过更换线

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

用这个:

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

并添加以下行:

@alert = Alert.new

到代理商控制器中的show方法。

但是同样的事情发生了。我做错了什么?

修改

这是我点击提交时开始的日志,并在加载alerts.create方法中的重定向之前结束。

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)

当我注释掉alerts.create方法并添加以下行时:

render plain: params

这是输出:

{"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"}

2 个答案:

答案 0 :(得分:1)

您的params调试显示了根本原因。 params哈希中包含alert个密钥。 alert值是bodyseverity的哈希值。

在您的控制器中,您引用params[:body]params[:severity]。这些应该是params[:alert][:body]params[:alert][:severity]

有没有理由不使用Rails的强参数?你可以重构一下:

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

答案 1 :(得分:1)

create method 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