控制器中的自定义操作方法,但link_to仍在拉动create方法

时间:2015-04-24 03:02:07

标签: ruby-on-rails ruby ruby-on-rails-3

我在帐户控制器中创建了存款和取款方法,并且我已经配置了路线。我不知道我做错了什么,我得到这个错误“缺少模板帐户/创建,应用程序/创建与{:locale => [:en],:formats => [:html] ,: variants => [] ,: handlers => [:erb,:builder,:raw,:ruby,:coffee,:jbuilder]}。搜索:   *“c:/ Users / geluna / Documents / Aptana Studio 3 Workspace / Luna-Kim-Kashkin / app / views”   *“c:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/devise-3.4.1/app/views”

我的帐户控制器看起来像这样:

     class AccountsController < ApplicationController
     # before_action :set_account [:show, :edit, :update, :destroy]
     before_filter :authenticate_user!

      respond_to :html

 def index
   # @accounts = Account.all
   @accounts = Account.where(id:current_user.id)
   if current_user.admin?
       @accounts = Account.all
   else
      @accounts = Account.where(email:current_user.email)
   end
       respond_with(@accounts)
   end

   def show
    if current_user.admin?
       @accounts = Account.all
    else
       @accounts = Account.where(email:current_user.email)
    end
        respond_with(@account)
    end

   def new
     @account = Account.new
     respond_with(@account)
   end

   def edit
     @accounts = Account.all
   end

   def create
     #@account = Account.new(account_params)
     # @account.save
     # respond_with(@account)
      end

     def update
      @account.update(account_params)
      respond_with(@account)
      end

     def destroy
        @account.destroy
        respond_with(@account)
     end

     def withdrawl
       @account = Account.new(account_params)
       @account.email = current_user.email
       @account.user_id = current_user.id
     end

    def deposit
       @account = Account.new(account_params)
       @account.email = current_user.email
       @account.user_id = current_user.id
       respond_to do |format|
       @account.save
    end
       redirect_to :root
     end 


   private
     def set_account
     #@accounts = Account.where(id:current_user.id)
      @account = Account.find(params[:id])
    end

    def account_params
        # params[:account]
        params.require(:account).permit(:created_at, :email, :credit,   
        :debit, :acctbal, :depotype)
         end 

我的页面链接转到新页面,其中有一个导入“新”页面的附加按钮,您可以添加浮动值并按下提交按钮,它将添加资金到帐户并显示在上一个页面。                            

上市帐户查询

          <h2>Your Account information</h2>
       <table border="3">
        <table class="table table-striped">
        <thead>
            <tr>
                <th>Date</th>
                <th>Credit</th>
                <th>Debit</th>
                <th>Account Balance</th>
            </tr>
            </thead>
            <tbody>      
             <% @accounts.each do |account| %>   
                <tr>
                    <td><%= account.created_at %></td>
                    <td><b><font color="green">
             <%=number_to_currency(account.credit)%></b></td>       
                    <td><b><font color="red">
             <%=number_to_currency(account.debit)%></font></b></td> 
                    <td><b><%= number_to_currency(account.acctbal)%></b>    
              </td>     
                </tr>
                 <% end %>
                <tbody>     
            </table>
            </table>
            <%= link_to "Add Funds", depsoit_post_path(@post),
              method: :post, action: :deposit, :class => "btn btn-primary 
              btn-sm" %>

            <table>
            <thead>
             <tr>
             <th colspan="3"></th>
             </tr>
             </thead>
             <% if can? :manage, Users%>
             <tbody>
              <% @accounts.each do |account| %>
                <tr>
              <td><%= link_to 'Show', account %></td>
                 <td><%= link_to 'Edit', edit_account_path(account) %></td>
               <td><%= link_to 'Destroy', account, method: :delete, data: 
                {      confirm: 'Are you sure?' } %></td>
               </tr>
               <% end %>
                </tbody>
                </table>

                <br>

                 </div>
                 <% end %>

我的routes.rb文件                Rails.application.routes.draw做

           devise_for :users
           get 'admin' => 'admin#index'
           get 'users/index'
           get 'accounts/index'

           get 'accounts/show'

           #get 'accounts/show'


           # resources :accounts

              resources :students
               #controller :sessions do
              #  get 'login' => :new
              #  post 'login' => :create
              #  delete 'logout' => :destroy
              #end

               root 'store#index', as: 'store'


             #get 'sessions/create'

             #get 'sessions/destroy'

              #resources :users

             resources :orders

             resources :line_items

               resources :carts

                get 'store/index'

             resources :menus
             resources :users

             resources :accounts do
             collection do
             post 'deposit', :action => :deposit
             post 'withdrawl', :action => :withdrawl

             end
             end

2 个答案:

答案 0 :(得分:0)

您的路线看起来有误,请将collection更改为member

resources :accounts do
  member do
     post 'deposit', :action => :deposit
     post 'withdrawl', :action => :withdrawl
  end
end

选中depsoit_post_path

,然后解决拼写错误的路径rake routes

答案 1 :(得分:0)

你的路线和路径似乎错了尝试:

resources :accounts do
    collection do
        post :deposit
        post :withdrawl
    end
end

我无法理解为什么你需要@post作为你的link_to的参数,我不知道你在该变量中使用的是什么,但对于你不需要它的集合,如果你想在请求中使用:id param,然后将上面的代码更改为'member'而不是collection,它将自动从@post变量中的任何对象获取id列。

并且使用上面的代码,如果你输入你的终端rake路线'你会发现你有一条路径:

deposit_accounts_path

deposit_account_path

取决于您使用的是集合还是成员