passing data to a rails partial - ruby-on-rails

Edit: Solution Worked Perfectly.
dom_id
I am trying to pass data to a partial and use it to change roles of a user. Right now it will only change the roles of one user. When it is rendered it only displays and acts on the roles of User.first
Where it is rendered. i pass the user in locals. as per this post I used the second answer because the first one didnt work and gave me errors.
<%= render :partial => 'roles', :locals => { :user => user } %>
Below is the partial _role.html.erb
<div id="roleModal" class="reveal-modal">
<%= semantic_form_for user, :url => user_path(user), :html => {:method => :put } do |f| %>
<h3>Change Role</h3>
<div class="reveal-modal-body">
<%= f.input :roles, :as => :check_boxes, :input_html => { :selected => user.role_ids } %>
</div>
<a class="close-reveal-modal">×</a>
<%= f.action :submit, :as => :button %>
<% end %>
<% #users.each do |user| %>
<tr>
<td><%= link_to user.name, user %></td>
<td><%= user.email %></td>
<td><%= user.created_at.to_date %></td>
<td><%= user.roles.first.name.titleize unless user.roles.first.nil? %></td>
<td>
Change role
<%= render :partial => 'roles', :locals => { :user => user } %>
</td>
</tr>
<% end %>

You had the original question about passing locals to a partial correct. The issue was using non-unique pairs of IDs for revealing your hidden forms. So the reveal links always revealed the first occurence of the div with that ID, thus always changing the role on the first user.
Try updating these two lines to use the Rails dom_id() helper to create unique matching pairs of data-reveal-id to id as per the Foundation reveal modal docs:
<a href="roles#<%= user.id %>" data-reveal-id="<%= dom_id(user) %>"
class="button" type="button">Change role</a>
<div id="<%= dom_id(user) %>" class="reveal-modal">

Related

Rails form submit operate like a link_to

So ultimately what I'm trying to do is get the form to be split across two different columns with the status to be in one column and the save button in another column next to the link_to OR have the form's submit operate like a link_to. The form automatically applies some CSS that's causing the issue of splitting the form.
<tbody>
<% #training_resource.spud_users.each do |training| %>
<tr>
<td><%= training.full_name %></td>
<% utr = training.user_training_resources.where(training_resource: #training_resource).first %>
<td class="utr-form">
<%= tb_form_for [:admin, utr], url: admin_update_utr_path(utr), :remote => true, :html => {:id=>'form_id'}, :data => {:errors => :inline, :success => admin_training_resources_path} do |f| %>
<%= f.select :status, UserTrainingResource.statuses.map {|k,v| [k.humanize, k]}, selected: utr.status %>
</td>
<td class="table-actions">
<%= f.tb_save_buttons('', admin_update_utr_path(utr)) %>
<% end %>
<%= link_to 'submit', admin_update_utr_path(utr), :onclick => "$('#form_id').submit()" %>
<%= link_to 'Delete', admin_destroy_utr_path(utr), :method => :delete, :data => {:confirm => 'Are you sure you want to delete this?'}, :class => 'btn btn-danger btn-sm' %>
</td>
</tr>
<% end %>
</tbody>
So what I'm trying to figure out is if there is a way to change the form save button to be a link_to. Right now I have it here under link_to 'submit'. It however does not operate like the tb_save_button as it doesn't redirect to the correct location or save.
You could handle this in the controller.
(example)
def create
if utr.save
redirect_to admin_update_utr_path(utr)
else
# render new form or display the previous view
end
end

rails render partial template with collection not working

Found some similar answers, but no one seems to fit.
I also read http://guides.rubyonrails.org/layouts_and_rendering.html, chapter 3.4.5, Rendering collections.
I'd like to render the following partial: (users/_user_append.html.erb)
<%= content_tag_for(:tr, user) do %>
<td class="dim"><%= user.id %></td>
<td><%= user.lastname %></td>
<td><%= user.firstname %></td>
<td><%= user.company %></td>
<td><%= user.email %></td>
<td class="fadeactions col-md-2">
<%= link_to(content_tag(:i, nil, class: 'fa fa-minus-square-o'),
remove_mailgroup_user_path(#mailgroup, user),
:method => :delete, remote: true,
:data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')),
toggle: "tooltip", title: "Remove user", placement: 'top'
}
)
%>
</td>
<% end %>
The call for rendering looks like:
<%= render partial: 'users/user_append', collection: #users, as: :user %>
...as: :user... should pass the collection to the partial, as far I understand.
However, nothing appears in the browser.
Who finds the bug? Thanks a lot for your help.
After some discussion we realised the problem was not in the way you call your partial, but rather in the way you defined the #users. As they weren't defined correctly, there was no way for the partial to receive the :user param.
The solution was to change
<%= render partial: '/users/user_append', collection: #users, as: :user %>
to
<%= render partial: '/users/user_append', collection: #mailgroup.users, as: :user %>

render from another index scaffold

I continue with this problem
Another Post
But somebody told me that if i want i can pass parameteres in the render, but i dont know how to do it, I mean here
<%= render :file => "userscuentas/index" %>
SO maybe I can pass the #userscuentas as parameteres
I really need your help
Thanks
From your http://pastebin.com/PuX5JheJ
change app/views/userscuentas/index.html.erb to
<%= render "list", :userscuentas => #userscuentas %>
then make a NEW file called app/views/userscuentas/_list.html.erb with this code
<% userscuentas.each do |userscuenta| %>
<tr>
<td><%= link_to userscuenta.id, userscuenta_path(userscuenta) %></td>
<td><%= userscuenta.nombre %></td>
<td><%= userscuenta.nro_cuenta %></td>
<td><%= userscuenta.tipo_cuenta %></td>
<td><%= userscuenta.user_id %></td>
<td><%=l userscuenta.created_at %></td>
<td>
<%= link_to t('.edit', :default => t("helpers.links.edit")),
edit_userscuenta_path(userscuenta), :class => 'btn btn-mini' %>
<%= link_to t('.destroy', :default => t("helpers.links.destroy")),
userscuenta_path(userscuenta),
:method => :delete,
:data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) },
:class => 'btn btn-mini btn-danger' %>
</td>
</tr>
<% end %>
You will also have to change how you are calling it in app/views/users/layout_users.html.erb
Also make sure (after you have changed this) that the #userscuentas variable is set in the controller action that references this view.
<li>Cuentas</li>
<div class="tab-pane" id="tab4">
<p>
<%= render "userscuentas/list", :userscuentas => #userscuentas %>
</p>
</div>
I'm kind of unsure about the structure of your application, but your new.html.erb will also not work in the way you are calling that partial because you have to set the #usersuentas variable before it will render properly. You are also calling there a view, and not a partial.
app/views/users/new.html.erb
<%= render "userscuentas/list", :userscuentas => #userscuentas %>
This is assuming you have to populate your #userscuentas variable in the controller side as well, so
userscuentas_controller.rb
def new
#userscuentas = #user.userscuentas.all
#userscuenta = Userscuenta.new
end
Assuming the file trying to be rendered is a partial, try this:
<%= render 'userscuentas/index', :userscuentas => #userscuentas %>
You can then access the variable in the partial using usercuentas
EDIT
For the partial, do the following to prevent the nil error:
<% if !userscuentas.nil? %>
#Do whatever you have to do here
<% end %>

observe_field in a rails 2.2.2 app is not saving checkbox value

I have the following form in a rails app which is not working:
<% form_remote_for :team do |f| %>
<tr>
<% 1.upto(12) do |i| %>
<td>
<%= f.check_box #mo[i] %>
<%= observe_field 'team_' + #mo[i],
:method => :put,
:with => "$(#mo[i] + '=') + $('team_'+#mo[i]).value" %>
</td>
<% end %>
</tr>
<% end %>
#mo[i] is the name of the field in the database (e.g. jan, feb, mar,... dcm). The code would look like this if i were to write them all out:
<% form_remote_for :team do |f| %>
<tr>
<td>
<%= f.check_box :jan %>
<%= observe_field 'team_jan',
:method => :put,
:with => "'jan=' + $('team_jan').value"
</td>
...
</tr>
<% end %>
Thanks for your help.
Your :with statements has some syntax problems, and you are also not interpolating your ruby variables correctly. Try this:
:with => "'#{#mo[i]}=' + $('team_#{#mo[i]}').value"
I derived this by taking your intended output 'jan=' + $('team_jan').value and replacing all instances of jan with #{#mo[i]}, which is the way to interpolate your variable that contains the month name.
Also, you probably need to add a :url option to tell where your ajax call should be sent. Something like this:
<%= observe_field 'team_jan',
:url => team_path(#team),
:method => :put,
:with => "'#{#mo[i]}=' + $('team_#{#mo[i]}').value" %>
I am guessing here that your desired endpoint is team_path(#team), but replace it if it is not.

Popup with edit action not working in rails 3

I have popup which is opened using:
<%= link_to user.username, "/users/"+user.id.to_s+"/edit", :method => :post, :target=> "_blank" %>
edit.html.erb file is:
<div id="dvUserMgmt" class="popup">
<%= form_for(:user, :url => { :controller => 'users', :action => 'update'}) do |f| %>
<table>
<tr>
<td class="labelfield">*Name</td>
<td class="textfield">
<input type="text" name="tb_realname" value=<%= #realname %> />
</td>
</tr>
<tr>
<td class="buttons" colspan="3">
<%= submit_tag 'Update', :url => { :controller => 'users', :action => 'update'}, :class => 'popup_closebox' %>
<%= tag :input, :type => 'button', :value => 'Cancel', :class => 'popup_closebox' %>
</td>
</tr>
</table>
<% end %>
</div>
Upon clicking update button, the action update is not getting executed.
Can anyone point where is the issue?
Lots of code is not written the Rails way, let's correct it and see if it changes your result.
1) correct your line, but explain wh you have a post method for an edit?
<%= link_to user.username, edit_user_path(user), :method => :post, :target=> "_blank" %>
2) Rails knows your entry already exists, I guess #user is defined
<%= form_for #user do |f| %>
3) use the form helpers:
<%= f.submit 'Update', :class => 'popup_closebox' %>
4) Provide your params to help debugging.

Resources