Answer the question
In order to leave comments, you need to log in
How to collect all data from a form in Rails?
I write the data from the form to a JSON file, I made the form, in the controller I figured out how to create a file, the file is created where necessary with the right name, but I don’t understand how I can serialize (or something else) to collect all the data from the form and in the right format put them in the created file.
The form itself:
<h1>Тут можно добавить элементы</h1>
<%= form_for :article, url: articles_path do |f| %>
<p>
<%= f.label :Категория %><br>
<%= f.text_field :text %>
</p>
<p>
<%= f.label :Название %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :Ссылка_на_картинку %><br>
<%= f.text_field :text %>
</p>
<p>
<%= f.label :id %><br>
<%= f.text_field :text %>
</p>
<p>
<%= f.label :Цвет %><br>
<%= f.text_field :text %>
</p>
<p>
<%= f.label :Цена %><br>
<%= f.text_field :text %>
</p>
<p>
<%= f.submit :Добавить %>
</p>
<% end %>
class ArticlesController < ApplicationController
def new
end
def create
require 'json'
data = [{ "foo" => "bar" } , { "foo1" => "bar1" }]
File.open("output.json", "w+") do |f| f.write(JSON.generate(data)) end
redirect_to :back
# File.open("myList.json", "w") { @my_model.my_data = params[:my_fields] }
end
end
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question