I am setting my form like this:<%= hidden_field :room_name, #room.name %>
in my View I am getting the parameter as
room_name: !ruby/object:ActionController::Parameters
parameters: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
Standard Apartment: ''
I just want to get the Standard Apartment Value
If you declared a form with object like
<%= form_for(#room, html: {role: "form"}) do |f| %>
here f is a form object then input field with value will look like this
<%= f.hidden_field :room_name, value: #room.name %>
the output HTML is something like this
<input value="Room Name" type="hidden" name="room[room_name]" id="room_room_name">
If your form declared without object then it will be
<%= hidden_field_tag :room_name, value: #room.name %>
Hope it will help other SO user in the future.
I figured it out changing my form to this
<%= hidden_field_tag "room_name",#room.name %>
Related
I have a form created using form_with. What I need is to retain the values submitted using that form after page reload. I am able to save the value of text_field but not the value of check_box. What should I change in my code so that I can achieve the same?
html.erb
<%= form_with url: search_path,
id: :search_by_filter,
method: :get, local: true do |f| %>
<div>
<p><strong>Search by Name</strong></p>
<%= f.label 'Name' %>
<%= f.text_field :name, value: params[:name] %>
</div>
<br>
<div>
<%= label_tag do %>
<%= f.check_box :only_students, checked: params[:only_students] %>
Show only students
<% end %>
</div>
<br/>
<div class="submit_button">
<%= f.submit :Search %>
</div>
<% end %>
controller.rb
def get_desired_people(params)
people = Person.includes(:country, :state, :university).order(id: :desc)
people = people.where(is_student: params[:only_students]) if params[:only_students]
people = people.where(name: params[:name]) if params[:name].present?
people
end
Here I am able to retain the value of params[:name] but not the value of params[:only_students]. It always remains unchecked after form submission. How can I retain the checked and unchecked value?
f.check_box check_box_tag is expecting checked to by boolean value, and every param is a string (string is always evaluated to true if exists) so you should do:
checked: params[:only_students].present?
you don't have to worry about a value of param, as unchecked params are not send while posting.
EDIT:
above works for check_box_tag.
f.check_box is tricky, you should carefully read description: https://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-check_box
The behaviour you described seems pretty correct, you can deal with it or switch to check_box_tag as a better option when not updating model attributes
All the solutions above did not work for me. Try this:
<%= check_box_tag :only_students, true, params[:only_students] %>
I need to use an object in an array as the object argument for collection_select in a form using a form_tag helper but my params hash isn't formatting correctly.
Example:
<%= form_tag(picks_path, method: :post) do %>
<% #awards.each do |award| %>
<%= hidden_field_tag "picks[][user_id]", 1 %>
<%= hidden_field_tag "picks[][game_id]", 1 %>
<%= hidden_field_tag "picks[][award_id]", award.id %>
<%= label_tag "picks[][:first_pick]", "First Pick" %>
<%= collection_select("picks[][:first_pick]",
:first_pick, award.nominations, :id, :nominee) %>
<% end %>
<%= submit_tag 'Submit' %>
<% end %>
Results in this params hash with extra nesting...
"picks"=>[{"user_id"=>"1", "game_id"=>"1", "award_id"=>"1", ":first_pick"=>{"first_pick"=>"1"}}, {"user_id"=>"1", "game_id"=>"1", "award_id"=>"2", ":first_pick"=>{"first_pick"=>"3"}}]
When what I'd really like is this...
"picks"=>[{"user_id"=>"1", "game_id"=>"1", "award_id"=>"1", "first_pick"=>"1"}, {"user_id"=>"1", "game_id"=>"1", "award_id"=>"2", "first_pick"=>"3"}]
I tried using this...
<%= collection_select("picks[]", :first_pick, award.nominations, :id, :nominee) %>
But I got this exception...
object[] naming but object param and #object var don't exist or don't
respond to to_param: nil
Am I missing some special syntax here or is this not the conventional way of achieving multiple entries from a form with one submit button?
try each.inject, something like this:
<% #awards.each.inject([]) do |collection_select, award| %>
collection_select << { first_pick: award[:first_pick], nominations: award[:nominations], id: award[:id], nominee: award[:nominee]}
If your award main object don't hold some values, u can hard coded it in each hash value i.e: id: 1, nominee: 'anything'
hope it helps.
Right now I'm using a hidden field tag to get the plan_id. But when I use I binding.pry it's nested in the accounts hash. In order to get to the plan_id I have to do this params[:accounts][:plan_id] when I simply want to do this params[:plan_id]. How can I achieve this? Here is my code.
<label>
<%= f.hidden_field :plan_id, value: 1 %>
<%= f.submit "Select", class: "button-big reverse-blue" %>
</label>
Here is my params look like
{ "account"=>{"name"=>"something", "plan_id"=>"1"}, "stripe_card_token"=>"", "card_number"=>"", "card_holder"=>"", "controller"=>"accounts", "action"=>"create"}
I want them to look like this
{"account"=>{"name"=>"something"}, "plan_id"=>"1", "stripe_card_token"=>"", "card_number"=>"", "card_holder"=>"", "controller"=>"accounts", "action"=>"create"}
Again. I'd like to access the plan Id by simply typing this params[:plan_id]
You could use hidden_field_tag
<%= hidden_field_tag :plan_id, 1 %>
I have a form for some model, inside this form I have some text_fields and hidden_fields
that i need to use in the controller but that are not from the model.
this is a simplified version of it
<%= from_for #user do |f| %>
<%= f.text_field :name %>
<%= hidden_field :photo, value: 'blabla' %>
<%= text_field :type %>
<% f.submit %>
Lets say that the :photo and :type parameters are not in the model user but i need them to decide how to create the user.
they are going in the params hash, but all messed up. How do I access their value?
Thank you
hidden_field_tag "photo", "photo_value"
=> <input id="photo" name="photo" type="hidden" value="photo_value" />
Then in your controller:
#hidden_photo = params[:photo]
Whenever you are working with a form and want a value not associated with a model or object, then use the helpers ending in "*_tag"
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 %>