Button not works inside a table - ruby-on-rails

I have a form, in this form I want to put some buttons, in case, submit and cancel.
I want this button stay in the same line, so I'm using a table.
But it's not working.
When I put the cancel button inside this table, the button does not work and it is not redirecting to root_path.
But if I put the button(cancel) outside this table, the button works but does not stay on the same line with the submit button
Does not work:
<table>
<tr>
<td><h1><%= t('labels.analyst') %></h1></td>
<td><%= f.submit :class => "btn" %> </td>
<td><%= button_to "Cancelar", root_path, :class => "btn", :method => :get %></td>
</tr>
</table>
Works:
<table>
<tr>
<td><h1><%= t('labels.analyst') %></h1></td>
<td><%= f.submit :class => "btn" %> </td>
</tr>
</table>
<%= button_to "Cancelar", root_path, :class => "btn", :method => :get %>
Any suggestion, please?

you can use:
<%= link_to "Cancelar", root_path, :class => "btn" %>

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

Render table in partial Rails 4.0

Could someone help me render a table view partial? Basically, I have a Link model with two attributes: Description and URL. My Link model is a nested resource of my Opportunity model. Therefore when I click "show" on an opportunity, I want it to show the Opportunity then show all the links that belong to that opportunity. I want the links to be rendered into a table with columns labeled "Description" and "URL". My current code looks like this:
views/opportunities:
<%= render #opportunity %>
<h3>Links</h3>
<div id = "links">
<%= render #opportunity.links %>
</div>
views/links/_link
<%= div_for link do %>
<p>Description:<%= link.description %></p>
<p>URL: <%= link.link_url %></p>
<span class='actions'>
<%= link_to 'Delete', [#opportunity, link], :confirm => 'Are you sure?',
:method => :delete %>
</span>
<% end %>
You're going to want...
<%= render #opportunity %>
<h3>Links</h3>
<table id = "links">
<thead><tr>
<th>Description</th>
<th>URL</th>
</tr></thead>
<tbody>
<%= render #opportunity.links %>
</tbody>
</table>
And then...
<tr>
<td><%= link.description %></td>
<td><%= link.link_url %></td>
<td><span class='actions'>
<%= link_to 'Delete', [#opportunity, link], :confirm => 'Are you sure?',
:method => :delete %>
</span></td>
</tr>
I'm a little unclear on how you're using the span in the partial, so I left it as a separate column in the table.
I hope that helps.

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 %>

onclick getting the correct data

I have an index view of a table called tasks. On each row I want the user to be able to click on Edit or Add Attach (add an attachment).
This is the code:
<% #workorder.tasks.each do |task| %>
<tr>
<td><%= task.position %></td>
<td><strong><%= link_to task.taskname, task_path(task) %></strong></td>
<td>
<a rel="popover" data-content="<%= task.longdesc %>" data-original-title="Description:"><img src='/images/find.ico' width='20' height='20'></a>
</td>
<td><%= task.employee.try(:employee_full_name) %></td>
<td><%= task.taskstatus.try(:statuscode) %></td>
<td><%= task.attachments.count %></td>
<td>
<a href="#" class="answer" data-type="select" data-pk="1" data-resource="task" data-source="/ratings" data-name="rating_id" data-url="/tasks/<%= task.id %>" data-original-title="Select Rating">
<% if task.rating_id != nil %>
<%= textarea_format(task.rating.ratingname) %>
<% end %>
</a>
</td>
<% if current_user.admin == true %>
<td><%= link_to 'Edit', edit_task_path(task), :class => 'btn btn-mini btn-success' %></td>
<td><%= link_to 'Add Attach', new_attachment_path, :class => 'btn btn-mini btn-primary', :onclick => flash[:task_id] = task.id %></td>
<% end %>
</tr>
The Add Attach links to a page for adding the attachment. In order to have it properly attach to the specific task, I use flash to carry forward the task.id.
But, the onclick is always passing the last task.id instead of each button containing the task.id for that row.
Any idea how I could accomplish this?
Thanks!!
I would think your 'Add Attach' link would look like:
<td>
<%= link_to 'Add Attach',
new_attachment_path(task.id),
:class => 'btn btn-mini btn-primary'
%>
</td>
This assumes you have a route entry that looks something like:
match "tasks/:id/add_attach" => "tasks#add_attach" , :as=>"new_attachment"
Then in your tasks_controller.rb:
def add_attach
#task = Task.find(params[:id])
# I don't know what goes here, not enough code posted
#task.save
end

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