How can i pass a value from a view to another view to populate another form in rails 3?
I am new to rails and im a bit confused about how to achieve this, here is my scenerio:
I generated a Clients and a Sales scaffold, then in the Clients index view i placed a search text field which searchs for the clients and renders them below it. Each client has a button called Sale, when clicked it should redirect you to the new_sales_path and automatically fill some fields of the form with the values of the client, like for example: name, address, id, phone number, etc…
So what i want to do is:
I want to pass the information of the client when the button Sale is clicked and then polute the new_sales form.
So i have this code in index clients view:
<%= form_tag clientes_path, :method => 'get', :id => "clientes_search", :class => "form-search" do %>
<p>
<%= text_field_tag :search, params[:search], :class => "input-medium search-query" %>
<%= submit_tag "Search", :name => nil, :class => "btn" %>
<% @clients.each do |client| %>
<tr>
<td><%= client.id %></td>
<td><%= client.email %></td>
...
...
<%= link_to t('.edit', :default => t("helpers.links.edit")),
edit_client_path(client), :class => 'btn btn-mini' %>
<%= link_to "Sale", new_sale_path, :action => 'sale_button', :id => 'sale_button', :class => 'btn btn-mini' %>
<% end %>
</tr>
<% end %>
In my application helper i have:
def sale_button
@sale_button = client
end
In my clients and sales controllers i have the line:
helper_method :sale_button
And in my new sales view i have
<div id='sales'>
<%= simple_form_for @sale, :html => { :class => 'form-horizontal' } do |f| %>
<%= f.input :client, :label => "Client ID", :required => true, :as => :string, :input_html => { :value => @sale_button.id } %>
...
...
<% end %>
I dont know if i am doing it the right way or if there is something that im missing, any help or input is welcome.
Im using Rails 3.2 btw
Sales controller
class SalesController < ApplicationController
before_filter :authenticate_user!
helper_method :sort_column, :sort_direction
helper_method :boton_venta
# GET /sales
# GET /sales.json
def index
@sales = Sale.search(params[:search]).order(sort_column + " " + sort_direction).paginate(:per_page => 10, :page => params[:page])
@lista_porcentajes = Porcentaje.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @sales }
format.js
end
end
# GET /sales/1
# GET /sales/1.json
def show
@sale = Sale.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @sale }
end
end
# GET /sales/new
# GET /sales/new.json
def new
@sale = Sale.new
@client = Client.find(params[:client_id])
respond_to do |format|
format.html # new.html.erb
format.json { render json: @sale }
format.json { render json: @client }
end
end
# GET /sales/1/edit
def edit
@sale = Sale.find(params[:id])
end
# POST /sales
# POST /sales.json
def create
@sale = Sale.new(params[:sale])
respond_to do |format|
if @sale.save
format.html { redirect_to @sale, notice: 'Sale was successfully created.' }
format.json { render json: @sale, status: :created, location: @sale }
else
format.html { render action: "new" }
format.json { render json: @sale.errors, status: :unprocessable_entity }
end
end
end
# PUT /sales/1
# PUT /sales/1.json
def update
@sale = Sale.find(params[:id])
respond_to do |format|
if @sale.update_attributes(params[:sale])
format.html { redirect_to @sale, notice: 'Sale was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @sale.errors, status: :unprocessable_entity }
end
end
end
# DELETE /sales/1
# DELETE /sales/1.json
def destroy
@sale = Sale.find(params[:id])
@sale.destroy
respond_to do |format|
format.html { redirect_to sales_url }
format.json { head :no_content }
end
end
private
def sort_column
Sale.column_names.include?(params[:sort]) ? params[:sort] : "id"
end
def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
end
end
routes.rb
Puntodeventa::Application.routes.draw do
resources :empresas
resources :cliente_grupo_empresas
devise_for :users, :admin_empresas, :empresa_users, :cliente_users, :admins
resources :relacion_porcentaje_grupoempresas
resources :relacion_grupo_empresas
resources :porcentajes
resources :categoria_empresas
resources :grupo_empresas
resources :sales
resources :cliente_empresas
resources :clients
get "home/index"
resources :productos
root :to => "home#index"
rake routes
sales GET /sales(.:format) sales#index
POST /sales(.:format) sales#create
new_sale GET /sales/new(.:format) sales#new
edit_sale GET /sales/:id/edit(.:format) sales#edit
sale GET /sales/:id(.:format) sales#show
PUT /sales/:id(.:format) sales#update
DELETE /sales/:id(.:format) sales#destroy
sales new.html.erb
<%- model_class = @sale.class -%>
<h1><%=t '.title', :default => t('helpers.titles.new', :model => model_class.model_name.human,
:default => "New #{model_class.model_name.human}") %> </h1>
<%= render :partial => 'form' %>
Form being rendered in sales new.html.erb
<div id='sales'>
<%= simple_form_for @sale, :html => { :class => 'form-horizontal' } do |f| %>
<%= f.input :client, :label => "Serial del cliente", :required => true, :as => :text, :input_html => { :value => ' ' } %>
<%= f.association :client, :label_method => :nombre, :label_value => :id, :required => true, :as => :string %>
<%= f.association :porcentaje, :label_method => :porcentaje, :value_method => :porcentaje, :required => true %>
<%= f.input :monto_venta, :required => true, :as => :string %>
<% if current_user.try(:admin?) %>
<%= f.association :empresa, :label_method => :nombre, :value_method => :id, :required => true %>
<% else %>
<% f.association :empresa, :disabled => true, :input_html => { :value => current_user.empresa_id } %>
<% end %>
<div class="form-actions">
<%= f.button :submit, :class => 'btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
sales_path, :class => 'btn' %>
</div>
<% end %>
</div>
New sales view with the modification you suggested.
<table class="table table-striped">
<thead>
<tr>
<th><%= sortable "id", "Id" %></th>
<th><%= sortable "name", "Name" %></th>
<th><%= sortable "grupo_empresa", "Grupo" %></th>
<th><%= sortable "telefono", "Telefono" %></th>
<th><%= sortable "celular", "Celular" %></th>
<th><%= sortable "email", "Email" %></th>
<th><%= sortable "serial_cliente", "Serial del cliente" %></th>
<th><%= sortable "puntos_cliente", "Puntos del cliente" %></th>
<th><%= sortable "created_at", "Creado el" %></th>
<th><%=t '.actions', :default => t("helpers.actions") %></th>
</tr>
</thead>
<tbody>
<% @clients.each do |client| %>
<tr>
<td><%= client.id %></td>
<td><%= link_to client.nombre, client_path(client) %></td>
<td><%= client.grupo_empresa.nombre %></td>
<td><%= client.telefono %></td>
<td><%= client.celular %></td>
<td><%= client.email %></td>
<td><%= client.serial_client %></td>
<td><%=l client.created_at %></td>
<td>
<%= link_to t('.edit', :default => t("helpers.links.edit")),
edit_client_path(client), :class => 'btn btn-mini' %>
<%= link_to "Sale", new_sale_path, :client_id => client.id , :id => 'boton_venta', :class => 'btn btn-mini' %>
</td>
</tr>
<% end %>
</tbody>
</table>
Listen carefully,
The following link “Sale” should be somewhere in your application.
If an User clicks that link, link_to method checks for the path and you have mentioned it as “new_sale_path”
From your “rake routes”,
It is clear that new_sale_path is referring to sales#new( sales controller, new action)
If you add underscore path, rails will take care of that. For example, edit_sale_path will execute sales controller, edit action/method.
After this step, since the new action in sales controller is executed, rails will look for a “new.html.erb” file in app/views/sales.
If it exists, that html.erb file will be displayed/rendered in your browser.
You should understand the above working.
Now, lets get specific.
When that sale link is clicked, the application reaches the sales#new action with :client_id as a parameter in the params hash.
You can access that params hash as follows,
You have done that. The above statement, takes the passed :client_id as input and finds the right client from the database using the Client model and find method.
Now the instance variable is set with a record from Clients table matching the :client_id.
Instance variable is @client.
Now, In your view you can use the instance variable @client as follows, which you have not done, or Im not seeing. What is there in app/views/sales/_form.html.erb ?
Please post that too.
If you use a form_for like below,
There will be a text_field which will be pre populated with the client’s name.
If you dont understand anything, tell me.
If you understood few things and couldn’t figure out few things, tell me.
If you understood everything, tell me.
Thanks.