1

I created a simple_form in my Ruby on Rails app. It's working fine except when there is an error in the form, and the form is reloaded, at this moment I lose the params in the url.

(I Already read Keep params after render Ruby on Rails and Rails 3 Render => New with Parameter)

Here is the controller code:

def new
  @parts = Part.where(id: params[:part_id])
  @part = params[:part_id]
  @traveller = Traveller.new
end

def create
  if @traveller.save
    redirect_to edit_part_path(:id => @traveller.part_id)
    flash[:notice] = t('alert.create')
  else
    @part = [:traveller][:part_id]
    @parts = Part.where(id: params[:part_id])
    render :action => "new", :part_id => @part
  end
end

And here is the view code

<%= simple_form_for @traveller, html: { class: "sigPad"} do |f| %>
  <%= f.input :name %>
  <%= f.input :surname %>
  <%= f.input :part_id , label: 'part',readonly: true, input_html: {id: 'part_id', value: params[:part_id] }%>
<% end %>

I check if the part_id is submited and it is.

My question is how can I keep the part_id in params when the page is reloaded. Note

anothermh
  • 9,815
  • 3
  • 33
  • 52
Tomas
  • 249
  • 3
  • 13

2 Answers2

0

I found the solution,

in action new:

@traveller.part_id = params[:part_id]
Tomas
  • 249
  • 3
  • 13
0

I fixed the same problem with a verification in controller action new:

Example :

if params[:part_id]
  @traveller.part_id = params[:part_id]
elsif params[:traveller][:part_id]
  @traveller.part_id = params[:traveller][:part_id]
end 

Because the user can fail one or many times in register

Nico JL
  • 409
  • 3
  • 6