Reputation: 3048
I have a form with hidden fields which gets values from parameters. When I first go to the form page my url looks like that:
http://localhost:3000/requests/new?parent_request_id=1&patient_id=1
so I have parent_request_id and patient_id parameters. And my hidden_fields get these values.
When user submits the form and in case if validation is not passed user is being returned to the same page with errors messages. The problem is that those parameters in url disappear so hidden fields are empty.
How can I save those parameters. Or is there any better way of sending variables between views?
EDIT: My controller code:
# requests_controller.rb
# POST /requests
# POST /requests.json
def create
@request = Request.new(request_params)
respond_to do |format|
if @request.save
format.html { redirect_to @request, notice: 'Request was successfully created.' }
format.json { render :show, status: :created, location: @request }
else
format.html { render :new }
format.json { render json: @request.errors, status: :unprocessable_entity }
end
end
end
private
# Never trust parameters from the scary internet, only allow the white list through.
def request_params
params.require(:request).permit(:description, :diagnos, :payed, :price, :doctor_id, :patient_id, :parent_request_id)
end
and my form:
<div class="form">
<h1>Данные по обращению</h1>
<%= form_for(@request) do |f| %>
<%= render 'layouts/error_messages', object: @request %>
<%= f.hidden_field :parent_request_id, value: params[:parent_request_id] %>
<%= f.hidden_field :patient_id, value: params[:patient_id] %>
<% if can? :manage, :all %>
<div class="field">
<%= f.label 'Лечащий врач' %>
<%= f.collection_select(:doctor_id, User.where(role:'doctor'),:id,:name,{include_blank:'-не выбрано-'}, class:'select2') %>
</div><br>
<% end %>
<div class="field">
<%= f.label 'Описание' %><br>
<%= f.text_field :description %>
</div>
<div class="field">
<%= f.label 'Первичный диагноз' %><br>
<%= f.text_field :diagnos %>
</div>
<div class="field">
<%= f.label 'Стоимость' %><br>
<%= f.number_field :price %>
</div>
<div class="actions">
<%= f.submit 'Добавить данные', class:'button tiny' %>
</div>
<% end %>
</div>
Upvotes: 3
Views: 442
Reputation: 10406
If you do this in your controller
class RequestsController
def new
@request = Request.new(parent_request_id: params[:parent_request_id],
patient_id: params[:patient_id])
end
def create
@request = Request.new(request_params)
respond_to do |format|
if @request.save
format.html { redirect_to @request, notice: 'Request was successfully created.' }
format.json { render :show, status: :created, location: @request }
else
format.html { render :new }
format.json { render json: @request.errors, status: :unprocessable_entity }
end
end
end
end
And have your view as simply
<%= form_for(@request) do |f| %>
<%= render 'layouts/error_messages', object: @request %>
<%= f.hidden_field :parent_request_id %>
<%= f.hidden_field :patient_id %>
<% end %>
You will have bound the attributes to the request object in the new action, so they'll be there if the validation fails and won't be set to the parameters which would be nil.
Upvotes: 2