我正在尝试使用以下站点中的表单在我的Rails应用程序中设置站点范围的全局变量:
http://spin360-staging.herokuapp.com/printer
结果应该在这里可见:
http://spin360-staging.herokuapp.com/printer.json
但是,如果你刷新json,你会发现它似乎没有持久性 - 它似乎在你刷新页面时改变了值。
以下是表单的内容:
<%= form_tag edit_printer_path do %>
<%= label_tag "Type" %>
<%= select_tag(:type, options_for_select(
[
['Set', "set"],
['Spin', "spin"]
], 1
)) %>
<%= label_tag "ID" %>
<%= text_field_tag :print_id %>
<%= label_tag "Format" %>
<%= select_tag(:layout, options_for_select(
[
['Receipt', "receipt"],
['Filmstrip', "filmstrip"],
['Flipbook', "flipbook"]
], 1
)) %>
<%= submit_tag "Submit", :class => "submit_button" %>
控制器方法:
def edit_printer
Rails.application.config.print_type = params[:type]
Rails.application.config.print_id = params[:print_id]
Rails.application.config.print_layout = params[:layout]
redirect_to(:back)
end
最后,JSON模板(printer.json.erb)
{
"print": [{"type":"<%= Rails.application.config.print_type %>","id":<%= Rails.application.config.print_id %>,"layout": "<%= Rails.application.config.print_layout %>"}]
}