[ask] how to save/get data from view hook's input form ?
Added by rangga rahadian over 8 years ago
i want to create a redmine plugin with add some input form on issue form page. i have created a view hook on bottom issue form like the picture bellow
what i want to ask is, how i can get the input form data?
any example code/plugin that i can learn from?
thanks.
really appeciate for your help
Replies (9)
RE: [ask] how to save/get data from view hook's input form ? - Added by Martin Denizet (redmine.org team member) over 8 years ago
Hello,
Assuming you are trying to save this field:
Redmine uses a system called SafeAttributes to assign input to object properties.
See this example.
Given that you added the field reward_points to the issues table, I suggest you simply add reward_points to the safe_attributes
.
According to #6000, you should be able to do:
Issue.safe_attributes 'reward_points'
After the attribute is assigned, you can use before_save
or after_save
callbacks to run custom logic.
If you just want to get the value in the controller:
You can use params
in controller_issues_new_before_save and controller_issues_edit_before_save
See the list of hooks
Cheers,
RE: [ask] how to save/get data from view hook's input form ? - Added by rangga rahadian over 8 years ago
Martin Denizet (redmine.org team member) wrote:
Hello,
Assuming you are trying to save this field:
Redmine uses a system called SafeAttributes to assign input to object properties.
See this example.
Given that you added the field reward_points to the issues table, I suggest you simply add reward_points to thesafe_attributes
.
According to #6000, you should be able to do:[...]
After the attribute is assigned, you can use
before_save
orafter_save
callbacks to run custom logic.If you just want to get the value in the controller:
You can use
params
in controller_issues_new_before_save and controller_issues_edit_before_save
See the list of hooksCheers,
hello,
thanks for your reply. i think i need much guidance, do you mind?
i have add the reward point field to issues table. i have look at your example (issue.rb line 430), do you mean i add reward_points to safe_attributes in issue.rb by executing the code Issue.safe_attributes 'reward_points' method call? where i should place that call/code?
RE: [ask] how to save/get data from view hook's input form ? - Added by Martin Denizet (redmine.org team member) over 8 years ago
I didn't test but that's what I would try:
init.rb
require 'redmine'
Rails.configuration.to_prepare do
Issue.safe_attributes 'reward_points'
end
Redmine::Plugin.register :my_plugin do
name '...'
author '...'
url '...'
author_url '...'
description '...'
version '0.0.1'
end
If that would not work, I'd try to patch the Issue
class.
Let me know if that works please.
RE: [ask] how to save/get data from view hook's input form ? - Added by rangga rahadian over 8 years ago
Martin Denizet (redmine.org team member) wrote:
I didn't test but that's what I would try:
init.rb
[...]If that would not work, I'd try to patch the
Issue
class.
Let me know if that works please.
solved
i've tried, and i got 500 error message when i submit new issue.
could you tell me what steps i must doing after database migration (adding reward points) and edit init.rb file?
any example before_save or after_save callback code for safe attribute ?
RE: [ask] how to save/get data from view hook's input form ? - Added by Martin Denizet (redmine.org team member) over 8 years ago
To my understanding, you should only add the safe_attribute
for it to be saved automatically for creation/edition.
RE: [ask] how to save/get data from view hook's input form ? - Added by Martin Denizet (redmine.org team member) over 8 years ago
The name attribute of your html input field is issue[reward_points]
, right?
Do you have the complete code available somewhere?
RE: [ask] how to save/get data from view hook's input form ? - Added by rangga rahadian over 8 years ago
Martin Denizet (redmine.org team member) wrote:
The name attribute of your html input field is
issue[reward_points]
, right?
Do you have the complete code available somewhere?
sorry for late update
your sugest about to configure only safe_attribute was right.
it works! big thanks
the screenshot
and for html view code, im following other's plugin code.
i add a condition so any users except the issue author can't change the field value
one question if you dont mind,
preface, im creating a gamification plugin for redmine. i add rewarding, purchasing rewards, leaderboard, and badges feature.
im thinking to modify he issue status dropdown list, so it will have same rule like the reward point field (any users except the issue author can't change issue status to closed). do you think its possible?
im thinking to hide the closes option from issue status drop-down list if current user isn't the issue author
why i wanted to hide that closed option because after the issue status changed to closed, my plugin will give the reward point to assignee (i set a hook after save). if any user can change issue status to closed multiple times, especially assignee, he will receive the reward points multiple times for the same task/issue. which is not what i wants.
RE: [ask] how to save/get data from view hook's input form ? - Added by Martin Denizet (redmine.org team member) over 8 years ago
I suggest you better rely on the native Workflow feature for that.
RE: [ask] how to save/get data from view hook's input form ? - Added by rangga rahadian over 8 years ago
Martin Denizet (redmine.org team member) wrote:
I suggest you better rely on the native Workflow feature for that.
ok, thanks for the suggestion. i'll search for other alternatives for that issue