Rails: Form help -- supplying method with argument -


i relatively new rails , have attempted write small loan app user can make loan , app can track balances / etc. adding function loan model handles payments , have gotten far (draw attention make_payment(value) portion:

loan.rb

class loan < activerecord::base   belongs_to :lender, :class_name => 'user'   belongs_to :borrower, :class_name => 'user'   has_many :payments    default_scope -> { order(created_at: :desc) }    validates :amount, presence: true   validates :lender, presence: true   validates :borrower, presence: true    def make_payment(value)     remainder = amount - value     update_attribute(:amount, remainder)     return remainder   end end 

i can use correctly deduct value original loan amount, cannot seem make work using views , form. view follows:

app/views/loans/show.html.erb

<p id="notice"><%= notice %></p>  <div class="row">   <div class="col-md-2">     <ul>       <li><strong>lender</strong>: <%= @loan.lender.username %></li>       <li><strong>borrower</strong>: <%= @loan.borrower.username %></li>       <li><strong>amount remaining</strong>: <%= @loan.amount %></li>     </ul>     <section>       <div class="col-md-6 col-md-offset-3">         <%= form_for @loan |f| %>            <%= f.label :make_payment %>  <!--confusion -->           <%= f.number_field :value, class: 'form-control' %>  <!--confusion -->            <%= f.submit "submit payment", class: "btn btn-primary" %>         <% end %>       </div>     </section>   </div> </div>  <%= link_to 'edit', edit_loan_path(@loan) %> | <%= link_to 'back', loans_path %> 

when try reach page, error wrong number of arguments (0 1) makes complete sense, i'd value submitted in form argument (value) function make_payment(value). should use form_for @loan.make_payment |f| or else entirely? i'm sure simple fix, can't seem find answer i'm looking for.

a nice restful solution this:

resources :loans, shallow: :true   resources :payments, only: :create end 

class paymentscontroller < applicationcontroller    # post /loans/:loan_id/payments   def create     @loan = loan.find(params[:loan_id])     @loan.make_payment(payment)     if @load.save       redirect_to @loan, notice: "payment successful"     else       redirect_to @loan, alert: "payment count not processed"     end    end    private     def payment     require(:loan).require(:payment)[:payment]   end end  

and form this:

<%= form_for @loan, url: loan_payments_path(loan: @loan), method: :post |f| %>   <%= f.label :payment %>   <%= f.number_field :payment, class: 'form-control' %>   <%= f.submit "submit payment", class: "btn btn-primary" %> <% end %> 

Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -