How to make that the typed code on ruby in text_area it was carried out in @params ["name"]?? -- Posted via http://www.ruby-forum.com/.
up -- Posted via http://www.ruby-forum.com/.
Sorry guys - I don''t want to be a help vampire, but I''ve been through a ton of tutorials, and they''re all written by guys who obviously have a solid backing in programming already because they seem to plow through without really explaining how things work in a way I (i.e. idiots) can understand. Anyway, thanks to everybody who helped me yesterday. You helped me send a value to a view. Now I''m trying to send a value from a text field back to the controller to check and see if it''s the correct answer. But I don''t quite understand how Rails wants me to do this. Here, for example: <%= start_form_tag :action => ''answer'' %> <%= text_field ''questions'', ''answer'' %> <%= submit_tag ''check answer'' %> <%= end_form_tag %> I went to the Rails API and found the explanation of text_field: the first value is the object, and the second value is the method. But I already set up the method that I want to go to in the start_form_tag part. So for some reason I have to call it twice? But my biggest issue is, where do I put the value that the user will enter into the form field? And how do I retrieve that value once I''m in the ''answer'' method in the controller? I''m used to PHP and HTML forms, where you specify a name="something" and then you can get the variable called "something" from the _POST array. But how does Rails do this? Obviously there''s no _POST array, but what is the Rails equivalent? Again, thanks and apologies in advance for being the newbie with all the questions. -- Posted via http://www.ruby-forum.com/.
Sean: The "method" to which you''re referring is really an attribute of an object. That is, it''s a field on an object that is going to be in the parameters of the post. The :action symbol of the start_form_tag tells Rails that you want to call the answer action when the user submits the form. So, you''re not actually doing anything twice. Regarding your biggest issue, if the object that you''re using already has a value, then the form field will display that automatically. To retrieve that value, you can access the params associative array in your answer method of your controller. If you haven''t already used the scaffold command, give it a try to get some generic CRUD views and controller. Study how the objects are instantiated for display on the edit page, and how a different action is called (update) when the form is submitted. It really helped me understand what was the intention of the framework. Hope it helps, -Anthony On Apr 4, 2006, at 9:17 PM, sean colquhoun wrote:> Sorry guys - I don''t want to be a help vampire, but I''ve been > through a > ton of tutorials, and they''re all written by guys who obviously have a > solid backing in programming already because they seem to plow through > without really explaining how things work in a way I (i.e. idiots) can > understand. > > Anyway, thanks to everybody who helped me yesterday. You helped me > send > a value to a view. Now I''m trying to send a value from a text field > back > to the controller to check and see if it''s the correct answer. But I > don''t quite understand how Rails wants me to do this. > > Here, for example: > <%= start_form_tag :action => ''answer'' %> > <%= text_field ''questions'', ''answer'' %> > <%= submit_tag ''check answer'' %> > <%= end_form_tag %> > > I went to the Rails API and found the explanation of text_field: the > first value is the object, and the second value is the method. But I > already set up the method that I want to go to in the start_form_tag > part. So for some reason I have to call it twice? But my biggest issue > is, where do I put the value that the user will enter into the form > field? And how do I retrieve that value once I''m in the ''answer'' > method > in the controller? I''m used to PHP and HTML forms, where you specify a > name="something" and then you can get the variable called "something" > from the _POST array. But how does Rails do this? Obviously there''s no > _POST array, but what is the Rails equivalent? > > Again, thanks and apologies in advance for being the newbie with > all the > questions. > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Sean, A couple things to sort out here: In the start_form_tag you have an ''action''. An action is a method in a _controller_. Your text_field documation shows it defined like this: *text_field*(object_name, method, options = {}) In this case ''object_name'' is most likely the name of an active record model object. The method mentioned must be a method of the model object. Now, I''ll try to answer your specific questions:>But my biggest issue >is, where do I put the value that the user will enter into the form >field?I don''t understand your question. I think you are asking how to store it in the database. If so, you need an active record model object. Do you have a copy of the rails book? (agile web development with rails).>And how do I retrieve that value once I''m in the ''answer'' method >in the controller?In the controller you can refer to whatever was typed into the text area by params[:questions][:answer], as in obj.answer = params[:questions][:answer] Or more commonly obj.update_attributes(params[:questions]) It occurs to me that you may not care to store this in a database -- that you just want to check the answer. If that is the case then you are using the wrong method. Instead of text_field, you should use text_field_tag. text_field is specifically about connecting active record models to forms. text_field_tag is simply a tag and you can do whatever you want with the result -- in other words you don''t need a model object. So if you simply want an answer from a form, use text_field_tag. It will be something like text_field_tag ''answer'' In your controller, in this case, you will check params[:answer].Note that there is no longer a reference to :questions. Good luck. -Kelly On 4/4/06, sean colquhoun <seancolquhoun@gk-a.com> wrote:> > Sorry guys - I don''t want to be a help vampire, but I''ve been through a > ton of tutorials, and they''re all written by guys who obviously have a > solid backing in programming already because they seem to plow through > without really explaining how things work in a way I (i.e. idiots) can > understand. > > Anyway, thanks to everybody who helped me yesterday. You helped me send > a value to a view. Now I''m trying to send a value from a text field back > to the controller to check and see if it''s the correct answer. But I > don''t quite understand how Rails wants me to do this. > > Here, for example: > <%= start_form_tag :action => ''answer'' %> > <%= text_field ''questions'', ''answer'' %> > <%= submit_tag ''check answer'' %> > <%= end_form_tag %> > > I went to the Rails API and found the explanation of text_field: the > first value is the object, and the second value is the method. But I > already set up the method that I want to go to in the start_form_tag > part. So for some reason I have to call it twice? But my biggest issue > is, where do I put the value that the user will enter into the form > field? And how do I retrieve that value once I''m in the ''answer'' method > in the controller? I''m used to PHP and HTML forms, where you specify a > name="something" and then you can get the variable called "something" > from the _POST array. But how does Rails do this? Obviously there''s no > _POST array, but what is the Rails equivalent? > > Again, thanks and apologies in advance for being the newbie with all the > questions. > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060405/3b825ce8/attachment.html
Thanks Anthony and Kelly - I am, in fact, on chapter 9 of that Rails book as we speak. :-) But it''s turning out to be more difficult than I thought. The book''s basic explanations are very, very basic, bordering on the purely theoretical, and the implementation examples are too advanced. That bit about using text_field for saving data in a database and text_field_tag, for example, I would never have found out if Kelly hadn''t been kind enough to clue me in (Thanks Kelly - you saved me a ton of wasted time). Is there a resouce out there somewhere that can help me figure out this middle ground? -- Posted via http://www.ruby-forum.com/.
Is there a resouce out there somewhere that can help me figure out this middle ground? I don''t have a good answer for you. I''ve spent a lot of time digging through the available docs at http://api.rubyonrails.com/, the rails book, and the rails sources. One thing cool about this stuff is you most likely have all the sources right there on your machine. I''m constantly searching through them and adding print statements to try to understand what is happening. -Kelly On 4/4/06, sean colquhoun <seancolquhoun@gk-a.com> wrote:> > Thanks Anthony and Kelly - > > I am, in fact, on chapter 9 of that Rails book as we speak. :-) > > But it''s turning out to be more difficult than I thought. The book''s > basic explanations are very, very basic, bordering on the purely > theoretical, and the implementation examples are too advanced. That bit > about using text_field for saving data in a database and text_field_tag, > for example, I would never have found out if Kelly hadn''t been kind > enough to clue me in (Thanks Kelly - you saved me a ton of wasted time). > Is there a resouce out there somewhere that can help me figure out this > middle ground? > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060409/f88d3227/attachment.html