form_for params - ruby-on-rails

I need to submit a form to an external URL, so I have this:
form_for(#task, :url => "https://www.external.com/Submit") do |f|)
<%= f.hidden_field :assignmentId, :value => #assignment %>
<%= link_to image_tag(#imagelocation) %>
....
I am using form_for, because I need to access to my controller variables.
The external server looks for a param, assignmentId. When the form is submitted, the param is actually available as
params[:task][:assignmentId]
which fails to pass validation on the external server.
How do I resolve this? How do I access variables from my controller and pass 'naked' params to the external server?
[edit] Here's what the submit params looks like
utf8=%E2%9C%93&_method=put&task%5BassignmentId%5D=2LVQ39Z0B6UWI8NXYWJTYRKGQXIMXN&task%5Boutput%5D=carpet&commit=Post
I want it to not have the task referenced.

Use hidden_field_tag instead of f.hidden_field. Btw, if you want this field to store assignment's id, you should use #assignment.id, not just #assignment.
<%= hidden_field_tag :assignmentId, #assignment.id %>

Related

How to build a rails form with a single parameter to flat hash

I have a rails-simpleform that looks like this:
=simple_form_for :search, url: search_path(:search), method: :get do |f|
= f.input :q
= f.submit 'Search'
When I want to retrieve the input I have to access it with:
params[:search][:q]
Is it somehow possible to build the form in a way, that the parameters aren't nested under params[:search]? So that I can access them directly through params[:q]? I understand that this only makes sense if there is only a single input for a form.
CONTEXT:
What I'm trying to do is to allow the user to either search through the search-form described above or through a short-url(myapp.com/search-text).
form_for (or simple_form_for) creates a form associated to a model object. The params passed to the controller add the model name as a key in the hash (params[:search][... fields_in_the_form ...]).
form_tag simply creates a form and it is not associated to any model. It does not add any key to the params hash except for the fields in the form. So this is a usual method to create search forms.
<%= form_tag(search_path, method: :get) do %>
<%= text_field_tag :term, params[:search] %>
<%= submit_tag 'Search', name: nil %>
<% end %>
Then you can use params[:search] in the controller.
Finally, to use myapp.com/search-text you should use route globing (http://guides.rubyonrails.org/routing.html#route-globbing-and-wildcard-segments) defining your route as:
get '*search', to: 'static_pages#search'
The StaticPagesController#search method will receive params[:search] with anything after myapp.com/. Note: this should be your last route, as it matches anything.

How to pass param object in form_tag in Rails

How to pass object using form_tag.
Ease with form_for, i only use
<%= form_for #user, url: signup_path do |f| %>. Every element htmt have name is name=user[email] or name=user[password] or name=user[select]. So in form_tag, i only do that with input type text, but can do that with Select, Radio, Password or something like that. So, How do i can pass 1 object with param like form_for to controller that use form_tag.
You need to just set name as the first parameter. Like this:
<%= text_field_tag 'user[email]' %>

Send params as part of url in rails

I have the following form
<%= form_for :key, url: unblock_keys_path, :html => {:method => :get} do |f| %>
<%= f.text_field :value %>
<%= button_to "Unblock", method: :get %>
<% end %>
Which works fine when I enter key values in the text box. But i want the user to be able to access the endpoint directly from the url as well.
Right now the params this request generates are:
{"utf8"=>"✓", "key"=>{"value"=>"x0vdPYWb9nAfyNjFS-UAGQ"},
"authenticity_token"=>"+Oi45DHYpPAiNbKrw5kNjWMVrQgCyLsBkhVb7huB0dr+xm/oKXxzTShajVUYEWxl9qlFLfjWsP4C4JM30DTGoA==",
"controller"=>"keys", "action"=>"unblock"}
url: http://localhost:3000/keys/unblock?utf8=✓&key%5Bvalue%5D=x0vdPYWb9nAfyNjFS-UAGQ&authenticity_token=%2BOi45DHYpPAiNbKrw5kNjWMVrQgCyLsBkhVb7huB0dr%2Bxm%2FoKXxzTShajVUYEWxl9qlFLfjWsP4C4JM30DTGoA%3D%3D
I want to be able to access localhost:3000/keys/unblock?<key_value>
What changes do I need to make in my request and routes?
This localhost:3000/keys/unblock?<key_value> is not a valid url*. The ? denotes the start of the params, and then everything after that needs to have the form "name=value", joined with "&".
Do you mean localhost:3000/keys/unblock/<key_value>?
If so then add a route like
get '/keys/unblock/:value', to: 'keys#unblock'
This will send eg /keys/unblock/foo to your unblock action, with params[:value] = "foo"
*note - technically it's not illegal, but it's poorly formed and almost certainly not what you want to do.

Rails pass parameter with submit

I have a Rails 3.2.12 app where I would like to pass a parameter via a form submit button.
The param is :invoice_id. In the form the value is in #invoice.
I tried this:
<%= f.submit "Submit", :class => "btn btn-success", params: {:invoice_id => #invoice} %>
In the controller, I would access it like this:
def addinvtimes
#invoice = params[:invoice_id]
But, it ends up being nil.
Thanks for the help!
UPDATE1
That's not how HTML forms work. If there's data that you want to get submitted along with the rest of your form's data but not be viewable or editable by the user, stuff it into a hidden field, like so:
<%= form_for #order do |f| %>
<%= f.text_field :customer_name %>
<%= f.hidden_field :invoice_id, value: #invoice.id %>
<% end %>
When you do this, the invoice_id will be submitted alongside the rest of the form's data, so in this case you would access it as params[:order][:invoice_id].

Difference between form_for , form_tag?

What is the difference between form_for and form_tag? Is anything different for form_remote_for and form_remote_tag?
You would use form_for for a specific model,
<% form_for #person do |f| %> # you can use f here
First name: <%= f.text_field :first_name %>
Last name : <%= f.text_field :last_name %>
<% end %>
Form_tag create basic form,
<%= form_tag '/person' do -%>
<%= text_field_tag "person", "first_name" %>
<% end -%>
form_for prefers, as its first arg, an activerecord object; it allows to easily make a create or edit form (to use it in a "new" view you should create an empty instance in controller, like:
def new
#foo = Foo.new
end
It also passes a form variable to the block, so that you don't have to repeat the model name within the form itself. it's the preferred way to write a model related form.
form_tag just creates a form tag (and of course silently prepare an antiforgery hidden field, like form_for); it's best used for non-model forms (I actually only use it for simple search forms or the like).
Similarly, form_remote_for and form_remote_tag are suited for model related forms and not model related forms respectively but, instead of ending in a standard http method (GET, POST...), they call an ajax method.
All this and far more are available for you to enjoy in the FormHelper and PrototypeHelper reference pages.
EDIT 2012-07-13
Prototype has been removed from rails long ago, and remote forms have completely changed. Please refer to the first link, with reguard to the :remote option of both form_for and form_tag.
These should be similar:
<% form_for #person do |f| %>
<%= f.text_field :name %>
<% end %>
and:
<%= form_tag '/person' do %>
<%= text_field_tag "person[name]" %>
<% end %>
If you want to submit the same params to the controller, you would have to define this explicitly.

Resources