I would like to know how to generate a form in Rails so that I can upload files without problems, I have read several tutorials, but it still costs me a bit to do it.
Controller:
class ContratosController < ApplicationController
DOCUMENTO_TOKEN = "public/uploads/documentotoken"
def index
@tituloindex = "Emitir Asset";
@tituloinfo = "Info de Contrato";
@coins = params[:coins];
if request.post?
documento = params[:documento];
#Nombre Original del Documento
nombre = documento.original_filename;
#Directorio donde se va a guardar
directorio = DOCUMENTO_TOKEN;
#Extension del archivo
extension = nombre.slice(nombre.rindex("."), nombre.length).downcase;
if extension == ".pdf" or extension == ".jpg"
#ruta del archivo
path = File.join(directorio, nombre);
else
@formato_erroneo = true;
end
end
end
def info
end
end
View:
<h1><%= @tituloindex; %></h1>
<table width="600px">
<%= form_tag({:controller => "contratos", :action => "index"}, :method => "post", :multipart => true) do %>
<tr>
<td><%= label_tag "coins", "Initial Coins" %></td>
<td><%= text_field_tag "coins", "", :size => 20, :maxlenght => 5, :placeholder => "coins", :required => "required" %></td>
</tr>
<tr>
<td><%= label_tag "decimals", "decimals" %></td>
<td><%= text_field_tag "decimals", "", :size => 20, :maxlenght => 5, :placeholder => "decimals", :required => "required" %></td>
</tr>
<tr>
<td><%= label_tag "tokenname", "Token Name" %></td>
<td><%= text_field_tag "tokenname", "", :size => 20, :maxlenght => 5, :placeholder => "Token Name", :required => "required" %></td>
</tr>
<tr>
<td><%= label_tag "tokenpreffix", "Token Preffix" %></td>
<td><%= text_field_tag "tokenpreffix", "", :size => 20, :maxlenght => 5, :placeholder => "Token Preffix", :required => "required" %></td>
</tr>
<tr>
<td><%= label_tag "selecttoken", "Select Token" %></td>
<td><%= select_tag "selecttoken",
"<option>crowdsale</option>
<option>ShareHolder Company</option>
<option>Simple Token</option>
<option>Pannic</option>
<option>comoditie</option>".html_safe %></td>
</tr>
<tr>
<td><%=label_tag "documento","Documento de Identidad (Solo *.pdf o *.jpg)"%></td>
<td><%= file_field_tag "documento", :required => "required" %></td>
</tr>
</table>
<%= submit_tag "Enviar" %>
<% end %>
<div id="resultados" >
<% if @coins %>
<%= "Se han enviado #{@coins} Lescovex ha su cuenta" %>
<% else %>
<%= "verifique bien los datos" %>
<% end %>
</div>