Pregunta

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
¿Fue útil?

Solución

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

Otros consejos

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top