I have a relationship where a merchant can have multiple payments. I am 
posting payments to a merchant and there is a validation error. Payment 
object does have the values retained. Can some one help me fix the issue?

View Code->

<%= @merchant.name %>

<%= form_for([@merchant, @merchant.payments.build]) do |f| %>

<% if @payment.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@payment.errors.count, "error") %> prohibited this 
payment from being saved:</h2>

<ul>
<% @payment.errors.full_messages.each do |msg| %>
<li>
<%= msg %>
</li>
<% end %>
</ul>
</div>
<% end %>

test
// Prints the values correctly
<%= @payment.credit_card_number %> 
<%= @payment.zip %>
<%= @payment.country %>

<div class="field">
<%= f.label :credit_card_number   %>
<br />
<%= f.text_field :credit_card_number , :autocomplete => "off" %>
</div>
<div class="field">
<%= f.label :address_line_2 %>
<br />
<%= f.text_field :address_line_2 %>
</div>
<div class="field">
<%= f.label :city %>
<br />
<%= f.text_field :city %>
</div>
<div class="field">
<%= f.label :zip %>
<br />
<%= f.text_field :zip %>
</div>
<div class="field">
<%= f.label :country %>
<br />
<%= f.text_field :country %>
</div>

<div class="actions">
<%= f.submit %>
</div>
<% end %>

Controller code->

class PaymentsController < ApplicationController
  # GET /merchants/1
  # GET /merchants/1.json
  def new
    @payment = Payment.new
    @merchant = Merchant.find(params[:merchant_id])
    respond_to do |format|
      format.html # show.html.erb
    end
  end

  def index

    if params[:merchant_id]
      @payments =  Merchant.find(params[:merchant_id]).payments
    else
      @payments = Payment.all
    end

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @merchants }
    end
  end

  def create
    @merchant = Merchant.find(params[:merchant_id])
    @payment = @merchant.payments.create(params[:payment])

    respond_to do |format|
      if @merchant.save
        format.html {redirect_to merchants_path}
      else
        format.html { render action: "new" }

      end
    end

  end

end

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msg/rubyonrails-talk/-/PTxFOGJob9sJ.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to