Building Form Based On an Association in Rails - ruby-on-rails

I am very new to rails and having a bit of a problem with a simple page I am putting together for learning and I was hoping someone might have some suggestions on a fix or a better approach.
Essentially, I have the concept of a scratch pad. Each pad has_many tasks and has_many notes. In the pad's show, I want to have two independent forms. One for adding tasks and one for adding notes. The problem is, I build the forms like so:
<%= form_for([#pad, #pad.tasks.build]) do |f| %>
<%= f.text_field :text %> <%= f.submit %>
<% end %>
Now, since I've called .build, when I later call the below code to render the tasks it always renders an additional, empty record in the list.
<ul>
<%= render #pad.tasks %>
</ul>
I thought about using a nested form, but this seems to require me to use PadsController::Update rather than TasksController:Create like I want to.
What would be the appropriate approach in this situation? Being so new to rails (started yesterday), I'm afraid I'm missing something obvious.
Edit: I added the source to a github repository. As I said, this is just a learning project for me. https://github.com/douglinley/scratchpad

If you simply want a new task without it being added to #pad.tasks collection, I believe you can just do #pad.tasks.new instead of #pad.tasks.build (which does add it to the collection).
For more on build vs new, check out
Build vs new in Rails 3

So, this was much easier than I thought. Instead of calling new on the collection, I just created a new object for the form. Example below:
<%= form_for([#pad, Task.new]) do |f| %>
<%= f.text_field :text %> <%= f.submit %>
<% end %>

Related

How should I approach making a custom form involving different model attributes?

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 !

Referencing attributes within a fields_for do block

I have a bit of an odd application where I need to reference the current values of the attributes being loaded by fields_for. The application takes a bit of explanation (it's to cater to a somewhat quirky but necessary jquery plugin) so I'll describe it with a simpler example.
Say I'm loading a list of customers into a form . And (for some reason) I want to head each person's fields with the name currently saved to the database before providing the fields to change it. Something like this:
<%= company.simple_fields_for :customers do |customer|%>
<span>Name previously saved was: <%= customer.name %></span>
<%= customer.input :name %>
<% end %>
Obviously the <%= customer.name %> above isn't a legitimate use for the builder but how might I reference the name attribute of each customer? Again, please pardon the goofy example.
Many thanks in advance.
I believe you should be able to access the customer object via customer.object, in the same way you can access the root object via form.object:
<%= company.simple_fields_for :customers do |customer|%>
<span>Name previously saved was: <%= customer.object.name %></span>
<%= customer.input :name %>
<% end %>

Nested forms using the Cocoon gem in Ruby on Rails 4.2

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.

Rails 3 form validations without a model

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 :-).

Building and submitting an Active Record form by Ajax in Rails best practices?

Alright all you wonderful people out there; In my user interface I am building a form where users can add new records via Ajax.
The users will be able add multiple records from the form (it will be cleared after each post) and I am wondering what conventionally would be the best way to setup the form.
If I want to use form_for helpers I need to have an instance of the model to work from i.e.
def index
#record = Record.new
end
However I am unsure of whether or not this is best from an ajax perspective.
Should I not build an object and just use form_tag and write a method to create the record from my custom form. i.e.
<%= form_tag "/create_record" do %>
<%= text_field_tag :record_name %>
<%= text_area_tag :record_description %>
<%= submit_tag %>
<% end -%>
Then grab the attributes in the controller and build my Record manually.
The second way will work, but I don't know if it is best. Can anyone shed light on how the build process works? Do you have to build a new object for each record you want to submit?
Thanks!
You can use
form_remote_tag
instead of form_tag http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper.html#M001648

Resources