如何将值从视图传递到另一个视图以填充 rails 3 中的另一个表单?
我是rails新手,我对如何实现这一点有点困惑,这是我的场景:
我生成了一个客户和一个销售脚手架,然后在客户索引视图中放置了一个搜索文本字段,用于搜索客户并将其呈现在其下方。每个客户都有一个名为“销售”的按钮,单击它时应将您重定向到 new_sales_path 并使用客户的值自动填写表单的某些字段,例如:姓名、地址、ID、电话号码等...
所以我想要做的是:我想在单击按钮 Sale 时传递客户的信息,然后污染 new_sales 表单。
所以我在索引客户端视图中有这个代码:
<%= 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 %>
在我的应用程序助手中,我有:
def sale_button
@sale_button = client
end
在我的客户和销售控制员中,我有这样一行:
helper_method :sale_button
在我的新销售视图中,我有
<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 %>
我不知道我是否以正确的方式做这件事,或者我是否缺少某些东西,欢迎提供任何帮助或意见。
我正在使用 Rails 3.2 顺便说一句
销售总监
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
路线.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"
耙路线
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
销售 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' %>
在 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>
具有您建议的修改的新销售视图。
<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>