Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7917583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:12:03+00:00 2026-06-03T15:12:03+00:00

How can i pass a value from a view to another view to populate

  • 0

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T15:12:06+00:00Added an answer on June 3, 2026 at 3:12 pm

    Listen carefully,

    The following link “Sale” should be somewhere in your application.

    <%= link_to "Sale", new_sale_path, :client_id => client.id , :class => 'btn btn-mini' %>
    

    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”,

    new_sale GET    /sales/new(.:format)                                  sales#new
    

    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,

    @client = Client.find(params[:client_id])
    

    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.

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

    If you use a form_for like below,

    <% form_for @client do |client| %>
    
    <% text_field_tag client.name %>
    -
    -
    -
    
    <% end %>
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to pass a null value from a RenderAction to another view. But
In ASP.net MVC: How should/Can I pass Form data (From the View) to the
How can i pass value from grid view to a pop up in client
How can I pass a variable value from a template using JavaScript without using
How can I pass the result from a scalar [single row, single value] query
Can I pass value to Navigation Context like this: NavigationContext.QueryString[param1] = PARAM1; Is it
How can I traverse through this set of data? String[] from = new String[]{AppSQLite.KEY_NAME,AppSQLite.KEY_ICON};
How do I pass a value from one class to another. eg Class one
Question: How can you pass an instance of an object from the view model
I want to pass the value entered in a UITextfield into another view. and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.