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 %>
Related
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 %>
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.
I'm using collection_select to select an option from a drop down. When I submit the form I want to send multiple params into the params hash. In this case 'team_id' and 'team_name'. team_id is showing up in the params hash just fine. 'team_name' is the value which shows up in the drop down list itself.
view.html.erb
<%= form_for #carpool do |f| %>
<h3>Select Team</h3>
<div class="form-group">
<%= f.collection_select :team_id, #ts_teams ? #ts_teams : [], :id, :name, include_blank: true %>
</div>
<%= f.submit 'Create Carpool', :class => 'button left' %>
<% end %>
params hash
{"utf8"=>"✓", "authenticity_token"=>"bdazhLNLZ0QunrpJT7Gu63ipX76WME+ENSxL/B0XGeFL/GP5nishozmQENe22aelfcnnhnPBr4B35MeRL+kJLQ==", "carpool"=>{"team_id"=>"1923565"}, "commit"=>"Create Carpool", "controller"=>"carpools", "action"=>"create"}
How can I pass team_name into the params hash?
You can get what you want without jumping through flaming hoops by just creating an instance of Team (which you are probably already doing anyhow) and just getting the name directly.
In your controller method:
#team = Team.find(params[:team_id])
#team_name = #team.name
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].
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 %>