Sending parameters to controller via button_to

I have

<%= label_tag :“Enter member code” %>
<%= text_field_tag :membercode%>

<%= button_to “Death/Retired Recovery Letter Printing”,
recovery_letter_rpts_path(membercode: membercode) %>

In my controller

def create
membercode = params[:membercode]
exist = Member.find_by_member_code(membercode)
respond_to do |format|
if exist.nil?
# format.html{ redirect_to new_recovery_url, notice: ‘Member not
found.’} # here you just echo the result
puts “******************************************”
puts “This is the member code #{membercode}” # done
puts “******************************************”
redirect_to(recoveries_path)
else
puts “This is the NO member code #{membercode}”
redirect_to(recoveries_path)
end
end
end

all I want to do is send membercode which is accepted via text_field_tag
to
the create action of the controller. And there is no form to contain
these
fields. I want it that way because, there will be more “button_to” which
want to send membercode to different controller’s action.

Please help me.

can’t you do that using template?

2015-07-09 5:57 GMT+06:00 Padmahas Bn [email protected]:

    redirect_to(recoveries_path)

these fields. I want it that way because, there will be more “button_to”
To post to this group, send email to [email protected].
To view this discussion on the web visit

https://groups.google.com/d/msgid/rubyonrails-talk/9c16f865-e756-4e91-83ce-b5d101abdfee%40googlegroups.com

https://groups.google.com/d/msgid/rubyonrails-talk/9c16f865-e756-4e91-83ce-b5d101abdfee%40googlegroups.com?utm_medium=email&utm_source=footer

.
For more options, visit https://groups.google.com/d/optout.


Md. Sadaf N. (@sadaf2605 https://twitter.com/sadaf2605)
www.sadafnoor.com

Can you please elaborate?

Use a form in the view. The form will ‘post’ the data to the create
method in your controller. This is assuming you have the correct routes.

So you are going to have one text field that gathers membercode? And
associated with the membercode are going to be a bunch of bottons, each
that will provide some controller action on membercode. Will the
action
always be def create?
You have your recover_letter_rpts_path(:membercode). I am assuming that
this path as defined in your router.rb and leads to controller action
‘create’. Run rake routes and show us the result, or copy/paste your
route.rb here.
Don’t understand: “And there is no form to contain these fields.”
Plural
fields. These fields?:
<%= label_tag :“Enter member code” %>
<%= text_field_tag :membercode%>
<%= button_to “Death/Retired Recovery Letter Printing”,
recovery_letter_rpts_path(membercode: membercode) %>
Are you saying that this code is not contained in a form…?
Otherwise,
show us the form tag.
Is membercode being returned in your params? Have you been able to
capture that param in your controller?
Pending your answers to the above, a possible solution is this…
If all buttons lead to def create…
Go to your router and modify recover_letter_rpts_path(:membercode).
Add
a new parameter, let’s call it :process. Then for the above button,
you
could rewrite it as…
<%= button_to “Death/Retired Recovery Letter Printing”,
recovery_letter_rpts_path(membercode: membercode, process:
‘verify_existence’) %> I’m not sure about the best syntax for adding a
second parameter
In your controller
def create
membercode = params[:membercode] ## should verify existence
(blank?)
of params[:membercode]
membercode_process = params[:process] ## should verify existence
(blank?) of params[:process]
if membercode_process == ‘verify_existence’
member_exist = Member.find_by_member_code(membercode) ##
pretty
sure that ‘exist’ is a deprecated method in Rails 2. I’d use something
like member_exist
respond_to do |format|
if member_exist.blank?? ## blank? is more thorough, it
tests for object.nil? || object.empty?
## puts is usually done for debugging purposes, do you
want
to provide this information to your user? If so, you may want to put
this
content into a partial – depending on how elaborate your messages
become
– which will be rendered in recoveries_path.
### Or you could store it in flash, which will be
presented
in recoveries_path
puts “******************************************”
puts “This is the member code #{membercode}” # done
puts “******************************************”
redirect_to(recoveries_path)
else
puts “This is the NO member code #{membercode}” ## Same as
above
redirect_to(recoveries_path)
end
elsif membercode_process == ‘some_other_process’
etc…
end
end
end
If all buttons don’t lead to def create… then create a new route path
for
each… But I think that folks here will have better suggestions in
that
case.

On 9 July 2015 at 00:57, Padmahas Bn [email protected] wrote:

    redirect_to(recoveries_path)

fields. I want it that way because, there will be more “button_to” which
want to send membercode to different controller’s action.

Put the field in a form, then you can have multiple submit buttons
within the form.

Colin