我在clients / index.json.jbuilder中有一个基本的index.json.jbuilder文件:
json.array!(@clientsAPI) do |client|
json.extract! client, :year, :business_name
end
当我在视图中提交表单时,@clientsAPI
会在clients_controller的索引方法中更新。但是,index.json.jbuilder不会更新,并保留旧的@clientsAPI
值(在表单提交之前)。
如何在表单刷新时让@clientsAPI
在index.json.jbuilder中获得更新?
clients_controlled code:
def index
@clients = Client.all
begin
@year_filter = params[:c][:year]
rescue
@year_filter = ["2016"]
end
@clientsAPI = @clients.where(year:@year_filter)
render 'index'
end
clients / index.html.erb表单路由到上面显示的索引方法:
<%= form_for :c, url:clients_path, method: :get" do |f| %>
<%= f.select :year, options_for_select(@years),{onchange: "this.form.submit();"} %>
<% end %>
答案 0 :(得分:1)
这是未经测试的,但希望能够让你足够近..
在form_for中,您需要告诉它提交json请求。类似的东西:
<%= form_for :c, url:clients_path(format: 'json'), method: :get" do |f| %>
然后在你的索引方法中,响应json请求:
def index
@clients = Client.all
begin
@year_filter = params[:c][:year]
rescue
@year_filter = ["2016"]
end
@clientsAPI = @clients.where(year:@year_filter)
respond_to do |format|
format.html {
render 'index'
}
format.json {
#any logic here specific to a json request.
#doing nothing will render index.json.jbuilder by default
}
end
端