Pass parameter of selected line to another Ruby view - ruby-on-rails

I am a beginner in ruby. I have a table in a view, that when I click on the line / item it should pick up the ID of the selected line and move to the other view that will be triggered, I am 3 days searching on and I can not implement.
The view that should take the ID or another parameter so that in the other I can treat the select to bring more detailed information >>:
<td>
<%= link_to pedido.id, detalhes_backoffice_pedidos_path(:pedido_id =>
pedido.id) , :onclick=>"window.open(this.href,'create_company',
'height=600,
width=600');return false;" %>
</td>
The view ("popup") that is called when clicked on the request id. Obs.:Tried in countless ways, and at the moment she is like this ... >>>>
<div class='container-fluid'>
<div style='display: block;' class="col-xs-6 esquerdo ">
<label>Num.pedido<%= pedido.id %></label> <br>
<label>nome</label> <br>
<label>telefone</label> <br>
</div>
Controller>>
class Backoffice::PedidosController < BackofficeController
.....
def detalhes
render :layout => "application"
#pedido = params[:pedido_id]
end
I am totally lost, after trying so much .... If you can pass some example link for study, it will be very useful too !!

It has become clear that you just need to use AJAX to show the selected pedido details on the page. Rails has an easy way to do this.
First put in a table with a div below it for displaying the pedido info. Each row in the table has a link to the controller action that will do the AJAX. Use remote: true to make the link AJAX, so the user is not taken to another page.
<table>
<tbody>
<% #pedidos.each do |pedido| %>
<tr>
<td><%= link_to 'More info on this Pedido!' fetch_info_pedidos_path(pedido), remote: true %></td>
</tr>
<% end %>
</tbody>
</table>
<div id="pedido-info"></div>
Make the route for the action in the pedido controller. This is in routes.rb
resources :pedidos do
get :fetch_info, on: :member
end
This should create a path that looks like /pedidos/:id/fetch_info with the name fetch_info_pedidos
The link in the table will make a js format request along this route so the controller action must be ready to handle that. The pedido id will come as a parameter called :id
def fetch_info
#pedido = Pedido.find params[:id]
respond_to :js
end
Since it responds to a js format request it will respond with js content, not html - that means we need to have a js file as the view, but before we make that let's make a partial for displaying pedido info. This can be dropped into any view using the render method. It will be in app/views/pedidos/_pedido.html.erb (you have to use an underscore at the start of the file name to show it is a partial) You can put any content in here you like, using the pedido variable to refer to the pedido. Here's an example:
<h2>Pedido!</2>
<p>number: <%= pedido.id %></p>
<p>nom: <%= pedido.nom %></p>
<p>telefone: <%= pedido.fone%></p>
Finally let's make the js that gets passed back to the browser. This will be an erb file like other views. It will just put the _pedido partial as the content to the div under the table. app/views/pedidos/fetch_data.js.erb
$('#pedido-info').html("<%= j render #pedido %>");
That's all you need. The j here escapes the output of the render method so that it plays nicely inside a javascript string.
There's a bit of Rails magic happening here, which I will explain, because, as you would learn at any wizard training school: "don't use any magic you don't completely understand"
The render method expects the name of a partial to render, and optionally some local variables to pass. If it's given only an ActiveRecord object, it will look for the partial that matches the class name of the object (Pedido goes to _pedido.html.erb) and it passes the object as a local variable also with matching name. so render #pedido gets translated to render partial: 'pedido', locals: {pedido: #pedido}
I haven't tried any of the code in this answer, I just wrote it off the top of my head, so you may have to jiggle it a bit to get it to work.
here's a link to a page you might have come across if you had simply Googled how to do this. It says essentially the same as what I have said here, except his AJAX brings in a whole collection of objects, not just one at a time.

I may have misunderstood the question, but it seems to me you just need to link to the detalhes action for each pedido in the table.
here is the table:
<table>
<tbody>
<% #pedidos.each do |pedido| %>
<tr>
<td><%= link_to 'Detalhes this Pedido!' detalhes_path(pedido) %></td>
<td>More table data related to this pedido</td>
</tr>
<% end %>
</tbody>
</table>
and in the routes you would have something like this:
resources :pedidos do
get :detalhes, on: :member, as: 'detalhes'
end

Related

Edit table line as form field using ajax in Rails

I'm working on an admin functionality for an application in Rails.
I've got a table in a page that lists a series of attributes of a particular model "Property".
I want users to be able to click on each line and make the attribute value editable by rendering a partial. In order to do that I assume I need to create a partial for each attribute and have a similar form in each partial with one visible field related to the attribute the user clicked on.
Something like:
View:
<tr id="edit_price" data-link="<%= 'edit_field_price_path' %>">
<% if !#property.price.blank? %>
<th scope="row">Price</th>
<td>$<%= #property.price %></td>
<td>Edit</td>
<% else %>
<th scope="row" class="empty">Price</th>
<td class="empty">Add price</td>
<% end %>
</tr>
Partial:
<%= form_with model: #property, method: :put do |f| %>
<%= f.number_field :price %>
<%= f.hidden_field ONE_FOR_EACH_VALUE, value: ATTRIBUTE_VALUE %>
...
<% end %>
The problem is that I've got over 40 potential fields that can be edited, which means I'd need to create over 40 partials in separate files and add them all to my routes.rb file.
How can I come up with a smarter solution for that?
Is it possible to pass the attribute name as a variable and dynamically edit a single form with jquery?
Does the same need to be done with the routes file?
Thanks a lot in advance.
for that you can use something like best in place gem, it basically create the functionality you like, not for the complete line, but for specific attributes.
read the documentation for configuration, after doing that, you can create the clickable/editable attribute by doing this on your view
<%= best_in_place #user, :name, :as => :input %>
this will create the clickable text, and this will fire the input on the frontend to edit it.
I ended up creating all of those partials. They have different types of fields, so I couldn't find any way to optimize that.
Running a single jquery script that identifies where to place each partial depending on the field name to access each form partial made implementation a bit easier though. It's been proposed here: Concatenate jquery variable into Rails partial

Print a partial in rails

I have a page in rails that has several partials rendered in it. I want many of those partials to have a link which will allow the user to print them. Currently, I can only get the print link to print the whole page rather than just the partial.
Is there a way to do this, or will I need the user to load a whole page before printing?
Edit:
I would like to have each partial have its own individual printing link. There are two many link_to lines I've tried using.
<%= link_to 'print ingredients', :partial => 'table_ingredients', :onclick => 'window.print();return false;' %>
The line above doesn't do anything when clicked.
<%= link_to 'print ingredients', 'table_ingredients', :onclick => 'window.print();return false;' %>
The line above prints the whole page.
I was lead to believe that the second argument for link_to was the page that should be printed.
I do it this way:
In my view I have a link like this:
<%= link_to 'Show Current Inventory Levels', parts_inventory_levels_path, :target => '_blank'%>
The :target => '_blank' causes it to open in a new tab/window
The parts_inventory_levels_path is in the routes.rb as:
get 'parts/inventory_levels' => 'parts#inventory_levels'
So it's going to the parts controller and calling the inventory_levels action which is:
def inventory_levels
#parts = Part.all.order(:name)
render 'inventory_levels', layout: "print_table"
end
Here's the key part, layout: "print_table". I have a layout file in my app/views/layout/ folder called print_table.html.erb:
<head>
<%= stylesheet_link_tag 'print', media: 'all' %>
<%= javascript_include_tag 'application' %>
<%= csrf_meta_tags %>
<%= yield(:head) %>
</head>
<body>
<%= yield %>
</body>
So the data from my controller action calls render on inventory_levels.html.erb:
<%= link_to_function('Print this Page', 'javascript:print()') %>
<br>
<table id="parts_table" class="table pretty" border='1'>
<thead>
<tr>
<th class="sortable">Name</th>
<th class="sortable">Sku</th>
...table omitted for brevity
Note the first line, it calls the javascript:print() function to print the page. The reason I render a separate page is I want to format that page in a very simple way that is better suited to printing. The nice thing is I am still able to use the table_sorter javascript to sort the table before I print it.
"I was lead to believe that the second argument for link_to was the page that should be printed." That is incorrect. The second part of the link_to is the path or url. Rails is interpreting your table_ingredients as
print ingredients
I don't know your controller name so I had to use a placeholder.
I could help you write the actual code but you should just be able to use what I have here applied to your code. But if you need more help please post your controller, and any applicable routes. You can probably reduce repetitive code by passing the same controller a value as to which partial you want to render (I am rendering a whole new page but the partial should also work). Also there is probably away to use AJAX.

Rails 4 - Displaying a dependent model in a table

I have an Article model, with a dependent model Photocup. Each Photocup instance has an image attached (using paperclip/imagemagick). I followed the rails guide for adding a second model, the same way Comments are associated with an Article.
http://guides.rubyonrails.org/getting_started.html#adding-a-second-model
It works fine. But I want more control over how the Photocups are displayed. I've created a partial _gallery.html.erb in Views/Article. The gallery partial looks like this:
Photo Gallery
<table class="table2">
<%= render #article.photocups %>
</table>
_photocup.html.erb in Views/Photocup looks like this"
<td>
<%= photocup.label %>
<br>
<%= image_tag photocup.image(:small) %>
<br>
<%= link_to 'Remove Photo', [photocup.article, photocup],
method: :delete,
data: { confirm: 'Are you sure?' } %>
</td>
Works fine, but I can't figure out how to add in a row!
Is there anyway to display the Photocups for the Article I'm looking at the way one does with non-dependent model? For example, an index page would typically have something like this"
<table>
<tr>
<th>Title</th>
</tr>
<%= #photocups.each do |photocup| %>
</tr>
<td><%= photocup.title %></td>
<% end %>
</table>
Could this be done on the show.html.erb page for the article somehow? Could it be done if I created a partial in Views/Photocup?
If it matters, the route looks like this:
resources :articles do
resources :comments
resources :photocups
get 'gallery'
get 'showall'
get 'feature', on: :collection
end
Thank you in advance for any help.
It can be on the articles#show page. Provided your associations are done correctly, you have a:
#article.rb
Class Article > ActiveRecord::Base
has_many :photocups
end
and
#photocup.rb
Class Photocup > ActiveRecord::Base
belongs_to :article
end
You'll have to declare an instance variable under the show action in the Articles controller. It'll look something like this:
#articles_controller.rb
def show
#article=Article.find(params[:id])
#photocups=#article.photocups
end
Then use the code for your example index view in your show view and it should show the rows you are seeking.
Also, side note, this line:
<%= #photocups.each do |photocup| %>
in your example index page does not need an =. Those should be reserved for things you want the user to see in the view, it should be:
<% #photocups.each do |photocup| %>.

getting text field value from view page into controllers page

I have a text field "tf_Designation" in a view page, see below:
<% form_for(:search) do |f| %>
<table>
<tr>
<td align="center">
<%= f.text_field :tf_Designation,placeholder: "Designation" %>
</td>
</tr>
</table>
<% end %>
I want to get text field value into a controllers page and I am doing like below:
def search
#blah=params[:search][:tf_Designation]
if !params[:search][:tf_Designation].blank?
#Designation = params[:search][:tf_Designation]
render '/index'
end
end
And I do not have search model. But it gave me error at this line #blah=params[:search][:tf_Designation]. The error is below:
undefined method `[]' for nil:NilClass
Kindly suggest me where I make mistake, waiting for your reply. Thanks
It's most likely an issue with how Rails processes form_for - we've only ever used instance variables with it (which can be populated by a resource):
form_for
Form helpers are designed to make working with resources much easier
compared to using vanilla HTML.
Typically, a form designed to create or update a resource reflects the
identity of the resource in several ways
(i) the url that the form is
sent to (the form element's action attribute) should result in a
request being routed to the appropriate controller action (with the
appropriate :id parameter in the case of an existing resource)
(ii)
input fields should be named in such a way that in the controller
their values appear in the appropriate places within the params hash,
(iii) for an existing record, when the form is initially
displayed, input fields corresponding to attributes of the resource
should show the current values of those attributes.
form_tag
I would highly recommend switching to form_tag, as this deals with perishable data:
#app/views/elements/_search.html.erb
<%= form_tag "/search" do %>
<%= text_field_tag :tf_Designation, nil, placeholder: "Designation" %>
<%= submit_tag "Search" %>
<% end %>
#app/controllers/your_controller.rb
def search
#designation = params[:tf_Designation]
unless #designation.blank?
render '/index'
end
end

Edit a record displayed as a table row in Ruby on Rails (3.1.0)

I am new to Rails and have been learning it on my own. This is also the first time I am posting on Stackoverflow although I refer to it a lot. Below is the snippet of the ERB code that's displaying the records of a model called Ideas:
<% #idea.each do |i| %>
<tr>
<td><%= i.name %></td>
<td><%= i.description %></td>
<td><a class="btn" href="share"><i class="icon-share"></i></a></td>
<td>
<%= link_to(ideas_edit_path(#i), :class => 'btn') do %>
<i class="icon icon-edit"> </i>
<% end %>
</td>
<td><a class="btn" href="destroy"><i class="icon-trash"></i></a></td>
</tr>
<% end %>
Each row contains "Name", "Description" followed by icons called "share", "edit", "destroy". I want to display a form for each of these actions for that particular record. I don't know how to pass the id of the record to these actions. Can somebody please point me in the right direction? I think I have the routes defined correctly because I can type in the full ERL (/ideas/2/edit) and it brings up the edit form.
When you're using the named routes (i.e. ideas_edit_path), you can just pass a model object in as an argument, and they'll build the correct route for that object. There's a bug in your code above where you're referring to #i when it should be i (the local variable declared in the loop above it).
Another minor nit: you probably want to use #ideas rather than #idea for the collection to indicate that it's a collection, but that's not necessarily a bug in itself.
Change your #idea to #ideas to reflect that it is a collection.
The set that you iterate over should be something like
#ideas.each do |idea|
link_to(edit_idea_path(idea), :class => 'btn')
end
So your link is now to edit_idea_path(idea)
The form file will be in app/view/ideas called edit.html.erb
Actually the form is shared by the new and edit actions so the best idea is to have _form.html.erb and include that in both the new and edit forms.
Code in the .erb template will be like
form_for #idea do |f|
various input fields
end
I would recommend that you quickly try to create another app using the scaffolding that's available in rails. Then look at what gets generated and you'll be better informed about what things should be named, where they are, etc.
This might sounds a bit drastic, but really it's as simple as:
$ cd ~ # Go to root
$ mkdir newapp # Create a new directory
$ cd newapp # Create a new rails app
$ rails generate scaffold Idea name:string description:text # Use scaffolding
You can use the link_to helper with a do to build a block.
<%= link_to edit_idea_path(i), class: "btn" do %>
<i class="icon icon-edit"></i>
<% end %>
Looks like your using the Bootstrap framework with Rails -- very nice combination!

Resources