Pre select form input based on params - ruby-on-rails

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.

Related

How do I reload my comments partial with ajax?

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.

Passing a value from one view to another

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.
What is the best way of doing this?
Add params[:user_id] in the selected option of your select tag
<div class="form-inputs">
<%= f.input :user_id, collection: User.all.collect, as: :select, :selected => params[:user_id] %>
</div>

Rails use helper method to search for id

Ok i have an simple input, where the user can typ in an user name, and the helper method finduserid should then search for the right user id:
<%= f.text_field :user_id, :value => finduserid(input), :class => "input-small" %>
The helper method:
def finduserid(name)
#user = User.find_by_typ(name)
return #user.id
end
First my code dont works because input is an "undefined local variable or method". So how can i change this? And is an helper method the right tool for this task? How would you perform it?
<%= form_for User.new do |f| %>
<td> <%= f.text_field :user_id, :value => findebmsid(input), :class => "input-small" %> </td>
<td></td>
<td> <%= f.text_field :extra %></td>
<td> <%= f.hidden_field :number, :value => "1" %></td>
<td><%= f.submit "Speichern", :class => 'btn btn-mini btn-success' %></td>
<% end %>
i dont think a helper is the correct aproach, you should use the show action on your app something like this:
view:
<%= form_tag(users_path,:method => :get ) do %>
<%= text_field_tag :user_name, params[:user_name] %>
<%end%>
controller:
def show
#user=User.find_by_user_name(params[:user_name])
end
show view:
<%= #user.name %>

The Mysterious Order-Dependent Checkbox State Array Controller Transmission Anomaly

Here's all of the relevant view code:
<%= form_for(:object) do |form| %>
<p id="batch_ops">
<b>Objects</b>
<%= submit_tag "Do This" %>
<%= submit_tag "Do That",
:confirm => "Really do that?" %>
<p id="capture" float="left">
<%= button_to_function "Operation_1", \
"Element.remove('batch_input_area'); Element.show('get_op1_params')" %>
<%= text_field_tag "op1_save_path", "_op1_full_dir_path_" %>
<%= button_to_function "Operation_2", \
"Element.remove('batch_input_area'); Element.show('get_op2_params')" %>
<%= text_field_tag "op2_save_path", "_op2_full_dir_path_" %>
</p>
<div id="batch_input_area" style="display:none;">
</div>
</p>
<ul id="object_list">
<% #objects.each do |c| %>
<li id="current_object" style="horizontal-align: left" width="auto"><b>
<%= hidden_field_tag('seen_objects[]', c.id) %>
<%= check_box_tag 'chkd_objects[]', c.id, c.object_checked? %>
<%= c.object_address %>
<% if c.data1? %>
<%= c.data1 %>
<% else %>
<%= "_____________" %>
<% end %>
<% end %>
</ul>
<div id="get_op1_params" style="display:none;">
<%= form_tag(
:action => 'index',
:remote => true,
:html => {:id => 'op1_form'}
) do %>
Options: <%= text_field :op1params, :op1_params, :value => 'freq=30 run=1 stop=0' %>
<%= submit_tag 'Op1 Run', :confirm => 'Run Operation_1 test on checked objects?' %>
<%= submit_tag 'Op1 Status' %>
<%= submit_tag 'Op1 Stop', :confirm => 'Stop Operation_1 test on checked objects?' %>
<%= link_to "Cancel", {:action => "index"} %>
<% end %>
</div>
<div id="get_op2_params" style="display:none;">
<%= form_tag(
:action => 'index',
:remote => true,
:html => {:id => 'op2_form'}
) do %>
Options: <%= text_field :op2params, :op2_params, :value => 'freq=30 run=1 stop=0' %>
<%= submit_tag 'Op2 Run', :confirm => 'Run Operation_2 test on checked objects?' %>
<%= submit_tag 'Op2 Status' %>
<%= submit_tag 'Op2 Stop', :confirm => 'Stop Operation_2 test on checked objects?' %>
<%= link_to "Cancel", {:action => "index"} %>
<% end %>
</div>
Now here's the twist: Over on the controller, the Operation_1 handler gets the checkbox presence array seen_objects[] and the activated elements of the checkbox state array chkd_objects[] while the Operation_2 handler does not. If I reverse the order of the get_op1_params and get_op2_params form_tag divs shown last above then the Operation_2 handler gets these checkbox arrays but the Operation_1 handler does not.
My question is simply: Why?
I've long since lost count of the different things I've tried to get around this, which included additional hidden_field_tags within the form_tags. Ultimately I gave up and used a far less elegant submit_tag with a text_field_tag in place of the Operation_2 button_to_function and its associated form_tag but still do not understand why I could not make both of these operations work using button_to_function as illustrated above.
If someone can get this to work, please post your solution here. Any suggestion short of a working solution I've probably already seen and tried.

Passing a parent_id via a form using the Ancestry gem

I am new to rails, I am using the ancestry gem and I am having trouble passing the parent_id via a form.
<% title "Messages" %>
<% for message in #messages %>
<div class="message">
<div class="created_at"><%= message.created_at.strftime("%B %d, %Y") %></div>
<div class="content">
<%= message.content %>
</div>
<div class="actions">
<%= link_to "Reply", new_message_path(:parent_id => message) %> |
<%= link_to "Destroy", message, :confirm => "Are you sure?", :method => :delete %>
</div>
</div>
<% end %>
<%= semantic_form_for #message do |f| %>
<%= f.error_messages %>
<p><%= f.select :content, [['United States','2'],['England','3'],['London','4']] %></p>
<p><%= link_to "Reply", new_message_path(:parent_id => :content) %></p>
<% end %>
I am trying to assign a number (if the user selects 'United States' the number will be '2') to some variable and pass that variable when the user click reply as <%= link_to "Reply", new_message_path(:parent_id => some_variable) %>
You are doing it wrong. Rethink the problem. You probably want to do something like
<%= semantic_form_for #message do |f| %>
<%= f.error_messages %>
<p><%= f.select :parent_id, [['United States','2'],['England','3'],['London','4']] %></p>
<p><%= f.submit "Reply" %></p>
<% end %>
And define needed routes.

Resources