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
Related
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
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
to start out preemptively, I've already looked at various similar articles dealing with this, but I still get the error.
I'm starting out on rails and attempting to create a GPA calculator and tracker application for fun (and spent a lot of time searching through documentation); I have a singular controller and view since redirecting to an entire different page for calculating or saving a new GPA every time would look ugly.
Rails will display everything without error up until I add the form, no other erb is written currently, and the form is meant to submit letter grade values from the "f.selection" tag.
The culprit is #cgpa in <%= form_for #cgpa do |f| %>.
My form from main\index view:
<%= form_for #cgpa do |f| %>
<div class="field">
(...)
</div>
<div class="actions">
<%= f.submit 'Calculate' %>
</div>
<% end %>
My controller:
class MainController < ApplicationController
def index
##cgpa = CurrentGpa.all #currently calls a key_to error while form exists, otherwise no error is raised
#pgpa = PastGpa.all
#csem = CurrentSemester.all
#psem = PastSemester.all
end
def new
#cgpa = CurrentGpa.new
end
def create
(...)
end
end
The routes are simply Rails.application.routes.draw { root 'main#index'; resources :main }
If any other information is needed, just let me know to add >.>
When you use: <%= form_for #cgpa do |f| %> this automatically tries to submit the form to the CurrentGpasController create action and for doing so it sends a request to current_gpas_path. So you don't have this path in routes that is why it throwing error. Either you can add routes for CurrentGpa like:
resources :current_gpas
or you can specify a path in the form_for:
<%= form_for #cgpa, url: any_path do |f| %>
So this will submit your form to that url specified.
If you add the current_gpas routes then do create the controller and action to process your input.
And as mentioned in comments do add the #cgpa = CurrentGpa.new this in index action. The above will solve your error you are getting after that.
Hope this helps.
I am trying to capture validation errors for individual forms as I have many on one page.
I am giving each form its own unique id by using object_id
<% object = #document || Document.new %>
<%= form_for object, :html => { id: object.object_id.to_s } do |f| %>
But if i do this to capture errors the same error message will appear on all my forms
<% if object.errors.any? %>
# errors
<% end %>
I have tried
<% if object.object_id.errors.any? %>
But i get
undefined method `errors' for 59187740:Fixnum
Is there a way around this please
Thanks
Edit
i have just noticed that the form id changes when validation fails as the page reloads, so that explains why the object cannot be found.
How can i keep the form id the same?
If really depends on what you want to do with the errors:
You want to show them for each form:
<%= form_for some_object, do |f1| %>
<%= f1.error_messages %>
<%= # f1 magic %>
<%= form_for other_object, do |f2| %>
<%= f2.error_messages %>
<%= # f2 magic %>
You want to access them for each object:
just call object.errors: some_object.errors
What isn't working for you:
Calling object_id on an object access its id which is a number (FixNum). You are trying to call a method/access an attribute on a FixNum which does not exist.
You just want some persistent ID for your form
I assume that your object is coming from a database and already has an ID. So why don't you just use object.id? Every time Rails create an ActiveRecord object for you from the DB, that object gets a new object_id. So it is only logical that the ID doesn't stay the same since you have a new object in memory. Read more about it here:
https://stackoverflow.com/a/3430487/2295964
I have a table in view:
#view
<%= form_tag save_table_path do %>
<table>
<% #channel_name_body.values.max_by(&:size).size.times do |i| %>
<tr class="border">
<% #channel_name_body.values.map { |a| a[i] }.each do |value| %>
<td contenteditable class="border light_green"><%= value %></td>
<% end %>
</tr>
<% end %>
</table>
<%= submit_tag "save",:class => "btn btn-primary offset4" %>
<% end %>
I don't know what to do next to pass value of all cell in table to controller such as:
#controller
def save_table
#table=params[:table] #or #row1=params[:row1]... or #col1=params[:col1]....
end
Edit: I found way to solve this problem, it must use js.
I don't want to use js, what about if I change to <%= text_field_tag :cell, value %> how can I get value of all cell in table ?
I think you're getting confused with how to handle data in Rails (or any backend system)
HTML
HTML is a markup language; which means if you give it certain code, it will put various elements onto the page. The <table> tag is one of those elements, and has no bearing on the controller-side functionality of your app
Controller Params
HTML form params are directly related to the form (nothing else)
In Rails, you get the params hash ordered like this:
params[:form_name][:input_name]
Your Code
From what you've shown, it seems you have several parts missing
Firstly, you need input elements (to populate the params). Currently, you have:
<td contenteditable class="border light_green"><%= value %></td>
This does not create any params, as it's not an input element. It's just a value that's been outputted on the screen. What you'd need is something like:
<td contenteditable class="border light_green"><%= text_field_tag :cell, :value => value %></td>
By adding these inputs, you will give Rails the ability to populate the params hash with their values, which you can then access from your controller like this:
def save
#table = params[:form_name][:cell][0]
end
Hope this helps?
Try this:
<%= text_field_tag "table[cell#{index}]", value %>
On form submit it will give you values like params[:table][:cell1], params[:table][:cell2] and so on...
Hope this helps..