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].
Related
In PHP, to retrieve the value of a link in PHP, all I have to do is use $_GET['value']
Now in Ruby on Rails, how do I do that? As an example, let say I have this link I want to retrieve its id and use it in a form.
This is the link
<%= link_to "Message", new_message_path %># This link will allow the viewer to message the profile owner
and this is the message script
<%= form_for(#message) do |f| %>
<%= render 'shared/error_messages', object: #message %>
<%= f.label :content %>
<%= f.hidden_field :receiver, value:# The ID should be retrieved from the user id of the previous page %>
<%= f.text_area :content, size:"20x15" %>
<%= f.submit "Send message", class: "btn btn-primary" %>
<% end %>
I suspect you might be talking about query params (such as receiver_id on /new?receiver_id=1234). You can do this on Rails as well by passing additional parameters to the helper function used to route, such as:
<%= link_to "Message", new_message_path(receiver_id: #receiver.id) %>
Which would yield something like /messages/new?receiver_id=1 or whatever.
Then you can use the params variable in your controllers in order to access the query params, such as params[:receiver_id], which would yield 1 in this case.
i have a form in my index-view where i create multiple checkboxes. One checkbox for every entry. This looks like this:
index.html.erb
<%= form_for :user, url: usersupdate_path() do |f| %>
<%= render #users %>
<%= f.submit 'test', :class => 'btn btn-primary' %>
<% end %>
_user.html.erb
<%= check_box_tag "checked[#{user.id}]","#{user.id}",true %>
Description:
With the form i want to allow the admin to uncheck users - this users i want to send to the controller and update their attributes.
There are only 2 problems:
1) I have to refresh the site until i can send the form to the controller - i don't know why
2) When i print the array it looks like this:
{"1"=>"1", "2"=>"2", "4"=>"4"}
User 3 was unchecked by me.
What i want is something like this:
{"1"=>"true", "2"=>"true", "3"=>"false", "4"=>"true"}
But how can i send the checked value of the checkbox to the controller?
In my controller i do only this at the moment:
def update
flash[:success] = params[:checked]
redirect_to root_path
end
Thanks
The browser does not serialize an unchecked checkbox when sending form data, so if it is not checked, it never gets sent.
You can generally fix this two ways. Make your action smart enough to see "missing" values as "unchecked", or add a hidden field before each checkbox:
<%= hidden_field_tag "checked[#{user.id}]", "false" %>
<%= check_box_tag "checked[#{user.id}]","#{user.id}", true %>
As for the true-values, the second parameter to check_box_tag is the value you want the checkbox to have, so you can change it to this:
<%= hidden_field_tag "checked[#{user.id}]", "false" %>
<%= check_box_tag "checked[#{user.id}]","true", true %>
And it should do what you want.
Note that if you use FormBuilders they handle this nuance for you.
hy, i'm trying to update a single field in a user resouce. The field to update is 'locale', it is a string type.
i have triend with a form and it works:
<%= form_for current_user do |f| %>
<%= f.select :locale, [['En', 'en'], ['It', 'it']] %>
<%= f.submit %>
<% end %>
now i'm trying to update the same field using a link or a button:
<%= current_user.locale = 'fr' %>
<%= button_to "update" ,current_user,method: :patch %>
<%= button_to "update" ,user_path(current_user),method: :patch %>
but none of this work.
the problem is the request, infact the web server doesn't recive the current_user parameters:
{"_method"=>"patch",
"authenticity_token"=>"7X6QdD4DGxsXaETT86/8Ut4xyuOICaxirs4IjmZl7jY=",
"locale"=>"en",
"id"=>"1"}
There is no current_users parameters.
i have tried with the link_to but the problem is the same:
<%= link_to "update", current_user ,method: :patch %>
I have no idea. Can you help me?
The fact that you don't get the parameter sent back to server is the expected one since you don't use a form.
In order to pass the parameter back you have to:
use a form (as you did) or
pass the parameter in the url of the button: button_to 'update', user_path(param_name: 'param_value')
In the second case, you will have to search for the appropriate parameter in your action, ex params[:param_name]
I have a url like this http://example.com/myController?key=12345
In my controller myController, i can access the key value with params[:key], but when i submit a form, i want to get that same key in the create method, but the params[:key] is null.
How can i access params[:key] in my post action create?
You might indeed add a hidden field.
If you find yourself adding many of those because you need to have the value available in more and more actions, consider setting it within the session as (e.g.) session[:current_key]
You can pass the info along by a hidden_field_tag. So your code would be like:
<%= form_for #model do |f| %>
<%= hidden_field_tag 'key', params[:key]
# other form code
<%= f.submit %>
<% end %>
You can add a hidden field to your form and set its value to params[:key]. Then it will be available in the params you get from the form.
If you're using Simple Form:
<%= simple_form_for #model do |f| %>
...
<%= f.input :field_name, :as => :hidden, :input_html => { :value => params[:key] } %>
<%= f.button :submit %>
<% end %>
Add a new hidden field to pass via params, when you submit the form your action is changed or called again params hash is recreated; what you can do is pass the value via hidden field in the form as follow,
<%= simple_form_for #model do |f| %>
# your remaining form fields
<%= f.input :field_name, as: :hidden, :input_html: { value: params[:key] } %>
<%= f.button :submit %>
<% end %>
I got a list page and I filter items via links with get params (I can choose many links so query would be like "?param1=value1¶m2=value2"). But also I have to filter it by text field, so I made a form:
<form>
<%= text_field_tag :zip, params[:zip] %>
<%= submit_tag 'OK', :name => nil %>
</form>
But when I submit it, text field param replaces existing query params. So, how to make text field value add to query, not to replace it?
Since I was just dealing with this problem in Rails 4 I thought I'd share my solution.
My page gets loaded with a sport_id parameter, and when the user specifies a sort-order I wanted it to submit a GET request for page.url/event?sport_id=1&sortby=viewers but it wouldn't preserve the sport_id parameter until I added a hidden field tag in the form like so:
<%= hidden_field_tag :sport_id, params[:sport_id] %>
This solution does submit an empty sport_id parameter if that parameter was not in the original request, but that is easily prevented by encapsulating the hidden field in an <% if params[:sport_id].present? %> condition.
Use hidden_field_tag.
Inside of your form, just set hidden_field_tags for the existing GET params, like so:
<% request.query_parameters.collect do |key, value| %>
<%= hidden_field_tag key, value %>
<% end %>
This will ensure that your existing params persist.
Rails 3?
<%= form_tag your_path(params.except(:controller, :action)), :method => :get do %>
<%= text_field_tag :zip, params[:zip] %>
<%= submit_tag 'OK', :name => nil %>
<% end %>