Save the content of a text area into a file

How can I replace the input with a textarea?

Using Sinatra-Framework

<form action="/create" method="post">
    <input type="text" name="file" value="<%= @contents %>">
    <input type="submit" value="Save">
</form>

e.g.

<textarea id="text" name="text" rows="25" cols="150"><%=@contents%></textarea>

When the page renders? Use JavaScript (or in this case jQuery).

var $input = $("input[name='file']"),
    contents = $input.val();

$input.remove();

var $textArea = $("<textarea />", {
    id: "text",
    name: "text",
    rows: 25,
    cols: 150,
    text: contents
});

$textArea.insertBefore($("input[type='submit']"));

I’m not sure that’s what you mean though :confused:

Hi Pullo,

thank you very much for your help.
I’m sorry but I can’t get it running, because I don’t know how to adapt my Ruby main.rb accordingly.

Please see the inital situation below.

Kind regards

main.rb:
require "sinatra"

get '/create' do
  @logfile = File.open("logfile.txt","r")
  @contents = @logfile.read
  @logfile.close
  erb :create
end

post '/create' do
  @logfile = File.open("logfile.txt","w")
  @logfile.truncate(@logfile.size)
  @logfile.write(params[:file])
  @logfile.close
  redirect '/create'
end

views/create.erb:
see HTML form of my initial post

Sorry, I’m not sure what you’re trying to do.

If you literally just want to replace the input with the text area, do this in views/create.erb

<form action="/create" method="post">
    <textarea id="text" name="file" rows="25" cols="150"><%=@contents%></textarea>
    <input type="submit" value="Save">
</form>

Had to change the name of the text area from text to file.

Or can you rephrase your question.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.