2012-05-07 14 views
1

ビューから別のビューに値を渡して、別のフォームをレール3に挿入するにはどうすればよいですか?その後、クライアント・インデックス・ビューで、私は、検索テキストフィールドを配置ビューから別のビューに値を渡して、別のフォームをレールに挿入する方法3

私はクライアントと販売足場を生成し、:

私はここで、これを実現する方法について混乱ビットをレールとIMに新しいですが、私のscenerioですクライアントを検索し、その下にレンダリングします。各クライアントにはSaleというボタンがあり、クリックするとnew_sales_pathにリダイレクトされ、名前、住所、ID、電話番号などのクライアントの値がフォームのフィールドに自動的に入力されます。

私がしたいことは次のとおりです。 ボタンセールをクリックしてからnew_salesフォームをポーリングするときにクライアントの情報を渡したいとします。

だから私は、インデックス・クライアントでこのコードを持っているが表示:私のクライアントと私はラインを持って販売コントローラで

def sale_button 
@sale_button = client 
end 

::私は自分のアプリケーションのヘルパーで

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

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

私は正しい方法で行っているのか、何か不足しているものがあれば、助けや歓迎を歓迎します。

Imを使用して、レール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 

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" 

すくい経路

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 
あなたが提案修正と

<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.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の

販売。

​​

答えて

1

注意深く、

次のリンク "Sale"はアプリケーションのどこかにあるはずです。ユーザーがパスする方法をチェックのlink_toそのリンクをクリックすると、あなたが "new_sale_path" あなた"すくいルート" から

としてそれを言及している場合は

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

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

それをnew_sale_pathがsalesを参照していることは明らかです#new(販売管理者、新しいアクション)

アンダースコアパスを追加すると、それを気にする。たとえば、edit_sale_pathはsales controller、edit action/methodを実行します。

salesコントローラの新しいアクションが実行されるので、railsはapp/views/sales内の "new.html.erb"ファイルを探します。

存在する場合、そのhtml.erbファイルがブラウザに表示/レンダリングされます。

あなたは上記の作業を理解する必要があります。

ここで、具体的に説明します。

この販売リンクをクリックすると、アプリケーションはsales#newactionに到達します。パラメータ:client_idはparamsハッシュのパラメータです。

あなたは、あなたがそれを行っている、次のようにそれは、ハッシュparamsは

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

にアクセスすることができます。上記のステートメントは、渡された:client_idを入力として受け取り、Clientモデルとfindメソッドを使用してデータベースから正しいクライアントを見つけます。

ここで、インスタンス変数には:client_idに一致するClientsテーブルのレコードが設定されています。

インスタンス変数は@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があります。

何かを理解できない場合は教えてください。

あなたが何かを理解していて、ほとんど何かを理解できなかった場合は、教えてください。

すべてを理解していれば教えてください。

ありがとうございました。

+0

オハイオ州オクラホマ、はい私はあなたが言っていることを理解していますが、私は自分自身を正しく説明していないかもしれない、私に試してみましょう。私は2つのテーブル、クライアントと売上高を持って、私が述べた販売ボタンは、クライアントのインデックスビュー "app/views/clients/index.html.erb"にあり、それはすべてのクライアントを表示し、各クライアントは販売ボタンを持っています。このボタンを押すと、ユーザーは新しい販売ビュー「app/views/sales/new.html.erb」にリダイレクトされ、これはうまく動作し、それを私にリダイレクトしてページを表示します。私はできないことは、特定のクライアントのIDを新しいセールスビューに渡すことです。私はそれを行う方法を知らないのです... – user1350443

+0

彼らは異なるコントローラ "app/controllers/clients_controller.erb"と "app/controllers /sales_controller.erb "と入力します。 販売ボタンが表示されているクライアントのインデックスビューを貼り付けて、私が何を話しているのか知っています。 – user1350443

+0

これを試してみてください。クライアントIDをこのリンクのパラメータとして渡しています。 <%= link_to "Sale"、{:controller => "sales"、:action => "new"、:client_id => client.id}、{:class => 'btn btn-mini'}%>今あなたのセールリンクとしてこれを使用してください。それをクリックして、ブラウザのアドレスバーにアクセスしたURLをクリックしてコピー&ペーストします。 – beck03076

0

new_sale_pathが販売コントローラ、新しいアクションをヒットすることを確認し、このリンクへのparamsとして

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

、などのclient_idを渡します。新しいアクションへのアクセスに

のparamsなどのclient_id [:のclient_id]と、あなたのビューで

SalesController 

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

を現在のクライアントレコードをフェッチし、次のようにそれとフォームをレンダリング、new.html.erb、あなたは以下のようにform_for @clientを書く必要があります。

<% form_for @client do |client| %> 

<% text_field_tag client.name %> 
- 
- 
- 

<% end %> 

ここで、私の構文についてはわかりません。私がここで何をしているのか理解しよう。

ありがとうございました。

+0

私はnew_sales_pathに私をリダイレクトしているときに私は次のエラーを受け取ります: IDなしでクライアントを見つけることができませんでした – user1350443

+0

してください、エラーを投稿してください。 – beck03076

+0

それはエラーでもあり、またそれは言う: app/controllers/sales_controller.rb:34: 'new ' – user1350443

関連する問題