Nested route values not populating edit Rails - ruby-on-rails

I have a belongs_to relationship between a map and a row. A map has_many rows.
For some reason, when I try and edit a row, even when it is populated in my database, it is not showing the values when I try to edit this row. Why would this be? Below is the _form.html.erb and the edit.html.erb file.
Edit.html.erb
<%= render 'form', row: #row %>
_form.html.erb
<%= form_for [#map, #map.rows.build], method: :post, url: map_rows_path do |form| %>
<div class="field">
<%= form.label :timeframe %>
<%= form.text_field :timeframe, id: :timeframe %>
</div>
<div class="field">
<%= form.label :standards %>
<%= form.text_field :standards %>
</div>
<div class="field">
<%= form.label :content %>
<%= form.text_field :content %>
</div>
<div class="field">
<%= form.label :skills %>
<%= form.text_field :skills %>
</div>
<div class="field">
<%= form.label :resources %>
<%= form.text_field :resources %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
If I try Row.last in the rails console it confirms that this row is populated with data. I am assuming this has something to do with the way the form is setup however I am not familiar with these relationships. How can this be fixed?

it seems you are passing row to form where <%= render 'form', row: #row %> but in Actual edit form you are using a variable, #map and building a blank row object by saying #map.rows.build, hence the row object will be a new object with out any values.
Based on your code change it to following and see. ( I am assuming you are setting up #map object in your edit action in controller)
<%= form_for [#map, row], method: :post, url: map_rows_path do |form| %>

Related

Placing Rails Form_For Fields in Different Locations

I have a model to be fill with a form, consisting of a title and a body of text. Optionally, I want the ability to submit a link as well as a part of this model. If this is filled out, it is submitted, otherwise ignored. The form would have title and body fields at top of page, for instance.
<%= form_for(#micropost, remote: true) do |f| %>
<div class="field">
<%= f.text_field :title %>
<%= f.text_area :content %>
</div>
<%= f.submit "Post" %>
<% end %>
Now, I want to add the field for the link, but in a different location on the page (so as to indicate that it is optional).
<%= f.text_field :link %>
I tried doing this all in one partial,
<%= form_for(#micropost, remote: true) do |f| %>
<div class="field">
<%= f.text_field :title %>
<%= f.text_area :content %>
<! insert other content here >
<%= f.text_field :link %>
</div>
<%= f.submit "Post" %>
<% end %>
But this would lead to very messy nesting of partials and I'm not sure how to get this to work correctly. Alternatively, I was wondering if it was possible to have one form_for at the top of the page, and another form_for at the bottom of the page that are somehow "synced", so that by pressing the submit button at the top, the value entered in the bottom form_for is collected and submitted as well.
<%= form_for(#micropost, remote: true) do |f| %>
<div class="field">
<%= f.text_field :title %>
<%= f.text_area :content %>
</div>
<%= f.submit "Post" %>
<% end %>
<%= form_for(#micropost, remote: true) do |f| %>
<! somehow sync this with the other form >
<%= f.text_field :link %>
<% end %>
One option is to have an auxiliary text field for link attribute. Copy it's value to hidden variable mapping the link attribute
Assuming you are going to use jQuery,
<%= form_for(#micropost, remote: true) do |f| %>
<%= f.hidden_field :link %>
<div class="field">
<%= f.text_field :title %>
<%= f.text_area :content %>
</div>
<%= f.submit "Post" %>
<% end %>
Somwhere on the same page
<%= text_field_tag 'micropost[link]', '', id: 'aux_link' %>
<script>
$(function(){
$('form').on('submit', function(){
$('#micropost_link').val($('aux_link'));
});
});
<script>
This is just an approach. Adjust code as per your form element ids.
One issue with this approach is it will be difficult to validate if link attribute is compulsory.
Just make the form_for be the root of the view
You can just use the form_for like a container div.
Then put the <input> and <button> as you want like making your normal webpage.
Then, because you want to make some inputs optional, you can do that in rails controller.
def create_or_update
params[:micropost].delete(:link) if params[:micropost][:link].blank?
# continue the task
end
This trick is useful for all optional fields like password
I found here that the canonical way to do this is to use the "form" attribute. Give the form an id, from which the field can refer back to the form, even if the field is placed outside of the form. For instance,
<%= form_for(#micropost, remote: true, html: { id: "micropost_form" }) do |f| %>
<div class="field">
<%= f.text_field :title %>
<%= f.text_area :content %>
</div>
<%= f.submit "Post" %>
<% end %>
We can then use fields_for to place the desired field elsewhere, linking back to the original form.
<%= fields_for #micropost do |f| %>
<%= f.text_field :link, form:"micropost_form" %>
<% end %>

NameError in User#new

I have a model named User and am referencing it in the form. The form is as follows:
<%= form_with(model: user, local: true) do |f| %>
<p>Who is the participant?</p>
<div class="field">
<%= f.label :person %>
<%= f.text_field :input %>
</div>
<p>What gifts would they like?</p>
<div class="field">
<%= f.label :gifts %>
<%= f.text_field :gifttext %>
</div>
<p>Who, if anyone, is their spouse? If they don't have one you can leave
this field blank</p>
<div class="field">
<%= f.label :person %>
<%= f.text_field :input %>
</div>
<p>What gifts would they like? If there is no spouse then you can leave this
field blank.</p>
<div class="field">
<%= f.label :gifts %>
<%= f.text_field :gifttext %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<%= link_to 'Done', user_results_path(#user) %>
<% end %>
Then I get this error:
NameError in User#new Showing C:/Sites/sstwo/app/views/user/_form.html.erb where line #1 raised:
undefined local variable or method `user' for
#<#<Class:0x5590668>:0x41b5ff0> Did you mean? #user Extracted source (around line #1): 1 2 3 4 5 6
I'm sure it's a quick fix so I'm willing to learn. If any more details are needed please let me know.
In the first line, you're using user, should be #user
form_with(model: #user, local: true)
Source: https://m.patrikonrails.com/rails-5-1s-form-with-vs-old-form-helpers-3a5f72a8c78a

Rails 4 simple Quiz web app using Cocoon

My current task is to create simple Quiz web app. App will be structured this way:
- Admin can create, edit, delete and activate quizzes.
- Visitors can take a quiz and submit it. Then receiving quiz results etc.
So far I have created nested form (with Cocoon gem) where admin can create quiz's and add new questions and possible answers, but I have not a single idea how to make that nested form as submit-able quiz for visitors.
My code:
Form:
<%= form_for [:admin, #test] do |f| %>
<div class="field">
<%= f.label :name %>
<br/>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :description %>
<br/>
<%= f.text_field :description %>
</div>
<h3>Questions</h3>
<div id="question">
<%= f.fields_for :questions do |question| %>
<%= render 'admin/question_fields', f: question %>
<% end %>
<div class="links">
<%= link_to_add_association 'add question', f, :questions %>
</div>
</div>
<%= f.submit %>
<% end %>
_question_fields partial:
<div class="nested-fields">
<div class="field">
<%= f.label :description %>
<br/>
<%= f.text_field :description %>
</div>
<%= link_to_remove_association "remove question", f %>
<h3>Answers</h3>
<div id="answers">
<%= f.fields_for :answers do |answer| %>
<%= render 'admin/answer_fields', f: answer %>
<% end %>
<div class="links">
<%= link_to_add_association 'add answer', f, :answers %>
</div>
</div>
</div>
Question: What is the best way to allow dynamic nested form to be used for taking tests by visitors and store submitted data in database?
Thanks in advance.!

Handle a B model inside the view's form of an A model

I have a classic form_for:
<div class="field">
<%= f.label "Modèle" %>
<%= f.text_field :model, required: true %>
</div>
<div class="field">
<%= f.label "Immatriculation" %>
<%= f.text_field :license_plate, required: true %>
</div>
<div class="field">
<%= f.label "Complément" %>
<%= f.text_field :complement, required: true%>
</div>
<div class="field">
<%= f.label "Puissance CV" %>
<%= f.number_field :horse_power, required: true %>
</div>
<div class="field">
<%= f.label "Indemnité KM" %>
<%= f.number_field :km_compensation, required: true%>
</div>
<div class="actions">
<%= f.submit 'Sauvegarder' %>
</div>
I would like to use another model inside this view, that would also update when the user clicks the submit button. I know this has to do with nested forms but I'm a little confused about how to implement it. Here's the variable from the second model that I would like to add:
<% #trip_distances.each do |t| %>
<%= form_for(t) do |e| %>
<div class="field">
<%= e.text_field t.id_contract %>
<%= e.number_field t.length %>
</div>
<% end %>
Obviously this is not correct. I guess I need to use the field_for method?
Firstly, consider using simple_form gem, as it cleans up a bit the code, and is overall more developer-friendly :)
Then in your view you will have something like
<%= simple_form_for sth do |form| %>
<% form.input :attributeUno %>
<% form.input :length %>
...
Then to add to this form inputs for nested element add simple_fields_for
<% #trip_distances.each do |t|
<%= form.simple_fields_for t do |fields| %>
<% fields.input :length %>
...
And don't forget to add
accepts_nested_attributes_for :something in your model :)
PS: You should also consider using slim or haml to make creating views a bit easier, and also clean them up :)

Rails - Use the same form with new/edit using fields_for

I have 5 models,
Person
person_car
car (has many tires)
person_car_tire
tire (belongs to car)
so I have this view
_form.html.erb
<%= form_for(#person) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div>
<% Car.all.each do |c|%>
<div class="field">
<%= f.fields_for(:person_cars) do |ff|%>
Car Name: <%= ff.check_box :car_id %>|<%= c.car_name%>
<% c.tires.each do |b|%>
<div class="field">
<%= ff.fields_for(:person_car_tires) do |fff|%>
Tire: <%#= fff.check_box :tire_id%>|<%= b.tire_name%>
<%end%>
</div>
<%end%>
<%end%>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
And when I save it works perfectly, the problem comes when I want to edit using this form because it duplicates data of each car 4 times in the view. I've been searching and fields for allows extra options so I made:
<%= form_for(#person) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div>
<% #person.cars.each do |c|%>
<div class="field">
<%= f.fields_for(:person_cars, c) do |ff|%>
Actividad: <%= ff.check_box :car_id %> | <%= c.car.name%>
<% c.person_car_tires.each do |t|%>
<div class="field">
<%= ff.fields_for(:person_car_tires, t) do |fff|%>
Tarea: <%#= fff.check_box :tire_id%>|<%#= t.tire.name%>
<%end%>
</div>
<%end%>
<%end%>
</div>
<%end%>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
and it now works, but only show the cars and tires that I've selected on the new action. not all as I wanted (because if I use the first form it duplicates checkboxes in the view).
How can I use the same form for both actions?
Hope someone can help me.
You can use the same _form partial for both new and edit. You just need to pass local variables set to different values to this form. Basically, whatever differs, abstract it away as a parameter (local variable) to this function-like partial.

Resources