2016-04-01 4 views
0

アラート/ビュー/フォルダに部分的に_form.html.erbという名前があります。私は代理店/ビュー/フォルダ内のショーページからその部分をレンダリングします。Ruby on Rails form_forは空のオブジェクトを作成します

すべてが正しくレンダリングされ、部分は新しい警告を作成しますが、警告は完全に空です。

は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変数はすべてnilです。

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

この行を追加:

私はラインこれに

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

を交換しようとしている政府機関の制御装置における表示方法に

@alert = Alert.new 

でも同じことが起こります。私は間違って何をしていますか?

EDIT

これは、ログ、私が提出ヒット時に始まり、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"} 
+1

あなたは 'console'ログを共有する可能性があるため?このメソッドを試してみてください: '@alert = Alert.new({body:params [:body]、 重大度:params [:severity]、 agency_id:@ current_member.id})' – 7urkm3n

+1

また、これを追加してトリガーします。 'render plain plams'はデータをどのように渡しているかを調べます。 – 7urkm3n

+0

2番目のコメントをもっと説明できますか?乾杯。 –

答えて

1

根本的な原因は、あなたのparamsデバッグから明らかです。 paramsハッシュにはalertというキーがあります。 alertの値は、bodyseverityのハッシュです。

コントローラでは、params[:body]params[:severity]を参照してください。それらはparams[:alert][:body]params[:alert][:severity]である必要があります。

Railsの強力なパラメータを使用しない理由はありましたか?このようなことになってcreate methodのparamsで

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 
+0

ありがとうございました!しかし今私は、Postsのための別のフォームがあり、params [:body]、params [:title]などに直接アクセスするので混乱します。なぜコードがその情報ではなく、 params? –

+0

また、誰かがform_for(Alert.new)を使用するのがform_for(@alert)を使用するほど良くないと言いました。違いは何ですか?私は一方的に揺れるべきですか? –

+0

@MatthewCliattもしあなたがそのフォームのものを編集していないのであれば、ちょっと変わっても大丈夫です。 –

1

:あなたのような何かにリファクタリングできます。

あなたの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 
関連する問題