Making a Little Application

Hi everyone.

I am really new to Ruby on Rails, so I apologize if this is a really simple question but I just can't think of how I could do this. I want there to be a button that will add a drop down menu to the screen, and then depending on what the user selects it will add either a text field or a text "area" it will add the selected one to the right of the menu. Also, the user would have to be allowed to delete certain "fields" as well. Then when the user submits the form Ruby on Rails will be able to see how many menus were made as well as read what the user put in all of them. I hope this makes sense, but if it doesn't, just ask for clarification. :slight_smile:

If anyone could help, it would be great.

Hello Peirce, I am a newbie myself, so take any ideas I give you with a great big grain of salt, but I think you have two options here:

  1. Instead of a button, use a link. Then, when the user clicks on the link, your controller could gather the information submitted (such as what was selected) and redirect the browser to a new page which would present the same information plus either a text field or a text area. This has the benefit of simplicity and sticks with what you probably already know (hopefully), but it will be somewhat clunky and look different than the more modern way of doing what you describe.

  2. The more modern way of doing what you describe is to use JavaScript and AJAX to dynamically render new portions of your page dependent upon what the user has entered so far. This has the benefit of looking very slick when done well, but requires that you learn something about JavaScript and AJAX. Fortunately, there are a lot of resources (in the form of web pages with tutorials as well as a plethora of books at your local technical bookstore).

Hopefully, these couple of ideas will get you thinking in one direction or another.

–wpd

Pierce wrote:

Hi everyone.

I am really new to Ruby on Rails, so I apologize if this is a really simple question but I just can't think of how I could do this. I want there to be a button that will add a drop down menu to the screen, and then depending on what the user selects it will add either a text field or a text "area" it will add the selected one to the right of the menu. Also, the user would have to be allowed to delete certain "fields" as well. Then when the user submits the form Ruby on Rails will be able to see how many menus were made as well as read what the user put in all of them. I hope this makes sense, but if it doesn't, just ask for clarification. :slight_smile:

If anyone could help, it would be great.

Hi Pierce,

Your example is explained in a difficult way. Could you point at an example on a existing website?

Actually, what you describe here is pretty much a lot of UI stuff, this can be done with AJAX and showing / hiding html elements.

Thanks, Michael

Ruby on Rails in not a magic wand even you can do a magical stuff with it. I have read your description a couple of times and with a "I am missing something" filing. Perhaps you should first do a simple paper prototyping and then when all actions are known and clear move to implementation.

Since this look like a simple web application, more like a students homework, perhaps the Ruby on Rails is not a right tool for it. Take look at Sinatra http://sinatra.rubyforge.org/ perhaps it's sufficient framework for your task.

@Patrick Doyle

I don't know about the first suggestion, but I know that the second option can work but brings up some problems... For example, when the page is refreshed the user looses all of the new fields. Also, it gets hard to manage in Javascript (at least the way that is below) because it gets a lot of code.

@Michael Hendrickx

Yeah, I kind of assumed it was hard to hard to understand. :slight_smile: I have wrote a quick example using Javascript, which is below this text. I know how to do this in Javascript, but there are some problems so I am looking if there is a way to do this in Ruby on Rails. The problems with doing it in Javascript are outlined above.

@Dejan Dimic

As I said, I kind of guessed that it would be hard to understand. I hope the code below makes more sense, but if not just say so and I will try to explain it better. Pretty much, a button/link creates a new object and shows it on the page. Preferably it would not loose the new fields (and their values) on a refresh. I am doing a rewrite of a web application in Ruby on Rails, and am wondering how to avoid doing this in Javascript again.

I suppose there really isn't a reason why I need to do this in Ruby on Rails, but Javascript brings up some problems and I think it may be easier in Ruby on Rails in general. If anyone could give me some advice about this, it would be great.

Code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:// www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml&quot;&gt; <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Script Example</title> <script type="text/javascript"> var number = 1;

function add() {   var table = document.getElementById('table');

  var textfield = document.createElement('INPUT');   var a = document.createElement('A');   var li = document.createElement('LI');

  textfield.setAttribute("Type", "text");   textfield.setAttribute("Name", "TextField_"+number);   textfield.setAttribute("Id", "TextField_"+number);

  a.onclick = function()   {     remove (li);     remove(textfield);     remove(a);   }

  a.innerHTML = "Remove";

  table.appendChild(li);   li.appendChild(textfield);   li.appendChild(a);

  number++; }

function remove(removeItem) {   removeItem.parentNode.removeChild(removeItem); }

</script> </head>

<body> <a href="javascript:add()">Add</a>

  <table id="table">   </table> </body> </html>

Sorry for the bump, but I am about to begin the rewrite and am wondering if there is some way to do this in Rails.