如何将值从视图传递到另一个视图以在rails 3中填充另一个表单

时间:2012-05-07 22:56:33

标签: ruby-on-rails

如何将视图中的值传递到另一个视图以在rails 3中填充另一个表单?

我是rails的新手,我对如何实现这一点感到困惑,这是我的场景:

我生成了一个客户端和一个Sales脚手架,然后在客户端索引视图中,我放置了一个搜索文本字段,用于搜索客户端并在其下方呈现它们。每个客户端都有一个名为Sale的按钮,点击后它会将你重定向到new_sales_path,并自动用客户端的值填充表单的某些字段,例如:姓名,地址,身份证,电话号码等...... / p>

所以我想做的是: 我想在单击按钮销售时传递客户端的信息,然后对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 btw


销售控制员

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

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

在销售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>

2 个答案:

答案 0 :(得分:1)

仔细听,

以下链接“销售”应该在您的申请中的某处。

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

如果用户点击该链接,link_to方法会检查路径,并且您已将其提及为“new_sale_path”

“佣金路线”,

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

很明显,new_sale_path指的是sales#new(销售控制器,新行动)

如果你添加下划线路径,rails会处理这个问题。例如,edit_sale_path将执行销售控制器,编辑操作/方法。

完成此步骤后,由于销售控制器中的新操作已执行,因此rails将在app / views / sales中查找“new.html.erb”文件。

如果存在,则会在浏览器中显示/呈现该html.erb文件。

你应该理解上面的工作。

现在,让我们来具体。

单击该销售链接时,应用程序将使用以下内容到达sales#new action:client_id作为参数散列中的参数。

您可以按如下方式访问该参数:

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

你做到了。上面的语句将传递的:client_id作为输入,并使用Client模型和find方法从数据库中找到正确的客户端。

现在,实例变量使用与客户端表匹配的记录来设置:client_id。

实例变量是@client。

现在,在您的视图中,您可以使用实例变量@client,如下所示,您尚未完成,或者我没有看到。 app / views / sales / _form.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_for,

<% form_for @client do |client| %>

<% text_field_tag client.name %>
-
-
-

<% end %>

将有一个text_field,它将预先填充客户名称。

如果你什么都不懂,请告诉我。

如果你理解的东西很少而且弄不清楚,请告诉我。

如果你理解了一切,请告诉我。

感谢。

答案 1 :(得分:0)

将client_id作为参数传递给此链接,

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

确保new_sale_path点击销售控制器,新操作。

在新操作中,将client_id作为params [:client_id]访问并获取当前客户端记录并使用以下内容呈现表单,

SalesController

def new
 @client = Client.find(params[:client_id])
 -
 -
 -
 render 'new'
end

在你看来,new.html.erb,你必须写一个form_for @client as,

<% form_for @client do |client| %>

<% text_field_tag client.name %>
-
-
-

<% end %>

现在,我不确定我的语法。试着理解我在这里做了什么。

感谢。