mortymacs
mortymacs

Reputation: 3736

fill fields in rails on validation failed

When I submit my form , my validation works fine and shows the errors.

but my form elements didn't fill with the posted data that I can't edit theme.

this is my code:

controller:

  def new
    @row = MyModel.new
  end

  def create
    @row = MyModel.new params[:post].permit!
    if @row.save
      redirect_to @row
    else
      render :new
      #render :action => 'new'
    end
  end

new.html.erb:

<%=form_for :post,url: sms_webservices_path(@row) do |f| %>
   <%=f.label :name,t('name'),class: 'f1' %>
   <%=f.text_field :name,class: 'form-control' %>
   <%=f.submit 'add' %>
<% end %>

model validation:

validates :name, presence: true, uniqueness: true

Thanks

Upvotes: 0

Views: 331

Answers (1)

mdesantis
mdesantis

Reputation: 8517

This is because you are using form_for :post, which says to Rails to check for a @post instance variable, while you are using @row.

Just change your code to this and it should work:

<%= form_for @row, url: sms_webservices_path(@row) do |f| %>

Upvotes: 1

Related Questions