2

I'm using a form_for to create new Records. Each record has a main photo url :photo and three optional photo urls that I'm storing in a serialized array, other_photos.

In my form, I want to give the user an option to add up to three additional photo urls, which would be stored in other_photos[0], other_photos[1], and other_photos[2]

I added the column to the database, added serialize :other_photos, Array to the model, and permitted the strong params :other_photos => []

How do I handle this in my form? Right now, I have this for my main photo:

<%= form_for @record do |r| %>
    ...
    <%= label_tag :name, "IMAGE URL:", class: "login-label" %>
    <%= r.text_field :photo, size: 50 %>

How do I set the params for other_photos? This doesn't work:

    <%= r.text_field :other_photos[0], size: 50 %>

2 Answers 2

3

I got it to work. I'm still a little hazy on how I did this, but here are the pieces I used in Rails 4.2:

First, I created a new text column called :other_photos and migrated it. Next:

In the model:

serialize :other_photos, Array

In the controller:

Set permissions:

params.require(:record).permit(:name, :caption, {:other_photos =>[] })

def create
  @record = Record.new(record_params)
  @record.other_photos = params[:other_photos]     # this was the piece that held me back

  ...

end

In the view:

<%= form_for @record do |r| %>
  <%= text_field_tag "other_photos[]" %>    
  <%= text_field_tag "other_photos[]" %> 
  <%= text_field_tag "other_photos[]" %>      # "r.text_field" didn't work for some reason

  ...

<% end %>
Sign up to request clarification or add additional context in comments.

1 Comment

The reason is rails looks for a symbol or string for the given form builder attribute. So you need a text_field_tag to give it the arbitrary name, then as @Wizard of ogz said you can use how rails uses arrays and form parameters to get the information!
1

Do not add an index inside the square brackets of your field name. See the guide on passing Arrays through params. In that guide the example for Arrays is shown using URL query string parameters, but the same principles apply to form encoded parameters.

<%= r.text_field "other_photos[]", size: 50 %>

You can add as many such fields as you desire

<%= r.text_field "other_photos[]", size: 50 %>
<%= r.text_field "other_photos[]", size: 50 %>
<%= r.text_field "other_photos[]", size: 50 %>

Then params[:record][:other_photos] will have an Array value with an element for each input on your form.

Be careful because empty text inputs will still be submitted and end up as empty strings. For example, if only the first of the three inputs shown above was given a value then other_photos would be set to something like this ["photo_url.png", "", ""]

1 Comment

I got it to work! I'm still confused, but I'll post the solution below.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.