Values not being being reflected on page after validation error but model has the values

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

<%= pluralize(@payment.errors.count, "error") %> prohibited this payment from being saved:

    <% @payment.errors.full_messages.each do |msg| %>
  • <%= msg %>
  • <% end %>
<% end %>

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

<%= f.label :credit_card_number %>
<%= f.text_field :credit_card_number , :autocomplete => "off" %>
<%= f.label :address_line_2 %>
<%= f.text_field :address_line_2 %>
<%= f.label :city %>
<%= f.text_field :city %>
<%= f.label :zip %>
<%= f.text_field :zip %>
<%= f.label :country %>
<%= f.text_field :country %>
<%= f.submit %>
<% 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

On Mon, Dec 10, 2012 at 1:53 PM, rubyrookie [email protected] wrote:

<% if @payment.errors.any? %>

<%= f.label :credit_card_number %>
<%= f.text_field :country %> # GET /merchants/1

end

  end
end

end

end

what error do you get?

No error . Validation error is populated but form does not retain
values.
Payment object has the values though.

On Mon, Dec 10, 2012 at 2:26 PM, rubyrookie [email protected] wrote:

No error . Validation error is populated but form does not retain values.
Payment object has the values though.

This is still confusing. What exactly do you want to do?

  • Show the validation errors on the form page?
  • Fix the payment process because you don’t know what validations are
    failing?
  • retain the values passed by the user when validations fail?

<%= msg %>
<%= @payment.country %>

Controller code-> end format.html # index.html.erb format.html {redirect_to merchants_path} --

Thank you very much. I will read up on the difference. Sorry should have
bolded my answers.

On Mon, Dec 10, 2012 at 3:16 PM, rubyrookie [email protected] wrote:

values. Payment object has the values though.

It would’ve helped if you somehow bolded your answers. anyway, you
should
be able to do that by
changing this line

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

to

<%= form_for([@merchant, @payment]) do |f| %>

Good luck!

<% @payment.errors.full_messages.****each do |msg| %>
<%= @payment.credit_card_number %>


<%= f.text_field :zip %>
<% end %>
format.html # show.html.erb

respond_to do |format|

end

visit my blog at http://jimlabs.heroku.com
For more options, visit https://groups.google.com/groups/opt_out.

On Mon, Dec 10, 2012 at 3:42 PM, rubyrookie [email protected] wrote:

Thank you very much. I will read up on the difference. Sorry should have
bolded my answers.

No worries. If you’re looking for the difference, @payment (as you
already
know) uses the same
record as the one you have in your controllers. If you use
@merchant.payments.build, you’re
creating a new payment record which does not contain the values
submitted
by the user :slight_smile:

It would’ve helped if you somehow bolded your answers. anyway, you should

View Code->

    // Prints the values correctly <%= f.label :address_line_2 %>
    respond_to do |format| end

.
-------------------------------------------------------------
.
-------------------------------------------------------------

For more options, visit https://groups.google.com/groups/opt_out.

On Mon, Dec 10, 2012 at 3:45 PM, Jim Ruther N. [email protected]
wrote:

already know) uses the same
record as the one you have in your controllers. If you use
@merchant.payments.build, you’re
creating a new payment record which does not contain the values submitted
by the user :slight_smile:

regarding your question on the field_error_proc. read this one and
please
don’t email users directly :slight_smile:

http://guides.rubyonrails.org/configuring.html#configuring-action-view

values. Payment object has the values though.
fixed.
<%= form_for([@merchant, @payment]) do |f| %>

am posting payments to a merchant and there is a validation error. Payment

<%= pluralize(@payment.errors.**coun****t, "error") %> <% end %> <%= f.text_field :credit_card_number , :autocomplete => "off" %>

def new
@payments = Merchant.find(params[:merchan**

  end

To unsubscribe from this group, send email to rubyonrails-ta…@**

To unsubscribe from this group, send email to rubyonrails-ta…@**

To unsubscribe from this group, send email to

visit my blog at http://jimlabs.heroku.com

thanks :slight_smile:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs