【问题标题】:custom action method in controller but link_to is still pulling create method控制器中的自定义操作方法,但 link_to 仍在拉创建方法
【发布时间】:2015-04-24 03:02:07
【问题描述】:

我已经在账户控制器中创建了一个存款和取款方法,并且我已经配置了路线。我不知道我做错了什么,我收到此错误“缺少模板帐户/创建,应用程序/使用 {:locale=>[:en], :formats=>[:html], :variants=>[] 创建, :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}。 * "c:/Users/geluna/Documents/Aptana Studio 3 工作区/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 

我的页面带有指向新页面的链接,该页面有一个 addfunds 按钮,该按钮通向“新”页面,您可以添加一个浮动值并按下提交按钮,它将向帐户添加资金并显示在上一个页 。

上市账户查询

          <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

【问题讨论】:

  • 在您看来,= link_to "Add Funds", deposit_post_path(@post) 拼写错误。另外,试试deposit_path, method: :post, action: :deposit

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


【解决方案1】:

您的路线看起来不对,请将collection 更改为member

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

并通过检查 rake routes 来解决拼写错误的路径 depsoit_post_path

【讨论】:

  • 更改了 link_to " "btn btn-primary btn-sm" %>
  • 也如上所述更改了到成员的路线,我现在得到了这个,没有路线匹配 {:action=>"deposit", :controller=>"accounts"} 缺少必需的键:[:id] . rake 路由说 URI 模式是 deposit_account POST /accounts/:id/deposit(.:format) accounts#deposit .....似乎需要一个 :id ???
  • 需要,给你的账户实例,比如deposit_account_path(account)
【解决方案2】:

您的路线和路径似乎错误尝试:

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

我不明白为什么你需要 @post 作为你的 link_to 的参数,我不知道你在那个变量中使用了什么,但是对于一个你不需要它的集合,如果你想要一个请求中的 :id 参数,然后将上面的代码更改为“成员”而不是集合,它将自动从@post 变量中的任何对象中获取 id 列。

使用上面的代码,如果您输入终端 rake routes',您应该会发现您有一条类似于以下内容的路径:

deposit_accounts_path

deposit_account_path

取决于你是使用collection还是member

【讨论】:

  • 我也在上面的评论中添加了一些信息。但我重新做了 link_to 以尝试遵循 rake 路线要求 "btn btn-primary btn-sm" %>但它是说 #:0x54ded98> 的未定义局部变量或方法“用户” ....这是因为它找不到帐户吗?或 account_id ?我将如何解决这个问题我在顶部的accounts.rb文件中有belong_to:user,在user.rb文件中有has_many:accounts。
  • 你能给我完整的错误信息吗?我看不到您在代码中的任何地方直接引用 .user,但我看到您有一个 before_filter :authenticate_user!在您的控制器中,我假设它来自超类,我感觉它可能来自那里?
  • undefined local variable or method `account' for #:0x573de60> 这是我得到的错误及其对 link_to 按钮的引用。我在索引方法中声明了@accounts.. 我不知道它是否与它在该特定参数中指向的变量相同
  • 哦,是的,如果您查看视图文件,您的所有其他 link_to 都在每个帐户的循环内,但是这个 link_to 浮动在任何范围或循环之外的中间已声明要引用的“帐户”变量
  • 说用户出现的错误是因为我更改了“deposit_account_path(accounts_id:user)” 未声明的帐户变量是当我将帐户而不是用户放入其中时?有什么想法吗?
猜你喜欢
  • 2011-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-02
  • 2012-06-26
  • 1970-01-01
  • 2014-04-07
相关资源
最近更新 更多