New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add params option for button_to #10471

Merged
merged 1 commit into from Oct 24, 2013

Conversation

Projects
None yet
@andyw8
Contributor

andyw8 commented May 5, 2013

The parameters are rendered as hidden form fields within the generated form. This is useful for when a record has multiple buttons associated with it, each of which target the same controller method, but which need to submit different attributes.

For example, a bug tracking system may have buttons to mark an issue as Closed or Fixed. These should both make a PATCH request to /issues/:id. This could be implemented by adding member routes to the Issue resource, but for some apps that may result in a large number of additional entries in routes.rb.

I understand that there is already a mechanism for adding additional params to the query string for button_on, but it's usually preferable to have this data in the request body.

@rafaelfranca

View changes

actionpack/lib/action_view/helpers/url_helper.rb Outdated
@@ -287,6 +288,7 @@ def button_to(name = nil, options = nil, html_options = nil, &block)
url = options.is_a?(String) ? options : url_for(options)
remote = html_options.delete('remote')
params = html_options.delete('params') || {}

This comment has been minimized.

@rafaelfranca

rafaelfranca May 7, 2013

Member
html_options.delete('params') { Hash.new }
@rafaelfranca

View changes

actionpack/CHANGELOG.md Outdated
@@ -1,3 +1,4 @@
* No changes.
* adds params option to button_to form helper, which renders the given hash

This comment has been minimized.

@rafaelfranca

rafaelfranca May 7, 2013

Member
Adds `params` option to `button_to` form helper, which renders the given hash
as hidden form fields.

*Andy Waite*

This comment has been minimized.

@andyw8

andyw8 May 7, 2013

Contributor

The Contributing To Rails guide says Your name can be added directly after the last word if you don't provide any code examples or don't need multiple paragraphs. Otherwise, it's best to make as a new paragraph. Is it incorrect?

@rafaelfranca

View changes

actionpack/lib/action_view/helpers/url_helper.rb Outdated
@@ -213,6 +213,7 @@ def link_to(name = nil, options = nil, html_options = nil, &block)
# * <tt>:form</tt> - This hash will be form attributes
# * <tt>:form_class</tt> - This controls the class of the form within which the submit button will
# be placed
# * <tt>:params</tt> - Hash of params to be rendered as hidden fields within the form.

This comment has been minimized.

@rafaelfranca

rafaelfranca May 7, 2013

Member

Hash of parameters

@andyw8

This comment has been minimized.

Contributor

andyw8 commented May 7, 2013

@rafaelfranca updated as per your suggestions.

@arunagw

This comment has been minimized.

Member

arunagw commented Jun 10, 2013

@andyw8 required a rebase here.

@andyw8

This comment has been minimized.

Contributor

andyw8 commented Jun 10, 2013

Rebased

@adrianpacala

This comment has been minimized.

Contributor

adrianpacala commented Jun 10, 2013

👍

1 similar comment
@augnustin

This comment has been minimized.

augnustin commented Jul 19, 2013

👍

@augnustin

View changes

actionpack/lib/action_view/helpers/url_helper.rb Outdated
@@ -310,6 +312,9 @@ def button_to(name = nil, options = nil, html_options = nil, &block)
end
inner_tags = method_tag.safe_concat(button).safe_concat(request_token_tag)
params.each do |name, value|
inner_tags.safe_concat tag(:input, type: "hidden", name: name, value: value)

This comment has been minimized.

@augnustin

augnustin Jul 19, 2013

Awesome pull request !

In line inner_tags.safe_concat tag(:input, type: "hidden", name: name, value: value), value:value should be value:value.to_param instead. Otherwise when value is an ActiveRecord (or any fancy object), it prints a wrong value.

This comment has been minimized.

@andyw8

andyw8 Aug 24, 2013

Contributor

Updated as per your suggestion, thanks.

@WojtekKruszewski

This comment has been minimized.

Contributor

WojtekKruszewski commented Aug 7, 2013

+1

@robin850

View changes

actionpack/CHANGELOG.md Outdated
* Adds `params` option to `button_to` form helper, which renders the given hash
as hidden form fields.
*Andy Waite*

This comment has been minimized.

@robin850

robin850 Aug 25, 2013

Member

Could you please move the changelog entry to the top of the file?

This comment has been minimized.

@andyw8

andyw8 Aug 25, 2013

Contributor

Done

@robin850

This comment has been minimized.

Member

robin850 commented Aug 25, 2013

Also it will nedd a rebase, this doesn't merge cleanly. Thank you!

@andyw8

This comment has been minimized.

Contributor

andyw8 commented Aug 25, 2013

Rebased

@WojtekKruszewski

This comment has been minimized.

Contributor

WojtekKruszewski commented Aug 28, 2013

👍

1 similar comment
@mzahir

This comment has been minimized.

Contributor

mzahir commented Sep 13, 2013

👍

Add params option for button_to
The parameters are rendered as hidden form fields within the generated
form. This is useful for when a record has multiple buttons associated
with it, each of which target the same controller method, but which
need to submit different attributes.
@leisti

This comment has been minimized.

leisti commented Sep 25, 2013

A good idea.

@vipulnsward

This comment has been minimized.

Member

vipulnsward commented Sep 27, 2013

LGTM 👍

@leisti

This comment has been minimized.

leisti commented Sep 27, 2013

I'm facing exactly the situation that this pull request would solve. I have a form that displays a table. In one of the columns, I have a checkbox for each displayed item. I have several buttons, one for each possible action that can be performed on the selected items. I don't want to have to define a separate route for each action, because the selected action is more like a function parameter for one action.

The way I've solved this for now is to add some Javacode that triggers on the click of any one of the buttons (based on their class), and then copies the id of the button to a hidden input field of the form, which is then transmitted to the controller in the parameter list of the HTTP call.

@cicloid

This comment has been minimized.

cicloid commented Oct 22, 2013

👍 Sounds like a nice convention that would remove a couple lines of code

@WojtekKruszewski

This comment has been minimized.

Contributor

WojtekKruszewski commented Oct 22, 2013

y u no merge? (are memes in PR discussions retro enough to be hip again?)

@arunagw

This comment has been minimized.

Member

arunagw commented Oct 22, 2013

This PR required a rebase/

rafaelfranca added a commit that referenced this pull request Oct 24, 2013

Merge pull request #10471 from andyw8/button_to_params
Add params option for button_to

Conflicts:
	actionpack/CHANGELOG.md

@rafaelfranca rafaelfranca merged commit e6e0579 into rails:master Oct 24, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment