I have a nested form with a parent object (full_application) and a collection of child objects (fullapplication_districts) via a has_many association. I am attempting to allow for deletion of individual child objects on the form (via javascript) but to do so I need to be able to grab the id of each child object in the view for passing to the controller. fields_for creates a hidden input field for the id but I can't seem to figure out how to grab the id from it. In the example below the record is the 13th in the list of rendered child objects.
<input type="hidden" value="538" name="full_application[fullapplication_districts_attributes][12][id]" id="full_application_fullapplication_districts_attributes_12_id">
Here's the form setup in the view:
<%= form_for(#full_application, url: full_applications_edit_path, method: :put) do |f| %>
<%= f.fields_for :fullapplication_districts do |fad| %>
<%= fad.collection_select :district_id, District.all, :id, :name, {include_blank: true}, {class: 'form-control'} %>
<%= fad.number_field :percent_one, class: 'form-control', step: :any %>
<%= fad.number_field :percent_two, class: 'form-control', step: :any %>
<%= fad.number_field :percent_three, class: 'form-control', step: :any %>
<%= link_to full_applications_districts_path(???), method: :delete, remote: true, data: { confirm: "Are you sure you want to delete this record?" } do %>
<i class="fa fa-trash"></i>
<% end %>
<% end %>
<% end %>
You can use: fad.object or fad.object.id. This will return to fullapplication_district instance.
<%= link_to full_applications_districts_path(fad.object), method: :delete, remote: true, data: { confirm: "Are you sure you want to delete this record?" } do %>
<i class="fa fa-trash"></i>
<% end %>
Related
So I'm a beginner in Rails and I'm trying to reload a partial when I create a Comment.
I am creating a sort of forum where I have Posts and Comments on Posts.
Here is the situation :
/views/posts/show.html.erb
<% #posts.each do |post| %>
<%= post.title.capitalize %>
<%= post.content %>
<%= render 'comments/comment', post: post, comments: post.comments %>
<%= form_tag(category_forum_post_comments_path(#category, #forum, post), :method => :post, class: 'form newtopic') do %>
<%= text_field_tag :content, nil, placeholder: 'Commenter', class: 'form-control' %>
<%= submit_tag "Envoyer", class: "btn btn-primary" %>
<% end %>
<% end %>
<%= form_tag(category_forum_posts_path(#category, #forum), :method => :post, class: 'form newtopic') do %>
<%= text_field_tag :title, nil, placeholder: 'Titre de votre Post', class: 'form-control' %>
<%= text_area_tag :content, nil, placeholder: 'Description', id: 'desc', class: 'form-control' %>
<%= submit_tag "Envoyer", class: "btn btn-primary" %>
<% end %>
and then in /views/comments/_comment.html.erb
<% comments.each do |comment|%>
<%= comment.content %>
<%= link_to 'Supprimer', category_forum_post_comment_path(#category, #forum, post, comment),
method: :delete, class: "btn-danger btn-sm", data: { confirm: 'Etes-vous sûr?' } %>
<% end %>
I would like to reload my comment partial on post.
I tried to understand so many posts on stackoverflow and tutorial that I could find online and still have no idea how to do it.
If someone would be kind enough to help me get through this it would be great.
What's the action that you want to trigger that reload? you are showing forms, links, it's not too clear.
Basically, you need to use remote: true on the form/link, then add a view for that action with .js.erb extension, and, on that view, use javascript to change the content of an specific element.
Something like:
1st, you need to give some unique way to reach the element you want with javacsript:
<% #posts.each do |post| %>
<div id="post_<%= post.id -%>">
<%= post.title.capitalize %>
<%= post.content %>
<div class='comments'>
<%= render 'comments/comment', post: post, comments: post.comments %>
</div>
<%= form_tag(category_forum_post_comments_path(#category, #forum, post), :method => :post, class: 'form newtopic') do %>
<%= text_field_tag :content, nil, placeholder: 'Commenter', class: 'form-control' %>
<%= submit_tag "Envoyer", class: "btn btn-primary" %>
<% end %>
</div>
<% end %>
<%= form_tag(category_forum_posts_path(#category, #forum), :method => :post, class: 'form newtopic') do %>
<%= text_field_tag :title, nil, placeholder: 'Titre de votre Post', class: 'form-control' %>
<%= text_area_tag :content, nil, placeholder: 'Description', id: 'desc', class: 'form-control' %>
<%= submit_tag "Envoyer", class: "btn btn-primary" %>
<% end %>
Note the div with id=post_x and the div with class comment
Now you can locate it using javacsript:
#your_view.js.erb
post = document.getElementById('post_<%= #post.id -%>');
comments = post.querySelector('comments');
And finally, also on your view, render the partial and replace comments' innerHTML
comments.innerHTML = '<%= j(render partial: "comments/comment", post: #post, comments: #post.comments) -%>';
Just a suggestion, use "comments" (in plural) for the name of the partial, since you are rendering all the comments and not just one.
I have a rails form in which I would like to add a select dropdown. for each dropdown, the user can select a single value, but at the end I want to parse through each value entered in the different select boxes as a part of an array. How can I do this:
My code looks like this:
<%= simple_form_for :or,
url: path,
method: :post,
remote: true do |f| %>
<%= f.fields_for "r_ids[]" do |ff| %>
<% count.times do |c| %>
<%= select(:or, :r_id, #r.collect { |r| [r.name, r.id] }) %>
<br/>
<% end %>
<% end %>
<%= f.button :submit,
class: 'btn btn-success btn-sm btn-update',
data: { disable_with: 'Submitting...'} %>
<% end %>
As you can see, I am using a loop to display the dropdown multiple times. for each of the dropdown, I would like to put the value in an array that allows me to obtain the values entered in each of the dropdown. How can I do this using either rails form helpers or simple form?
I am using the Best In Place gem.
This is an example of my object:
<h5><%= best_in_place node, :name, as: :input, activator: "#edit-node-title-#{node.id}" %> <%= link_to "<i class='fa fa-pencil'></i>".html_safe, "#", id: "edit-node-title-#{node.id}" %></h5>
But what I want to do is have the node.name attribute be shown as a regular link.
So just link_to node.name, node.
How do I combine the two?
Use the display_with option? It takes a helper or proc. I would prefer a helper, but for brevity I show it with a proc:
<%= best_in_place node, :name, as: :input,
activator: "#edit-node-title-#{node.id}"
display_with: proc{|node| link_to node.name, node_path(node) }
%>
<%= link_to "<i class='fa fa-pencil'></i>".html_safe, "#", id: "edit-node-title-#{node.id}" %>
You could wrap your Best in Place tag inside of a link_to tag:
<h5>
<%= link_to node_path(node) do %><!-- Or whatever path you want to link_to -->
<%= best_in_place node, :name, as: :input, activator: "#edit-node-title-#{node.id}" %>
<% end %>
<%= link_to "#", id: "edit-node-title-#{node.id}" do %>
<i class='fa fa-pencil'></i>
<% end %>
</h5>
I also edited your activator link so it is a block too. This is untested, but it should work.
In Rails, I have a custom controller action that needs to accept some parameters from a form:
def update_ordid
# Get the active exchange
#exchange = Exchange.find(params[:id])
# Decide which order ID field to update
active_order_field = params[:ordfld]
# Save the order ID
order_id = params[:ordid]
if active_order_field == 1 then
#exchange.order_id_1 = order_id
else
#exchange.order_id_2 = order_id
end
#active_exchange.save
respond_with(#exchange)
end
Because these parameters aren't actual data fields in the exchange table, I would typically invoke the action by using a link such as:
link_to "Update Order ID", update_ordid_exchange(ordfld: value_from_form, ordid: value_from_form), :method => :post
Because in this case the value of these parameters needs to be populated by user input, I created the following form to pass the data:
<%= form_for(#exchange, url: update_ordid_exchange_path) do |f| %>
<div class="field">
<%= f.label :ordid, "Order ID" %><br>
<%= f.text_field :ordid, class: "form-control" %>
</div>
<% if #isrequestor == true %>
<%f.hidden_field :ordfld, :value => "1" %>
<% else %>
<%f.hidden_field :ordfld, :value => "2" %>
<% end %>
<div class="actions">
<%= f.submit "Submit", class: "btn btn-primary" %>
</div>
<% end %>
When I attempt to render this form, I receive the error: undefined method `ordid' for #
When researching this issue, I found that I could be able to do this by changing the text_field line to:
<%= f.text_field_tag :ordid, class: "form-control" %>
While this resolves the initial error, it throws a new error: undefined method `text_field_tag' for #
Any ideas as to what I'm doing wrong?
You cannot use form_for since your form elements doesn't represent the attributes of a model. use form_tag instead
<%= form_tag(update_ordid_exchange_path, :method => :patch) do%>
<div class="field">
<%= label_tag "Order ID" %><br>
<%= text_field_tag :ordid, class: "form-control" %>
</div>
<% if #isrequestor == true %>
<%= hidden_field_tag :ordfld, "1" %>
<% else %>
<%= hidden_field_tag :ordfld, "2" %>
<% end %>
<div class="actions">
<%= submit_tag "Submit", class: "btn btn-primary" %>
</div>
<%end%>
Documentation here
form_tag vs form_for
hidden_field_tag
I'm a bit new to this myself, but I believe it's because you're calling "text_field_tag" from a form_for instead of form_tag builder object. Try just leaving off the form for object as such:
<%= text_field_tag :ordid, class: "form-control" %>
I have a button on an index page that links to a new_assignment_path
<% #users.each do |user| %>
<tr>
<td><%= link_to user.name, user %></td>
<td><%= link_to 'Assign to Class', new_assignment_path, :class => 'btn btn-mini' %></td>
</tr>
<% end %>
And I want it so that when you click on it, it takes you to the new_assignment_path, and takes the dropdown select on that pages form.
<%= simple_form_for(#assignment) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :user_id, collection: User.all.collect, as: :select %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
And have the drop down automatically set to the user.id of whatever user the button was inside of.
<%= link_to 'Assign to Project', new_assignment_path(#assignment, :user_id => user.id), :class => 'btn btn-mini' %>
and
<%= link_to 'Assign to Project', new_assignment_path(:user_id => user.id), :class => 'btn btn-mini' %>
And neither of them worked.
What is the best way of doing this?
With your second option try setting the instance variable in the AssignmentsController.
#user_id = params[:user_id]
Then you need to specify the default in the form
<%= f.input :user_id, collection: User.all.collect, selected: #user_id %>
You don't need as: :select with Simple Form.