0

I'm creating a controller hook of my issues_controller on Redmine. I followed this tutorial.

The question is I couldn't access the checkbox (That I created through a hook on view page) value to set my controller variable. Follow my code

_new_hook.html.erb

<div class="splitcontentright">
  <% @user = User.current %>
  <b><br><label for="mail_checker_issue"><%= check_box_tag "mail_checker_issue", 1, @user.pref.always_check_email %><%= l(:send_email) %></label></b>
</div>

issues_email_patch.rb

Rails.configuration.to_prepare do
  require_dependency 'issue'
  class Issue
    attr_accessor :mail_checker_issue
  end
end

controller_hook.rb

module Redmine_send_emails
  module Hooks
    class Issues_controller_hook < Redmine::Hook::ViewListener
      def controller_issues_new_before_save(context={})
        context[:issue].mail_checker_issue = context[:params][:mail_checker_issue]
      end
    end
  end
end

Until where I've checked the hook works well. I've debugged the application and it called correctly the controller_hook method, but I couldn't find the value of my checkbox within the params.

What's wrong? How to do it?

kamusett
  • 1,403
  • 1
  • 12
  • 22
  • why do you use `check_box_tag` when (I think) you should use `f.check_box` (as [here](https://github.com/redmine/redmine/blob/master/app/views/issues/_form.html.erb#L6)). And recheck what params come to hook: params or params[:issue]. Because if first you should retrieve value from `context[:params][:issue][:mail_checker_issue]` – gotva Dec 12 '13 at 06:15
  • f.check_box doesn't work because it's a hook and the hook can't see the form f. I got this error: `ActionView::Template::Error (undefined local variable or method f' for #<#:0x6646d10>)` Also I couldn't retrieve the value using `context[:params][:issue][:mail_checker_issue]` – kamusett Dec 12 '13 at 13:00
  • what hook for displaying check_box do you use? For example [this hook](https://github.com/redmine/redmine/blob/master/app/views/issues/_form.html.erb#L44) provides access to `form` – gotva Dec 12 '13 at 13:12
  • [this one](https://github.com/redmine/redmine/blob/master/app/views/issues/new.html.erb#L3) The only hook which exists on this page. – kamusett Dec 12 '13 at 13:19

1 Answers1

1

You don't see your checkbox value because you display it before labelled_form_for @issue so it does not belongs to the form.

You can place it here (there are 2 place for hook)

You can try to implement gem deface - I managed to use it in Redmine plugin.

gotva
  • 5,919
  • 2
  • 25
  • 35
  • It seems to works well as you suggested. I'm gonna do some tests here. Confirming this I come back here to accept your answer. Thanks. – kamusett Dec 12 '13 at 13:35