title: How to call a Rails method from in javascript
body: <p>I'm new in ruby on rails, I want to call controller method from javascript</p>
  <p><strong>show.html.erb</strong></p>
  <pre><code>&lt;label class=&quot;switch&quot;&gt;
    &lt;%if @vehicle.parking%&gt;
      &lt;input type=&quot;checkbox&quot; checked onchange=&quot;parkingMode(&lt;%= @vehicle.id %&gt;)&quot;&gt;
    &lt;%else%&gt;
      &lt;input type=&quot;checkbox&quot; onchange=&quot;parkingMode(&lt;%= @vehicle.id %&gt;)&quot;&gt;
    &lt;%end%&gt;
    &lt;span&gt;&lt;/span&gt;
  &lt;/label&gt;
  </code></pre>
  <p><strong>vehicle_controller.rb</strong></p>
  <pre><code>def change_parking
    vehicle = Vehicle.find(params[:vehicle_id])
    if !vehiculo.parking
      vehicle.parking = true
      vehicle.save
    else
      vehicle.parking = false
      vehicle.save
    end
  end
  </code></pre>
  <p><strong>vehicle.js</strong></p>
  <pre><code>function parkingMode(id){
      console.log(id)
  }
  </code></pre>