0

I've been trying to figure out how to post a string into an array from a user input text field that is part of a form. The push and << methods work fine over the console, but I couldn't manage to save any string data into arrays from my forms. It always reports a type mismatch like this:

Attribute was supposed to be a Array, but was a String.

What is the appropriate way to deal with this ?

EDIT code example:

the array:

serialize(:name, Array)

Let's say that the form contains just a simple text field, nothing else. How would I store the input data into the array above ? Basically what's the correct format of this piece of code:

<%= form_tag(grids_path , method: "post") do %>
    <table id="grid">
    <thead>
        <tr>
            <th>Name</th>
        </tr>
    </thead>

    <tbody>
        <tr>
            <td><%= text_field(:grid, :name) %></td>
        </tr>
    </tbody>

    </table>
    <%= submit_tag("Save") %>

<% end %>
2
  • Where is your relevant code? Commented Aug 19, 2015 at 10:11
  • Sorry, edited. The code is basically non-existant as I simply need to learn of a way to usually do this kind of thing. Commented Aug 19, 2015 at 10:16

2 Answers 2

2

If you simply want to store a param into and array, you should do something like this:

<%= text_field_tag 'grid[name][]' %>

then the params['grid']['name'] will contain an array of strings

Sign up to request clarification or add additional context in comments.

4 Comments

I want to store it from my form which is in the view.
It's in the post now.
Rails is not reporting errors anymore, it just seems that the data isn't being saved properly. But that's another issue unrelated to this I suppose. This is what I'm looking for then, thanks !
By the way, a quick question. What if I used form_for tag like this: <%= form_for(@grid) do |f| %> How would the syntax for the fields go then ?
0

In your view:

<%= text_field_tag :my_string_a, 'String A' %>
<%= text_field_tag :my_string_b, 'String B' %>

In your controller:

my_array_of_strings = []
my_array_of_strings << params[:my_string_a]
my_array_of_strings << params[:my_string_b]

puts my_array_of_strings

Hope this works.

5 Comments

I'd like to store multiple strings into an array called :name, for example. The strings to be stored are from the form's text field.
Where in the controller should I put your code ? It reports this error: undefined local variable or method params' `
In your example <%= form_tag(grids_path , method: "post") do %>, you used grids_path. Therefore, I am assuming the controller is grids_controller, and the method is create. Therefore you could add these lines above inside def create
You guessed right, I'll try your code out definitely just to test it all out. Thanks !
No prob! Good luck! :)

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.