Say I have a Rails form like the following
<% form_for #model do |f| %>
<%= f.label :column %>
<%= f.check_box :column %>
<% end %>
Is there a way I can get the html id that will be generated for the 'column' check box? What would be great is if there was a way to add in
<%= f.observe_field :column, options %>
Anyone know how to add this to FormBuilder?
For generating an element's id programatically, see this. As it seems, there is no easy way out.
You could add this to FormBuilder by wrapping up certain field tags. See for example this.
Related
I have a form in Rails which uses fields_for to accept nested attributes:
<%= form_with(model: #combat_tracker, url: form_url) do |f| %>
…
<%= f.fields_for :zones do |zone| %>
<div class="zone-field">
<%= zone.text_field :name %>
<%= zone.check_box :_destroy %>
<%= zone.label :_destroy, "Remove zone" %>
</div>
<% end %>
…
<% end %>
Currently this gives me input fields for any existing zones on #combat_tracker. I want to add a button that will dynamically add a new zone-fields div for a new zone to be added when the form is submitted.
I’m using Rails 7 and assume the solution will involve the use of Turbo or possibly Stimulus, but can’t quite figure out the best way to do this. Thanks.
I don't think you need Turbo or Stimulus. Take a look at cocoon gem, it should do exactly what you're looking for.
Explaining all process here is quite complex for me, but try to follow this guide if the gem's one is too long.
I want a form like this, because I just want to use :pass_value to pass value to other properties in controller. But it does not work. Could you please provide a right way to do this?
<% form_for #person do |f| %>
<%= f.text_field :name %>
<%= text_tag :pass_value %>
<%= f.submit %>
<% end %>
No, you can't use a form tag inside a form. But you can use one of:
a) fields_for
if this is a nested model, you can use it something like:
fields_for :widget do |g|
g.text_field :widget_name
end
doc for fields_for: http://apidock.com/rails/ActionView/Helpers/FormHelper/fields_for
b) text_field_tag
if it's just an independent field
<%= text_field_tag :some_value %>
doc for text_field_tag:
http://apidock.com/rails/ActionView/Helpers/FormTagHelper/text_field_tag
you cannnot use form_tag inside another form. But if you want to pass the value of :pass_value actually not as an attribute, try making use of virtual attribute. You can make use of :attr_accessor method.
you can use raw html tags, such as
input type="hidden" name="person[pass_value]" value=#person.pass_value
it's just html form after all
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.
In rails is there any simple way to implement select or create from view.
Eg:
Product has_many(or has_one) Tags.
While creating new Product I can select existing tags or create new one.
This can be done by using JavaScript and other ways are there.. But all will take more time and effort.
Please share if you know other simple way...
Edit:
Something like this.
But imagine you have 100 tags or more ! your page will look bad with 100 checkbox or more..., one elegant way to do this is by using a jQuery plugin called jQuery Tokeninput i use it in my project and it's very helpful for what do you want, you can find the plugin Here
This is a screencast on how to use it : Token fields
and this is the revised version : Token Fields (revised)
check also this blog post about the same plugin if you want too How to create a token input field where the user can also add new items
cheer
Yep.
You are after nested forms. Try, https://github.com/ryanb/nested_form
For example,
<% form_for #product do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<% f.fields_for :tags do |tag| %>
<p>
<%= tag.label :content, "Tag Name" %><br />
<%= tag.check_box :content %>
</p>
<%= tag.link_to_remove "Remove this tag" %>
<% end %>
<%= tag.link_to_add "Add new tag" %>
<p><%= f.submit "Submit" %></p>
<% end %>
Setup the controller and model as given in the documentation and try the above code in the view. This railscast will help you a lot in figuring nested forms http://railscasts.com/episodes/197-nested-model-form-part-2
This isn't working as I'd like.
I have an entry form for a new job, which is made up of a number of steps. By default there are 4 new and unsaved steps populated per job.
Using the code from the view below, I am able to attach the jQuery UI datepicker control to the text fields and they appear against the correct input field when i click them.
However, when I select a date from the picker it is only ever going into the field representing the start date of the first step.
<%= form_for #job, :url => jobs_path do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<% #job.steps.each do |step| %>
<%= fields_for "job[step_attributes][]", step do |s| %>
<%= s.label :name %>
<%= s.text_field :name %>
<%= s.label :start_date %>
<%= s.text_field :start_date, :class => :datepicker %>
<%= end %>
<% end %>
<script type="text/javascript">
$(function() {
$('.datepicker').datepicker({ dateFormat: "dd/mm/yy"});
});
</script>
I know ultimately this has to do with the generated id attribute for the input elements being the same, was wondering, has anybody successfully overcome this issue?
This fiddle: http://jsfiddle.net/twilson/u9m9L/ demonstrates my problem.
First, invoke fields_for on your form builder instead:
<%= f.fields_for :steps do |s| %>
Second, you don't need to iterate through #job.steps if you specify the association name to fields_for. There's an example on how to use it with one-to-many assoiciations here.
If you still have this problem, paste your generated HTML, it would easier to find the cause.
EDIT
OK so the problem is because Rails form builder does not generate child indexes to give unique IDs to nested fieldsets. This most likely happened because the associations are built but not yet saved to the DB. One way I see is to assign child indexes manually, like so:
<% #job.steps.each_with_index do |step,i| %>
<%= f.fields_for :steps, step, :child_index => i do |s| %>
See if that helps.
This is a somewhat contrived scenario given that you have only new objects. To be able to dynamically add/remove nested items, this would be a bit trickier. You can see how this can be dealt with in the nested model Railscasts.