2016-04-07 8 views
1

私は現在4つのtext_fieldsを持っているフォームを持っていますが、実際にはフォームの下にあるものだけが実際にモデルにデータを追加しますか?すべてのtext_fieldsは同一で、私の人生のために、なぜ彼らはすべて同じように動作しないのか理解できません。うまくいけば私のコードは誰かが簡単な答えを持っていますか?Form_forヘルパー - Rails

class ResponsesController < ApplicationController 

def new 
@response = Response.new 
end 

def create 
@response = Response.new(response_params) 
if @response.save 
    flash[:notice] = "Response has been edited" 
    redirect_to new_response_path(:response) 
else 
    render "new" 
end 
end 

private 

def response_params 
params.require(:response).permit(:message) 
end 

あなたが他を使用している場合、これは、あなたは下のテキストフィールドに入力した場合は、メッセージへの入力データを実際になります私の見解

<div class="container"> 
<div class="row"> 
<h3 class="text-center">Edit The Bounce Back Response</h3> 
    <div class="col-lg-8 col-lg-offset-2 well"> 
    <%= form_for @response do |form| %> 
     <div class="form-group"> 
     <%= form.label :message, "Visitor:", class: "response_label"%> 
     <%= form.text_field :message, class: "form-control", placeholder: "Change Visitor Response!" %> 
     </div> 
     <div class="form-group"> 
     <%= form.label :message, "Staff:", class: "response_label"%> 
     <%= form.text_field :message, class: "form-control", placeholder: "Change Staff Response!" %> 
     </div> 
     <div class="form-group"> 
     <%= form.label :message, "Volunteeer:", class: "response_label"%> 
     <%= form.text_field :message, class: "form-control", placeholder: "Change Volunteer Response!" %> 
     </div> 
     <div class="form-group"> 
     <%= form.label :message, "Dance:", class: "response_label"%> 
     <%= form.text_field :message, class: "form-control", placeholder: "Change Dance Response!" %> 
     </div> 
     <%= form.submit "Update", class: "btn btn-primary" %> 
    <% end %> 
    </div> 

enter image description here

ですテキストフィールド私のコンソールリターンはこれです

enter image description here

+0

uがあなたの 'responses_controller.rb'ファイルを投稿することができますか? – 7urkm3n

+0

あなたの '編集と更新'メソッドはどこですか?あなたのform_forは '_form.html.erb'にありますか? – 7urkm3n

+0

私はまだそこにいません、私はちょうど彼らがモデルを入力するtext_field – Bitwise

答えて

1

これは、毎回:メッセージを送信するフォームがあるためです。フォームが送信されているとき、すべてのフィールドをparams [:message]にポストしているので、最後のフィールドだけが実際にポストされています。

編集:例えば

私が投稿するためのフォームがある場合:

= form_for @post do |f| 
    .input-title 
    = f.text_field :title, placeholder: "Title", required: true 
    .input-content 
    = f.text_area :content, class: 'ckeditor', required: true 
    .input-submit 
    = f.submit 

をそれはHAMLに書かれたが、それはほぼ同じです。私のtext_fieldはタイトルのもので、私の体はちょうどcontentという名前です。私はこの1つを試してみてください

class PostsController < ApplicationController 

     def create 
    @post = current_user.posts.build(post_params) # this is where the params are saved 
    @post.forum_id = params[:forum_id] 
    if @post.save 
     usercount = current_user 
     usercount.post_count += 1 
     usercount.save 
     redirect_to forum_posts_path 
    else 
     render 'new' 
    end 
    end 

     private 

     def post_params 
     params.require(:post).permit(:title, :content) # this is permitting what can be saved from the form 
     end 
+0

私は同意しますが、どうすれば修正できますか? – Bitwise

+0

の代わりに:あなたは単にそれらを記述するものに変更することができます。 –

+0

メッセージはそれを説明しています。最終的には、それぞれがユーザーが選択するフィールドに応じて異なるグループに属しますので、すべてのメッセージをメッセージに投稿します。 – Bitwise

0

強いのparamsを作成し、コントローラに

。私はあなたがそのフィールドを更新したいと思うと理解しています。それはあなたを助けるはずです。

<div class="container"> 
<div class="row"> 
<h3 class="text-center">Edit The Bounce Back Response</h3> 
    <div class="col-lg-8 col-lg-offset-2 well"> 
    <%= form_for @response do |form| %> 
     <div class="form-group"> 
     <%= form.label :message, "Visitor:", class: "response_label"%> 
     <%= form.text_field :message, class: "form-control", placeholder: "Change Visitor Response!" %> 
     </div> 
     <div class="form-group"> 
     <%= form.label :staff, "Staff:", class: "response_label"%> 
     <%= form.text_field :staff, class: "form-control", placeholder: "Change Staff Response!" %> 
     </div> 
     <div class="form-group"> 
     <%= form.label :volunteer, "Volunteeer:", class: "response_label"%> 
     <%= form.text_field :volunteer, class: "form-control", placeholder: "Change Volunteer Response!" %> 
     </div> 
     <div class="form-group"> 
     <%= form.label :dance, "Dance:", class: "response_label"%> 
     <%= form.text_field :dance, class: "form-control", placeholder: "Change Dance Response!" %> 
     </div> 
     <%= form.submit "Update", class: "btn btn-primary" %> 
    <% end %> 
    </div> 
コントローラー更新ストロングのparamsで

def response_params 
    params.require(:response).permit(:message, :staff, :volunteer, :dance) 
end 
関連する問題