1 / 21

Lecture 14 Rails – Forms

This lecture covers topics related to forms in Rails, including creating forms, form submission, and redirection. It also discusses debugging techniques and the use of the interactive debugger.

rosalynm
Télécharger la présentation

Lecture 14 Rails – Forms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 14Rails – Forms CSCE 740 Software Engineering • Topics • SaaS • Readings: SaaS book Ch 4.3-4.5 March 3, 2014

  2. Rails - • Last Time • SaaSbook 4.3 Models • app/models/movies.rb • rake db:migrate • rails console – walkthrough • rake db:seed • 4.4 Controllers, Views • movies_controller.rb • views/movies/index.html.haml • 4.5 Debugging • RASP • New • SaaSbook4.6 Forms • xxx • 4.4 Controllers, Views • xxx • Next Time:

  3. http://www.cyberciti.biz/tips/top-linux-monitoring-tools.html --- 510 • .

  4. RASP

  5. Printf debugging – Code instrumentation • code Instrumentation • stderr • printf • logging • interactive debugger

  6. Instrumenting a rails app • Display a detailed description of an object in a view. • inserting = debug(@movie) or • = @movie.inspect in any view • the leading = tells Haml to execute the code and insert the result • “Stop the show” inside a controller method by raising an exception • raise params.inspect to see the detailed value of the params • Use logger.debug( msg) to print msg to the log • logger is available in models and controllers and can record messages with a variety of urgencies; • compare config/environments/production.rb & development.rb • rails server –debugger • insert debugger where you would like a break point

  7. show movie 999

  8. Figure 4.12: Command summary of the interactive Ruby debugger • n execute next line • s execute next statement • f finish current method call and return • p expr print expr, which can be anything that’s in scope within the current stack frame • eval expr evaluate expr; can be used to set variables that are in scope, as in eval x=5 • up go up the call stack, to caller’s stack frame • down go down the call stack, to callee’s stack frame • where display where you are in the call stack • b file:num set a breakpoint at line num of file • b method set a breakpoint when method called • c continue to next break • quit

  9. Summary of Debugging • Use a language-aware editor with syntax highlighting and automatic indentation to help find syntax errors. • Instrument your app by inserting the output of debug or inspect into views, or by making them the argument of raise, which will cause a runtime exception that will display message as a Web page. • To debug using the interactive debugger, make sure your app’s Gemfile includes debugger, start the app server with rails server --debugger, and place the statement debugger at the point in your code where you want to break.

  10. 4.6 Form Submission: New & Create • There are three problems we need to address: • How do we display a fill-in form to the user? • How is the information filled in by the user actually made available to the controller action, so that it can be used in a create or update ActiveRecord call? • What resource should be returned and displayed as the result of a RESTful request to create or update an item?

  11. Forms • params hash • create a new movie is 2 steps • new – retrieve blank form • create – submit the form • create new movie could be one step in SaaS

  12. Rails Cookbook : create new form • To create a new submittable form • Identify the action that gets/renders the form • Identify the action that receives the submission • Create routes, action, views for each • In form view, form element name attributes control how values will appear in params [ ] • Helpers privide for many common elements

  13. helpers • radio boxes

  14. Get the form: RESTful action new • Use automatically-provided RESTful URI helper new_movie_path to create a link to the form. • http://pastebin.com/XUGTnere • -# add to end of index.html.haml • = link_to 'Add new movie', new_movie_path

  15. Creating the form in HAML • app/views/movies/new.html.haml • Screencast 4.6.1 • http://pastebin.com/cWFkjxf3   • = form_tagmovies_path, :method => :post do • = label :movie, :title, 'Title' • = text_field :movie, :title • = label :movie, :rating, 'Rating' • = select :movie, :rating, Movie.all_ratings • = label :movie, :release_date, 'Released On‘ • = date_select :movie, :release_date • = submit_tag 'Save Changes'

  16. Summary: Generate the form • Rails provides form helpers to generate a fill-in form whose fields are related to the attributes of a particular type of ActiveRecord object. • When creating a form, you specify the controller action that will receive the form submission by passing form_tag the appropriate RESTful URI and HTTP method (as displayed by rake routes) . • When the form is submitted, the controller action can inspect params[], which will contain a key for each form field whose value is the user-supplied contents of that field.

  17. 4.7 Redirection and the Flash • 4.7 Redirection and the Flash • Screencast 4.7.1: The Create action • Fox, Armando; Patterson, David (2014-01-31). Engineering Software as a Service: An Agile Approach Using Cloud Computing (Kindle Location 3629). Strawberry Canyon LLC. Kindle Edition.

  18. Summary • Although the most common way to finish a controller action is to render the view corresponding to that action, for some actions such as create it’s more helpful to send the user back to a different view. Using redirect_to replaces the default view rendering with a redirection to a different action. • Although redirection triggers the browser to start a brand-new HTTP request, the flash can be used to save a small amount of information that will be made available to that new request • You can DRY out your views by putting markup to display flash messages in one of the application’s templates, rather than having to replicate it in every view that might need to display such messages.

  19. Self-Check 4.7.1. Why must every controller action either render a view or perform a redirect? • HTTP is a request-reply protocol, so every action must generate a reply. One kind of reply is a view (Web page) but another kind is a redirect, which instructs the browser to issue a new request to a different URI. • Self-Check 4.7.2. In Figure 4.13, given that we are going to output an HTML tag, why does line 2 begin with - rather than =? = directs Haml to evaluate the Ruby expression and substitute it into the view, but we don’t want the value of the if-expression to be placed in the view—we want the actual HTML tag, which Haml generates from #notice.message, plus the result of evaluating flash[:notice], which is correctly preceded by

  20. 4.8 Finishing CRUD: Edit/Update and Destroy

More Related