Search code examples
ruby-on-railsrubyform-helpers

Form_for helpers - Rails


I have a form right now that has 4 text_fields and only the bottom one actually adds the data to the model? All the text_fields are identical and for the life of me I can't figure out why they all don't work the same. Here is my code hopefully someone will have any easy answer?

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

this is my view

<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

If You type in the bottom text field it will actually input the data to message, if you use any other text field my console return is this

enter image description here


Solution

  • It is because you have the form submitting to :message everytime. So when the form is being submitted it is posting every field to params[:message] and so only the last one is actually being posted.

    Edit:

    For instance if I have a form for posts:

    = 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
    

    It is written in haml but it is nearly identical. You can see that my text_field is for the title and my body is just named content.

    In the controller I would create strong params

        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