render from another index scaffold - ruby-on-rails

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

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

Why my form won't submit correctly?

I have User model and Comment model using acts_as_commentable_with_threading
I'm trying to put the comment function on each User's show page with partial.
But When I try to submit a form, it shows routing error.
Why is this?
Error
Routing Error
No route matches [POST] "/user/john/add_comment"
models/user.rb
acts_as_commentable
views/users/show.html.erb
<%= render 'comment', :user => #user %>
views/users/_comment.html.erb
<table>
<tr>
<th>ID</th>
<th>Title</th>
<th>Body</th>
<th>Subject</th>
<th>Posted by</th>
<th>Delete</th>
</tr>
<% #user.comment_threads.each do |comment| %>
<tr>
<td><%= comment.id %></td>
<td><%= comment.title %></td>
<td><%= comment.body %></td>
<td><%= comment.subject %></td>
<td><%= comment.user.user_profile.nickname if comment.user.user_profile %></td>
<td><%= button_to 'destroy', comment, confirm: 'Are you sure?', :disable_with => 'deleting...', method: :delete %></td>
</tr>
<% end %>
</table>
<%= form_for #user, :html => { :class => 'form-horizontal' } do |f| %>
<div class="field">
<%= f.label :body %><br />
<%= f.text_field :body %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
routes.rb
get "user/:id/add_comment" => 'users#add_comment', :as => :add_comment_user
get "user/:id/delete_comment" => 'users#delete_comment', :as => :delete_comment_user
users_controller.rb
def add_comment
#user = User.find_by_username(params[:id])
#user_who_commented = current_user
#comment = Comment.build_from( #user, #user_who_commented.id, params[:users][:body] )
#comment.save
redirect_to :controller => 'users', :action => 'show', :id => params[:users][:id]
flash[:notice] = "comment added!"
end
Because your route is for a GET, and the form is a POST.
It needs to be a post route. Also you are posting to user/:name/add_comment, you should post the the user id. You can use the name but this would need to be unique across all users.
This line is also wrong.
#user = User.find_by_username(params[:id])
You can either pass in the params[:username] of find_by_id

Ruby on Rails, Posting Variables

I'm very new to rails so hopefully this should be a quick fix. I'm writing an application that searches a database and reloads the page displaying the desired results. In rails how does one save a input into a text_field and post it so that it can be retrieved and used in the query for retrieving data.
My view:
<title></title>
</head>
<body>
Search Collection <br>
<%= text_field "person", "name" %>
<%= select_tag(:search_id, '<option value="0">Search by</option><option value="1">Make</option><option value="2">Condition</option>
<option value="3">Sport</option>') %>
<%= link_to 'New catalog', new_catalog_path %>
<br>
<br>
<%= link_to "Search", :search_text => , :action => :index %> <br>
<br>
<h1>Results</h1>
<%= will_paginate #catalogs %>
<table border="1">
<tr>
<th>Catalog id</th>
<th>Year</th>
<th>Make</th>
<th>Card number</th>
<th>Number</th>
<th>Condition</th>
<th>Sport</th>
<th>Tracking list</th>
</tr>
<% for catalog in #catalogs %>
<tr>
<td><%= catalog.Catalog_ID %></td>
<td><%= catalog.Year %></td>
<td><%= catalog.Make %></td>
<td><%= catalog.Card_Number %></td>
<td><%= catalog.Number %></td>
<td><%= catalog.Condition %></td>
<td><%= catalog.Sport %></td>
<td><%= catalog.Tracking_List %></td>
<td><%= link_to 'Show', catalog %></td>
<td>
<%= link_to 'Edit', edit_catalog_path(catalog) %></td>
<td>
<%= link_to 'Destroy', catalog, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<br>
</body>
My Controller method
def index
#search_text = 'T206 White Border'
#catalogs = Catalog.paginate_by_sql ['select * from catalogs where Make =\''+ #search_text+'\'' , 80000], :page => params[:page]
end
Be gentle if its an easy fix, I'm still getting used to the whole MVC thing
Your question has a lot going on, so let's try to sort through it one piece at a time. First, I'll assume your database has a table called catalogs with a column called make, and that you're using the will_paginate plugin. It looks like you got started by copying and modifying some examples straight from the docs. First, your controller - you don't need the more complex paginate_by_sql, you can use the simpler paginate.
controller:
def index
#catalogs = Catalog.paginate(:all, :conditions => {:make => params[:search]}, :page => params[:page])
end
Now your view, just the stuff relevant to the search:
<% form_tag catalogs_path, :method => :get do %>
<%= text_field_tag 'search' %>
<%= submit_tag 'submit search' %>
<% end %>
And that's it. Good luck!

Resources