I am complete beginner with forms, just trying to tame it but hopeless atm.
I have this following button_to setup:
<% #product.variants.each do |variant| %>
<div><%= variant.asin %></div>
<div><%= variant.price %></div>
<div><%= button_to "Add to cart", shopping_cart_path(:variant_id => variant) %></div>
<% end %>
This works quite fine, the problem with it is, it creates a several buttons for each product.
What I want to achieve is to have only one button and select, as shown below. Which directions do I need to take to pass this hash dynamically.
You could use select_tag and use it nested inside a form_tag. You will have a form containing a select tag inside.
Your code should look similar to this:
<%= form_tag shopping_cart_path do %>
<%= select_tag('variant', options_from_collection_for_select(#product.variants, "id", "asin")) %>
<%= submit_tag "Create" %>
<% end %>
Note: The code above is not tested, but it should give you an idea on how you should approach this.
Related
I have a form for which I am using checkboxes (Not using radio buttons for my purpose). The problem I run into is when I submit a form, I get an error saying params is missing or value is empty:checkup. I am trying to use hidden filed but get the same error. HOw to have an option of sending only one if selected?
def checkup_params
params.require(:checkup).permit(:eye, :heart)
end
my form:
<%= form_for(#checkup) do |f| %>
<%= hidden_field_tag "checkup[eye]", nil %>
<%= check_box_tag :eye, "eye" %>
<%= hidden_field_tag "checkup[heart]", nil %>
<%= check_box_tag :heart, "heart" %>
<%= f.submit %>
<% end %>
I suggest reading this guide http://guides.rubyonrails.org/form_helpers.html#dealing-with-model-objects
Most specifically section 2.2. You really shouldn't be using tag helpers here.
For one of my projects i'm using https://github.com/rsantamaria/papercrop in addition to paperclip to crop pictures before upload.
It is said in the readme to do :
<%= form_for #user do |f| %>
<%= f.cropbox :avatar %>
<%= f.crop_preview :avatar %>
<%= f.submit 'Save' %>
<% end %>
The issue is that my form i need to adapt those function to is a form_tag.
I usually always manage to navigate between the two as needed but since my form is in my application layout ( needs to be there to be on all pages) i don't really use a controller for it but instead form_tag my_post_route_path :
<%= form_tag products_path, multipart: true do %>
name
<%= text_field_tag(:name) %>
</br>description
<%= text_field_tag(:description) %>
</br>image
</br><%= file_field_tag "image" %>
<%= submit_tag("create product") %>
<% end %>
I would like to know two things :
-How do you translate the methods from f.cropbox etc. to Form tag syntax ? i really don't see a way.
I've tried cropbox_tag but it didn't work ( and i didn't make any sens anyway)
-My second question is kind of different but where , when i have a form that need to pop up on all pages ( i'm using a jquery dialog to make it pop up and a button in the navbar ), should it be placed ? is the fact that i've put it in my application_view completely wrong , i've placed it cause the form is only for logged in users ?
Thanks to anyone who tries to help!
I have the following code in my view:
<% form_tag(search_path) %>
<%= search_field_tag("search", nil, placeholder: "Book name") %>
<%= submit_tag ("Search") %>
And the following route:
search_path POST /search(.:format) searches#create
The form appears correctly but when I click submit nothing happens. Server logs show absolutely nothing. Chrome Dev tools shows nothing happens on the client side. I think that the way I am constructing this form is incorrect. I think I need to add something to associates the different tags I have, and without this element all these form elements are all disassociated and fragmented. What am I missing here?
You need to add do to the end of your form_tag. This is how Rails associates the different elements of the form.
<%= form_tag(search_path) do %>
<%= search_field_tag("search", nil, placeholder: "Book name") %>
<%= submit_tag ("Search") %>
<% end %>
"Doing-Ending" the form_tag is a good practice, but I'd say your only problem is the lack of the = sign in the embedded Ruby syntax.
My code is as below :
<% for market_language in #pyr_market_languages %>
<%= f.input :"pyr_crm_call_script[market_language_ids][]", :as => :check_box, input_html:{value: market_language.id}, #callscript.market_languages.include?(market_language) %>
<%= market_language.name %><br/>
<% end %>
I just want to use simple form tag for checkbox like f.input,because i couldn't see the errors for checkbox field even if it is mandatory.
Can any one of you provide the way of using checkbox in a simple form in rails.
What I have now gives me a dropdown menu where I can only select one:
<%= form_for(#submission) do |f| %>
<%= f.collection_select :id, Submission::SUB_ID, :to_s, :to_s %>
<% end %>
where SUB_ID=[1,2,3] in model Submission
I want to implement a checkbox instead of a dropdown menu so that I can select multiple SUB_ID (i.e. 1&2 or 1&3 or 2&3 or 1&2&3). I tried to use this but it does not work:
<%= f.check_box :id, Submission::SUB_ID, :to_s, :to_s %>
Any idea?
Try this:
# view
<%= form_for(#submission) do |f| %>
<%= Submission::SUB_ID.each do |sub_id| %>
<%= f.checkbox 'ids[]', value: sub_id, checked: #submission.id == sub_id %>
<%= sub_id %>
<% end %>
<% end %>
# controller
params[:submission][:ids].each do |checked_sub_id|
# do your logic here
end
you have to iterate over SUB_ID
somehow like this...
<% Submission::SUB_ID.each do |ssid| %>
<%= f.check_box "ids[]", value: ssid %>
<% end %>
or you can use formtastic gem. it has :as=>:check_boxes input fields http://www.ruby-doc.org/gems/docs/n/nuatt-formtastic-0.2.3/Formtastic/Inputs/CheckBoxesInput.html
The core answer is you need to loop over each item in Submission::SUB_ID and make a checkbox for each id. Depending on how your models are set up and what you want to do - you may need to be much more involved in the form building. I hesitate to provide specific examples without know more about how you want the data to come back to the controller
<%= form_for(#submission) do |f| %>
<% Submission::SUB_ID.each do sub_id %>
<%= f.check_box_tag 'submission_ids[]', sub_id %>
<% end %>
<% end %>
Note that that will not default anything to checked and it does not come back as part of the submission parameters.
Usually when I have a similar situation I'm using nested forms to add or remove objects.
If you're using Rails 4, there is a new helper, collection_check_boxes, which helps streamline the building of your check boxes.
<%= f.collection_check_boxes :submission_ids, Submission::SUB_ID, :to_s, :to_s %>
Documentation:
Form builder version - which wraps...
...the general form options helper
If you look at the documentation in the second link, you'll also find how to use the optional block syntax to customise the HTML structure for each check box.