1

I have a model in rails called info with address:string and in my view I have the same input field that a user can enter a value into. Heres my form.html

<%= form_for(@info) do |f| %>
  <%= f.text_field :address %>
  <%= f.text_field :address %>
<% end %>

I know it sounds weird not to add another additional field in the info table, but I'm trying hard just to use address column alone. I'm trying to display both the values in the show but only the last one is displayed, my guess that the first address is not saved but the last one is always saved instead of both.

My show html

<p>
  <strong>Addresses:</strong>
  <%= @info.address %>
</p>

How can I save both values into the table from the two input fields and display them both in my show. html? If anyone can provide a way or a solution I would be thankful.

UPDATE

def create
   address_array = params[:info][:address]
   address_string = address_array[0] + ', ' + address_array[1]
   params[:info][:address] = address_string

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

2 Answers 2

1

If you don't want to create two separate address fields altogether, you can pass the same address argument to each of two tag helpers as an array:

<%= f.text_field_tag 'address[]' %>
<%= f.text_field_tag 'address[]' %>

Then, in your controller, you can access the respective values of each of the two fields as members of an array:

address_array = params[:address]
address_string = address_array[0] + ', ' + address_array[1] # Assigns comma delimited string

Thereafter, you can pass address_string as the value for the address attribute of your model.

If you don't want to change your model schema, there's considerable upside to this approach since you can use your address attribute as-is – all you're doing is modifying how the field is treated in the form, and then processing the return values in your controller accordingly.

UPDATE:

The params hash can be modified in-place such that params[:info][:address] reflects your custom string, rather than an array of two string:

def create
    address_array = params[:address]
    address_string = address_array[0] + ', ' + address_array[1]
    params[:info][:address] = address_string # Assigns `address_string` to `params[:info][:address]`
    # remainder of the `create` logic
end

UPDATE 2:

Your new and create controller actions should look something akin to this:

def new
   @info = Info.new
end

def create
   address_array = params[:address]
   address_string = address_array[0] + ', ' + address_array[1]
   params[:info][:address] = address_string

  respond_to do |format|
    # First, instantiate a new instance of `Info`
    @info = Info.new(params[:info])
    # Then, save
    if @info.save
      format.html { redirect_to @info, notice: 'Info was successfully created.' }
      format.json { render action: 'show', status: :created, info: @info }
    else
      format.html { render action: 'new' }
      format.json { render json: @info.errors, status: :unprocessable_entity }
    end
  end
end

UPDATE 3:

If you're having issues modifying the params hash in place, you may want to try assigning the address attribute directly:

def create
   address_array = params[:address]
   address_string = address_array[0] + ', ' + address_array[1]

  respond_to do |format|
    @info = Info.new(params[:info])
    @info.address = address_string # Assign attribute directly
    if @info.save
    # remainder of code
12
  • what method do I adjust in my controller set_info, or info_params Mar 4, 2014 at 5:00
  • @CalvinJones, see my update for details on how to modify your params hash in place. Once you've done so, you can pass the value to your new object instance as you would normally.
    – zeantsoi
    Mar 4, 2014 at 5:09
  • 1
    Copy my controller code precisely, including the new action. Then, make sure that you're passing @info, rather than :info, to your form_for helper. This should be more than sufficient detail to get things working for you.
    – zeantsoi
    Mar 4, 2014 at 5:33
  • 1
    One final update: try accessing params[:address], rather than params[:info][:address]... that should make things work – does it? I've updated my answer to reflect.
    – zeantsoi
    Mar 4, 2014 at 5:59
  • 1
    One more idea: try assigning the address attribute directly. I've depicted this in my third update to the answer. If this fails, something else is amiss – I'm guessing it might be an issue with your model declaration.
    – zeantsoi
    Mar 4, 2014 at 6:16
1

Try something like this:

<%= form_for :info do |f| %>
    <%= f.text_field :address1 %>
    <%= f.text_field :address2 %>
<% end %>

Then in your controller:

def create
    # Look up model object

    address = "#{params[:info][:address1]} #{params[:info][:address2]}"

    # save model object with address
  end

The basic idea is to create the form without using the attributes on your model, then in the controller pull the needed info out of the params hash and make the column data yourself.

6
  • in the view you have address1 and address2, I'm guessing you can't just use address for both input fields. I saw stackoverflow.com/questions/8556888/… this one which is very similar but don't know what content_array is or how to display it Mar 4, 2014 at 3:38
  • There are several different ways to do this, no matter what though you're going to have to manipulate the data somewhere. Using a separate column in your database really is the easiest approach. Is there a reason you want to combine them? Mar 4, 2014 at 3:44
  • no reason I just want to display both values in my show somehow using one column thanks for the input and help dude Mar 4, 2014 at 3:47
  • Of course man. Rails works really hard to make things easy for you, for stuff like this its best not to fight it :) Mar 4, 2014 at 4:00
  • 1
    @DanFairaizl, I'm not convinced this approach works. When the form_for helper accepts a model instance as an argument, it reconciles the attributes on the model with the fields being generated. Since address1 and address2 aren't attributes – and they're presumably not being generated on the fly – this approach will likely throw an exception.
    – zeantsoi
    Mar 4, 2014 at 5:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.