Rails form_for syntax in parent child association - ruby-on-rails

I'm trying to wrap my head around the form_for syntax, when using parent/child associations. Basically my question is, what are the differences in the following, as I've seen most of them being used:
<%= form_for (Child.parent) do |f| %>
<%= form_for (#child.parent) do |f| %>
<%= form_for (#child, #parent) do |f| %>
<%= form_for (#child, #parent.child) do |f| %>
<%= form_for (Child, #parent) do |f| %>
<%= form_for ([Child.parent]) do |f| %>
<%= form_for ([#parent.child]) do |f| %>

I'd say it depends on what you're trying to accomplish. I've used this before:
<%= form_for [#parent, #child] do |f| %>
or even with a namespace:
<%= form_for [:namespace, #child] do |f| %>
If you're looking for nested fields perhaps this would be more what you're looking for:
<%= form_for #parent do |f| %>
...
<%= f.fields_for :children do |builder| %>
<%= render "child_fields", f: builder %>
<% end %>
...
<% end %>

Related

How to insert new line breaks using form_for with collection_check_boxes

I use the following form_for at one point in my code to display a series of topics for users to select from.
<%= form_for #user do |f| %>
<%= f.collection_check_boxes(:topic_ids, Topic.all.sample(50).each, :id, :topic_name) %>
<%= f.submit %>
<% end %>
When it renders on the page, the boxes are all one-after-the-other. How can I separate them so that there is one check box per line?
from reference here
It is possibly to customize the way the elements will be shown by giving a block to the method as sample below from your code above
<%= form_for #user do |f| %>
<%= f.collection_check_boxes :topic_ids, Topic.all.sample(50).each, :id, :topic_name do |b| %>
<%= b.label(class: "check_box") do %>
<%= b.check_box(class: "check_box") %>
<%= b.object.topic_name %></br>
<% end %>
<% end %>
<%= f.submit %>
<% end %>
You may of course render HTML inside the block:
<%= form_for #user do |f| %>
<%= f.collection_check_boxes :topic_ids, Topic.all.sample(50).each, :id, : topic_name do |b| %>
<div class="my-checkbox-wrapper">
<%= b.label(class: "foo") do %>
<%= b.object.topic_name %>
<%= b.check_box(class: "bar") ></br>
<%end%>
<%= f.submit %>
<%end%>
You can have a look at this example
It is very broad question. In short, using CSS.
For example using Bootstrap 4:
<%= form_for #user do |f| %>
<div class="form-check">
<%= f.collection_check_boxes :topic_ids, Topic.all.sample(50).each, :id, :topic_name, class: 'form-check-input' %>
</div>
<%= f.submit %>
<% end %>

Change parameter name FORM_WITH VS FORM_FOR

I am using rails 5 and the form_with helper. My code works when using Form_for, but not Form_with. Is the :as parameter not valid with Form_with?
View
<%= form_with(model: #user_wizard , :as => :user_wizard, url: validate_step_wizard_path, local: true, builder: BsFormBuilder) do |f| %>
Model
def user_wizard_params
params.require(:user_wizard).permit( :name )
end
However, every time I try to submit the form, the parameter submits:
{"utf8"=>"✓",
"authenticity_token"=>"LvRDsdfsdfsd3V0l4NLg14q2JWBdwkDPqUIu2l7SXDiioCtvMwW6Bv3ss/LPSS9+bdxiPIzjg==",
"current_step"=>"step1",
"wizard_user_step1"=>
{"name"=>"Name"}
}
I have been following this.. https://medium.com/#nicolasblanco/developing-a-wizard-or-multi-steps-forms-in-rails-d2f3b7c692ce
However, my code I use Form_with and they use Form_for. I don't know what the difference is.
Rails form_with replaced the as option with scope, which has the same functionality. Your example should look like this:
<%= form_with(model: #wizard , scope: :user_wizard) do |f| %>
Now your inputs should have names like user_wizard[attribute].
form_for using underlying model instance for example here #post as a model
<%= form_for #post do |form| %>
<%= form.text_field :author %>
<%= form.submit “Create” %>
<% end %>
form_tag is form helper but don't have underlying model and you should use text_field_tag instead of text_field
<%= form_tag “/posts” do %>
<%= text_field_tag “post[author]” %>
<%= submit_tag “Create” %>
<% end %>
form_with will combine form_for and form_tag and later these two will be unify with form_with
here implementation of form_for and form_tag using form_with, as you can see below the difference,
if you passing form_with with a model it will work as form_for but if you passing it url then it will work as form_tag
<%= form_with model: #post do |form| %>
<%= form.text_field :author %>
<%= form.submit “Create” %>
<% end %>
<%= form_with url: “/posts” do |form| %>
<%= form.text_field :author %>
<%= form.submit “Create” %>
<% end %>
meanwhile for your problem there is no options as, you can check from this link for more detail options

Ruby Rails is it possible to check before displaying form?

Is it possible to check submission before showing this simple form:
<%= form_for #article, url: {action: "create"}, html: {class: "nifty_form"} do |f| %>
<%= f.text_field :title %>
<%= f.text_area :body, size: "60x12" %>
<%= f.submit "Create" %>
<% end %>
like if its already submitted first time show other html content instead ?
Test the object. If you're using ActiveRecord/Mongoid:
<% if #article.new_record? %>
... display form ...
<% else %>
... do something different ...
<% end %>

pops an error when i am updating the form and include :url

I am new to ruby on rails and got an error while following http://guides.rubyonrails.org/getting_started.html tutorial.the error come at when i am updating the form like this "<%= form_for :post, URL :posts_path do |f| %>"..When i move my cursor to that error it says Unexpected tSYMBEG..i just followed the instructions...The error occur on 2nd line .any help will be appreciable
<h1>New Post</h1>
<%= form_for :post, url :posts_path do |f| %>
<p>
<%= f.label :title %><br />
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %><br />
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
<%= form_for #post do |f| %> will do. Otherwise, try this: <%= form_for :post, url: :posts_path do |f| %>
There is a typo in your code. It causes the issue.
replace
url :posts_path
with
url: posts_path
You can also use.
<%= form_for :post, :url => posts_path do |f| %>
or
<%= form_for #post do |f| %>
All are same..

passing variable into partial

I have the following for and partial setup but i keep on getting an error, that the partial does not recognise the variable |builder|.
Form
<%= simple_form_for #firm do |f| %>
<%= f.fields_for :events do |builder| %>
<%= render 'event_fields', :builder => builder %>
<% end %>
<%= end %>
_events_fields partial
<fieldset>
<%= builder.input :name, :collection => ['Applications Open', 'Applications Close', 'Traineeship Starts'] %>
<%= builder.text_field :start_at, :class => 'datepicker' %>
<%= builder.hidden_field :all_day, :value => true %>
<%= link_to "remove", '#', class: "remove_fields" %>
any idea how i should be padding the variable across? or if in fact this is the correct way of doing it? It would be really helpful is someone could help me understand a little more about how and why you need to do this.
You need to tell it that it is a partial view and pass in a hash to the locals option. Like so:
<%= simple_form_for #firm do |f| %>
<%= f.fields_for :events do |builder| %>
<%= render partial: 'event_fields', locals: {builder: builder} %>
<% end %>
<% end %>
If you're using Ruby 1.8 then:
<%= simple_form_for #firm do |f| %>
<%= f.fields_for :events do |builder| %>
<%= render :partial => 'event_fields', :locals => { :builder => builder } %>
<% end %>
<% end %>
partial_name would be replace with actual partial name or partial name with directory name.
We would provide the data_object object to the partial, available under the local variable data_variable in the partial and is equivalent to:
<%= render partial: "partial_name", locals: { data_variable: data_object } %>

Resources