Home>

I want to solve

I want to create the following process.

    Trying to CONTROLLER as a trigger as a trigger for operations such as pressing the
  1. button
  2. Controller performs some processing with the received variable, and displays the processing result to View Screen

1. Whether to realize how JavaScript variables are given to Controller, do you not teach how or code?

Environment

  • Ruby 2.6.3
  • Rails 6.0.3

Thank you very much.

  • Answer # 1

    There are many ways

    1. hidden_field is prepared and the button is pressed instantly fill the value in the field and leave it to Form Submit

    2. button When you create an HTTP request at the time of the pressed trigger

    If you do it in the way of rails

    I think that it is beautiful and easy to use this Data-PARAMS

    view.html.erb

    <
    %= Button_TO 'Label', 'Data-URL': XXX_Path, ID: 'YYY'% >
    
    

    Alternatively LINK_TO can be Form + Submit

    Application.js

    $ ('# YYY'). On ('click', ()= >
     {
      $ ('# Yyy'). Attr ('data-params',
        'AAA=' + 'Favorite value made with JavaScript' +
        '&
    BBB '+' Favorite value made with JavaScript ');
    }
    
    

    As it is possible, it is a premise that jQuery is in the premise.
    Native JavaScript has the same method

    Set the DATA-XXX attribute to jQuery

    $ ('# YYY'). Data ('PARAMS', 'CCC=' + 'favorite value');
    
    It is also prepared that 

    is prepared, but it may not be possible to move even if this is a long time ago

    XXX_CONTROLLER.RB

    DEF XXX
      AAA= PARAMS.REQUIRE ('AAA')
      pp aaa
    
    

    Whenever you pass, be sure to make any data as a string, so please cast it as appropriate on the Rails side.

    Do not check the operation, so I'm sorry if the details are wrong

  • Answer # 2

    There are many ways

    1. hidden_field is prepared and the button is pressed instantly fill the value in the field and leave it to Form Submit

    2. button When you create an HTTP request at the time of the pressed trigger

    If you do it in the way of rails

    I think that it is beautiful and easy to use this Data-PARAMS

    view.html.erb

    <
    %= Button_TO 'Label', 'Data-URL': XXX_Path, ID: 'YYY'% >
    
    

    Alternatively LINK_TO can be Form + Submit

    Application.js

    $ ('# YYY'). On ('click', ()= >
     {
      $ ('# Yyy'). Attr ('data-params',
        'AAA=' + 'Favorite value made with JavaScript' +
        '&
    BBB '+' Favorite value made with JavaScript ');
    }
    
    

    As it is possible, it is a premise that jQuery is in the premise.
    Native JavaScript has the same method

    Set the DATA-XXX attribute to jQuery

    $ ('# YYY'). Data ('PARAMS', 'CCC=' + 'favorite value');
    
    It is also prepared that 

    is prepared, but it may not be possible to move even if this is a long time ago

    XXX_CONTROLLER.RB

    DEF XXX
      AAA= PARAMS.REQUIRE ('AAA')
      pp aaa
    
    

    Whenever you pass, be sure to make any data as a string, so please cast it as appropriate on the Rails side.

    Do not check the operation, so I'm sorry if the details are wrong