I have been using Django for about a year and thought I would try Ruby on Rails to see how it compares. In Django, you can create a ModelForm that automatically generates a html for based on the types of fields in your model. Is there a class that does something similar in Rails? I know scaffolding will generate a erb file for my model, but is there a way to do this?
<%= auto_form_for #person %>
Or this?
<%= form_for #person do |f| %>
<% for each #person.fields do | field| %>
<%= auto_field f, field %>
<%end%>
<% end %>
I apologize for my poor ruby code.
Formtastic implements the Django pony magic you're looking for, and most variants thereof that might actually be useful. In my opinion, most end-user apps don't find "auto forms" and scaffolds to be very useful (since they so often need to be customized in detail), but Formtastic may output enough semantic HTML for you to style in a way that works for your users.
Rails also allows you to write your own custom form builders; you can also extend the ones Formtastic provide.
I found a gem called simple_form that is similar to formtastic. It allows you to to define forms like this:
<%= simple_form_for #user do |f| %>
<%= f.input :username %>
<%= f.input :password %>
<%= f.button :submit %>
<% end %>
I've used it for a few projects, and it's worked out great. It also has integration with twitter bootstrap, if you are using that library.
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'm rather new to Ruby, so I need guidance as how to approach this. I know how to make very simple forms, but I'm thinking of implementing a custom form that can generate a payroll for a business during a specific start and end date. I already have business and payroll models, but I'm not sure how to implement this type of custom form and properly route it to get it be fully functional. Are there any resources or pieces of guidance I can get to help me with this?
please checkout this link :create an application on RoR. Please go to the link above and get started by creating your very own application. I have started with this and have learned a lot from this. This will get you kick started on RoR. But I believe you need more guidance in Ruby also. So I am attaching some more useful links. Please check them out also.
tutorails point - ruby
ruby-lang documentation
ruby guides
Thank you.
I don't know if I really get your issue... If your models Business and Payroll are linked (with :belongs_to and :has_many) you should check the fields_for method. You can nest it in a form_for/form_with to create fields associated to an other model. For instance you can do something like that:
#in your view
<%= form_with model: #business |f| %>
<%= f.label "Something:" %>
<%= f.text_field :something %>
<%= f.fields_for #business.payroll do |ff| %>
<%= ff.label "Something else:" %>
<%= ff.text_field :something_else %>
<% end %>
<%= f.submit %>
<% end %>
#in your controller
if #business.create(business_params)
#...
end
if #business.payroll.create(payroll_params)
#...
end
Hope it will help you !
I'm building a web app using Rails 4.2 and I have a model called Strategy which has a has_many relationship to a Tactic model.
To create a view for Strategy, I need to have many nested Tactics on the form. I've been using the Cocoon gem to develop this view.
So far, I've been able to get the form to work properly for the most part but the issue is that I wanted to add a Type attribute to my Tactic model.
For a given strategy, I want to have 3 tactics of one given type, and 2 models of another.
The issue is that rendering my view, all five models are listed sequentially without any room for putting in my own HTML code around them.
Is there a simple way to separate out which of these are rendered? Right now, my view looks as follows...
<%= form_for #strategy do |f| %>
<div class="strategy">
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div class="tactics">
<%= f.fields_for :tactics do |i| %>
<%= render 'tactics_fields', f: i %>
<% end %>
</div>
<div class="buttons">
<%= f.button "Save Strategy", class: 'button' %>
</div>
<% end %>
If anyone can offer any advice, it would be greatly appreciated.
Are you wanting the form to be rendered so that each Strategy has exactly 5 Tactics listed below it, of which 3 are of one type and 2 are of another?
If so, you can do this in your controller:
5.times do
tactic = #strategy.tactics.build
tactic.tactic_type = 'tactic_type'
end
Also, note that using an attribute called type is not a good idea. Use something like tactic_type instead.
Here's a question I've been meaning to ask for a long time, but have just accepted it as "Rails magic" up to this point. As the title states, why does the Rails form helper look like a do loop? If you check the official Rails documentation, it doesn't seem to explain this, it just jumps right in by giving the below as a basic example:
<%= form_tag do %>
Form contents
<% end %>
So what's going on here exactly? Why does the form appear to be creating a loop as opposed to input forms in other languages that do not have said loop.
<%= form_for #person, url: {action: "create"} do |person_form| %>
<%= person_form.text_field :name %>
<%= fields_for #person.contact_detail do |contact_details_form| %>
<%= contact_details_form.text_field :phone_number %>
<% end %>
<% end %>
That's not "Rails magic", that's completely vanilla Ruby syntax. do/end denote a block in Ruby, not a loop.
Rails' documentation doesn't explain this, because Rails' documentation assumes a knowledge of Ruby. Rails does not introduce any new syntax (nor can it), it's just a framework, written in 100% plain old Ruby.
Because it takes a block argument.
It doesn't have anything to do with looping, e.g., File#open also takes a block.
#open yields the opened file. The form_for helper yields the helper.
I've created a contact form using form_tag and other simple non-model-related rails form helpers. The form just submits an email to the site administrator. Anyway, I'd like to validate the fields on the form, but I'm having trouble finding a Rails-friendly way of doing this.
I found this answer to what seems to be the same question, however the accepted answer didn't seem to present a clear solution for Rails 3 (it seemed more oriented toward Rails 2). Could someone provide a concise example of how to easily do client-side validation of a form not related to a model in Rails 3? Extra points for a strategy that utilizes the default Rails validators.
And let's pretend my form looks something like this:
<%= form_tag "/contact" do %>
<%= text_field_tag :name %>
<%= text_field_tag :email %>
<%= text_area_tag :message %>
<%= submit_tag "Submit" %>
<% end %>
Give active_attr a try. Here's a short audio description.
I personally haven't tried it yet, but was thinking I will in the near future.
Also, in Rails 4, you'll kind of have the same functionality of the gem included: http://blog.plataformatec.com.br/2012/03/barebone-models-to-use-with-actionpack-in-rails-4-0/. And audio description :-).