Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get data from html form to ruby in Ruby on Rails

Get data from html form to ruby in Ruby on Rails

I have some html like this

<html>
<h1>Text to PDF</h1>
<textarea name="comments" cols="40" rows="5">
Enter your Text here...
</textarea><br>
<input type="submit" value="Submit" />
</form>
</body>
</html>

I want to give the value of the text into the controller for this page/view. How do I do this with rails?

I am new to rails, what is the mechanism for this? I don't need to write to the database just want to hand it to the controller. If there is a good tutorial for this sort of thing that would be great, I am not convince I am approaching this correctly.

like image 500
Maestro1024 Avatar asked Jan 29 '26 16:01

Maestro1024


2 Answers

You can use params['comments'] in your controller to get the value.

like image 114
Mitch Dempsey Avatar answered Jan 31 '26 06:01

Mitch Dempsey


In your controller-

  def parse_comments
    comments_from_form = params['myform']['comments']
    #do your stuff with comments_from_form here
  end

In your view-

<h1>Text to PDF </h1>
<%= form_tag :action => 'parse_comments' do %>
    <%= text_area :myform, :comments, :cols => '40', :rows => '5' %>
    <%= submit_tag "Submit" %>
<% end %>

(edit: added = to form_tag opening, without it code won't work)

like image 29
daustin777 Avatar answered Jan 31 '26 07:01

daustin777



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!