This example is based on the ruby ruport practice-Report parameter implementation to implement addition, deletion, and modification of table parameters when the report is executed and defined.
1. Modify views/report_executions/edit.html. ERB.
Displays not only the report parameters saved when the new report is executed, but also the remaining parameters dynamically (up to five control parameters can be defined here ), this avoids forgetting to define report parameters or adding report parameters.
<H1> editing report_execution </p> </P> <p> <% form_for (@ report_execution) Do | f |%> <br/> <% = f. error_messages %> </P> <p> <br/> <% = f. label: execute_code % >:< br/> <% = f. text_field: execute_code %> <br/> </P> <br/> <p> <br/> <% = f. label: execute_name % >:< br/> <% = f. text_field: execute_name %> <br/> </P> <br/> <p> <br/> <% = f. label: report_definition_id % >:< br/> <% = report_definitions_control %> <br /> </P> <br/> <p> <br/> <% = f. label: report_template_id %>: <br/> <% = report_templates_control %> <br/> </P> <br/> <p> report parameters </p> <p/> <br/> <! -- Display existing report parameters --> <br/> <% if @ report_parameters.length> 0%> <br/> <% for I in 0..@report_parameters.length-1%> <br/> <p> <br/> <% = hidden_field_tag 'parameter _ id [] ', @ report_parameters [I]. report_parameter_id %> <br/> parameter <% = I + 1% >:< % = text_field_tag 'parameter _ name [] ', @ report_parameters [I]. parameter_name %> <br/> value <% = I + 1%>: <% = text_field_tag 'parameter _ value [] ', @ report_parameters [I]. parameter_valu E %> <br/> </P> <br/> <% end %> <br/> <! -- Other parameter boxes are displayed. To add parameters, click here --> <br/> <% for I in @ report_parameters.length + 1 .. 5%> <br/> <p> <br/> parameter <% = I %>: <% = text_field_tag 'parameter _ other_name [] '%> <br/> value <% = I %>: <% = text_field_tag 'parameter _ other_value [] '%> <br/> </P> <br/> <% end %> <br/> <p> <br/> <% = f. submit 'update' %> <br/> </P> <br/> <% end %> </P> <p> <% = link_to 'show ', @ report_execution %> | <br/> <% = link_to 'back', report_executions_path %>
Ii. Modify reportexecutionscontroller
The create_report_parameter, update, and destroy methods are mainly modified.
Def index <br/> @ report_executions = reportexecution. all <br/> respond_to do | format | <br/> format.html # index.html. ERB <br/> format. XML {render: XML =>@ report_executions} <br/> end </P> <p> # Get/report_executions/1 <br/> # Get/report_executions/1.xml< br /> def show <br/> @ report_execution = reportexecution. find (Params [: Id]) <br/> @ report_definition = reportdefinition. find (@ report_ex Ecution. report_definition_id) <br/> @ report_template = reporttemplate. find (@ report_execution.report_template_id) <br/> @ report_parameters = reportparameter. find_by_ SQL ("select * From report_parameters where report_execute_id =#{ Params [: Id]}") </P> <p> respond_to do | format | <br/> format.html # show.html. ERB <br/> format. XML {render: XML =>@ report_execution} <br/> end </P> <p> # Get/Repo Rt_executions/New <br/> # Get/report_executions/New. XML <br/> def new <br/> @ report_execution = reportexecution. new <br/> respond_to do | format | <br/> format.html # new.html. ERB <br/> format. XML {render: XML =>@ report_execution} <br/> end </P> <p> # Get/report_executions/1/edit <br/> def edit <br/> @ report_execution = reportexecution. find (Params [: Id]) <br/> report_definition = reportd Efinition. find (@ report_execution.report_definition_id) <br/> report_template = reporttemplate. find (@ report_execution.report_template_id) <br/> @ latest_report_name = report_definition.report_name <br/> @ latest_template_name = report_template.template_name <br/> @ report_parameters = reportparameter. find_by_ SQL ("select * From report_parameters where report_execute_id =#{ Params [: Id]}") </P> <p> If (@ Re Port_parameters.length> 0) <br/> puts "has parameters # {@ report_parameters.length }===================" <br/> @ report_parameters.each do | f | <br/> puts "# {f. parameter_name }:# {f. parameter_value} "<br/> end <br/> puts" has parameters ================ = "<br/> end </P> <p> def create_report_parameters (parameter_name, parameter_value) <br/> flag = true <br/> If (parameter_name) <br/> F Or I in 0 .. parameter_name.length-1 <br/> If (parameter_name [I]! = "") <Br/> puts "Create report parameter: # {parameter_name [I]}, # {parameter_value [I]}" <br/> report_parameter = reportparameter. new <br/> parameters ("report_execute_id", @ override) <br/> report_parameter.write_attribute ("parameter_name", parameter_name [I]) <br/> report_parameter.write_attribute ("parameter_value ", parameter_value [I]) <br/> If! Report_parameter.save <br/> flag = false <br/> end <br/> return flag <br/> end <br/> # post/report_executions <br/> # post/report_executions.xml <br/> def create <br/> @ report_execution = reportexecution. new (Params [: report_execution]) <br/> # @ report_parameter = reportparameter. new (Params [: report_parameter]) <br/> respond_to do | format | <br/> If @ report_execution.save <B R/> If create_report_parameters (Params [: parameter_name], Params [: parameter_value]) <br/> FLASH [: Notice] = 'reportexecution was successfully created. '<br/> end <br/> format.html {redirect_to (@ report_execution)} <br/> format. XML {render: XML =>@ report_execution,: Status =>: created,: Location =>@ report_execution }< br/> else <br/> format.html {render: action => "new"} <br/> format. XML {rend Er: xml => @ report_execution.errors,: Status =>: unprocessable_entity} <br/> end </P> <p> # Put/report_executions/1 <br/> # Put/report_executions/1.xml< br/> def update <br/> @ report_execution = reportexecution. find (Params [: Id]) <br/> respond_to do | format | <br/> If @ report_execution.update_attributes (Params [: report_execution]) <br/> FLASH [: notice] = 'reportexecution was succe Ssfully updated. '</P> <p> If (Params [: parameter_id]) <br/> for I in 0 .. params [: parameter_id]. length-1 <br/> report_parameter = reportparameter. find (Params [: parameter_id] [I]) <br/> If (Params [: parameter_name] [I]! = "") <Br/> puts "# {report_parameter.report_parameter_id} shocould be updated" <br/> report_parameter.update_attribute ("parameter_name", Params [: parameter_name] [I]) <br/> report_parameter.update_attribute ("parameter_value", Params [: parameter_value] [I]) <br/> else <br/> puts "# {report_parameter.report_parameter_id} shocould be deleted" <br/> report_parameter.destroy <br/> end </P> <p> create_report_parameters (Params [: parameter_other_name], Params [: parameter_other_value]) </P> <p> format.html {redirect_to (@ report_execution)} <br/> format. XML {head: OK} <br/> else <br/> format.html {render: Action => "edit"} <br/> format. XML {render: xml => @ report_execution.errors,: Status =>: unprocessable_entity} <br/> end </P> <p> # delete/report_executions/1 <br/> # delete/report_executions/1.xml< br/> def destroy <br/> @ report_execution = reportexecution. find (Params [: Id]) <br/> @ report_parameters = reportparameter. find_by_ SQL ("select * From report_parameters where report_execute_id =#{@ report_execution.report_execute_id}") <br/> If (@ report_parameters.length> 0) <br/> @ report_parameters.each do | report_parameter | <br/> puts "Destroy report_parameter # {parameter}" <br/> report_parameter.destroy <br/> end <br /> @ report_execution.destroy </P> <p> respond_to do | format | <br/> format.html {redirect_to (report_executions_url )} <br/> format. XML {head: OK} <br/> end
Demo effect (main demo edit interface ):