How to add a new action to the existing controller?

后端 未结 4 791
南笙
南笙 2020-12-13 02:19

I\'m pretty new in Rails. Sorry for the noob question.

I\'ve create a new controller: rails new controller Say hello goodbye

How can i add a new

4条回答
  •  遥遥无期
    2020-12-13 03:14

    When you repeat the generate command with another method's name, you can skip overwriting the existing controller and it's test implementation. That will automatically add the route and create a view. After that you have to add the method manually to the controller because that action didn't touch the controller file.


    Example:

    one method called new was already created in the controller when it was initially created:

    $ rails generate controller Person new
    ... successfully created the controller, it's route and view ...
    

    when trying to add a new method:

    $ rails generate controller Person all
    Running via Spring preloader in process 28648
        conflict  app/controllers/person_controller.rb
    Overwrite ../app/controllers/person_controller.rb? (enter "h" for help) [Ynaqdh] h
        Y - yes, overwrite
        n - no, do not overwrite
        a - all, overwrite this and all others
        q - quit, abort
        d - diff, show the differences between the old and the new
        h - help, show this help
    Overwrite ../app/controllers/person_controller.rb? (enter "h" for help) [Ynaqdh] n
            skip  app/controllers/person_controller.rb
           route  get 'person/all'
          invoke  erb
           exist    app/views/person
          create    app/views/person/all.html.erb
          invoke  test_unit
        conflict    test/controllers/person_controller_test.rb
    Overwrite ../test/controllers/person_controller_test.rb? (enter "h" for help) [Ynaqdh] n
            skip    test/controllers/person_controller_test.rb
          invoke  helper
       identical    app/helpers/person_helper.rb
          invoke    test_unit
          invoke  assets
          invoke    coffee
       identical      app/assets/javascripts/person.coffee
          invoke    scss
       identical      app/assets/stylesheets/person.scss
    

提交回复
热议问题