NOTE: To make it easier, skip to the EDIT part below. Thank you!
I am currently working on adding a simple feature to my app.
The feature involves a view with a chart that depends on two query parameters: :from and :to. I am using "Chartkick" and below are snippets of my view and controller.
Here is the index view along with a debug feature:
<%= params %>
<input type="text" id="from-date">
<%= column_chart dashboard_chart_path(:from => @from, :to => @to) %>
Controller:
class DashboardController < ApplicationController
def index
@from = params[:from]
@to = params[:to]
end
def call_from_ajax
from = params[:from]
to = params[:to]
render action: 'index', to: to, from: from
end
end
Everything is working as expected when I manually input query parameters in the URL. The chart displays the correct data. Now, I have this JavaScript code for testing:
$(document).ready(function(){
$('#from-date').pickadate({
onClose: function() {
array ={
"from": "20-05-2018",
"to": "01-05-2018"
}
$.ajax({
type: 'GET',
url: 'dashboard_chart_ajax',
data: array
})
},
});
Route:
get 'dashboard_chart_ajax', to: 'dashboard#call_from_ajax'
The ajax call is working correctly. The 'call_from_ajax' method is triggered, and my console confirms that the Index view has been rendered. However, the params variable in my view does not update, nor does the chart... (I verified using byebug, params does contain :from and :to values). What am I overlooking? Appreciate any assistance.
EDIT:
For practice purposes, let's consider a different scenario:
I included a new route
get 'button', to: "dashboard#test"
My controller methods
def test
redirect_to controller: :dashboard, action: :index, from: 'start_date', to: 'end_date'
end
def index
@from = params[:from]
@to = params[:to]
end
And a button in the view
<%= button_to "Some Button", button_path, method: :get, remote: true %>
<%= params %>
I am aiming to have the params variable in the view updated upon clicking the button.
When I initially load the page, in the :index method I see:
<ActionController::Parameters {"controller"=>"dashboard", "action"=>"index"} permitted: false>
Upon clicking the button, I see:
<ActionController::Parameters {"from"=>"start_date", "to"=>"end_date", "controller"=>"dashboard", "action"=>"index"} permitted: false>
However, the view remains unchanged... I must be missing something crucial, as I am still learning.
My objective is to modify a variable with my 'test' method, and reflect this change in the same view.
If anyone can offer assistance, I would greatly appreciate it.