我试图从一个简单的表单中检索一些text_input。但事情是在调用params [:param_name]时,我得到了类似的东西:
{"{:readonly=>\"true\", :placeholder=>\"Ville\", :class=>\"form-control\"}"=>"marrakech"}
知道我所需要的只是字符串'马拉喀什' !
以下是表格:
<%= form_for([@voiture, @voiture.reservations.new]) do |f| %>
<div class="row">
<div class="col-md-12 price_tag">
<span><%= @voiture.prix %>Dhs</span>
<span class="pull-right">Par jour</span>
</div>
</div>
<div class="row">
<div class="col-md-6">
<label>Nom</label>
<%= text_field :nom, readonly: 'true', placeholder: 'Nom', class: 'form-control' %>
</div>
<div class="col-md-6">
<label>Prenom</label>
<%= text_field :prenom, readonly: 'true', placeholder: 'Prenom', class: 'form-control', disabled: 'true' %>
</div>
</div>
<div class="row">
<div class="col-md-6">
<label>CIN</label>
<%= text_field :cin, readonly: 'true', placeholder: 'CIN', class: 'form-control' %>
</div>
<div class="col-md-6">
<label>Age</label>
<%= text_field :age, readonly: 'true', placeholder: 'Age', class: 'form-control', disabled: 'true' %>
</div>
</div>
<div class="row">
<div class="col-md-6">
<label>Ville</label>
<%= text_field :ville, readonly: 'true', placeholder: 'Ville', class: 'form-control' %>
</div>
<div class="col-md-6">
<label>Télephone</label>
<%= text_field :telephone, readonly: 'true', placeholder: 'Telephone', class: 'form-control', disabled: 'true' %>
</div>
</div>
<div class="row">
<div class="col-md-6">
<label>Email</label>
<%= text_field :email, readonly: 'true', placeholder: 'Email', class: 'form-control' %>
</div>
</div>
<div class="row">
<div class="col-md-6">
<label>Check In</label>
<%= f.text_field :start_date, readonly: 'true', placeholder: 'Start Date', class: 'form-control' %>
</div>
<div class="col-md-6">
<label>Check Out</label>
<%= f.text_field :end_date, readonly: 'true', placeholder: 'End Date', class: 'form-control', disabled: 'true' %>
</div>
</div>
<%= f.hidden_field :voiture_id, value: @voiture.id %>
<%= f.hidden_field :prix, value: @voiture.prix %>
<%= f.hidden_field :total, id: 'reservation_total' %>
<h4><span id="message"></span></h4>
<div id="preview" style="display: none">
<table class="reservation-table" >
<tbody>
<tr>
<td>Day(s)</td>
<td><span id="reservation_days"></span></td>
</tr>
<tr>
<td>Total</td>
<td><span id="reservation_sum"></span>Dhs</td>
</tr>
</tbody>
</table>
<br>
</div>
<br>
<%= f.submit "Book Now", id:"btn_book", class: "btn btn-primary wide", disabled: 'true' %>
控制器:
def create
@client = Client.create(nom: params[:nom],prenom: params[:prenom],cin: params[:cin],age: params[:age],ville: params[:ville],telephone: params[:telephone],email: params[:email])
@reservation = Reservation.new(reservation_params)
@reservation.client = @client
if @reservation.save
redirect_to @reservation.voiture, notice: "Votre reservation a bien ete cree"
else
redirect_to @reservation.voiture, notice: "Erreur"
end
end
答案 0 :(得分:0)
你应该这样做
f.text_field :nom
但你所做的只是
text_field :nom
不确定你是否以正确的方式这样做,但你应该看看这个nested forms
如果您正在尝试保存嵌套表单,那么对于您的情况,我已经完成了相当简单的情况。
clients_controller.rb
中的
def new
@client = Client.new
@client.reservations.build
end
def create
@client = Client.new(client_params)
if @client.save!
render :show, id: @client
else
render :new
end
end
private
def client_params
params.require(:client).permit(:name, reservations_attributes: [:start_date, :end_date])
end
client.rb
中的
class Client < ActiveRecord::Base
has_many :reservations
accepts_nested_attributes_for :reservations
end
请注意accepts_nested_attributes_for
此处。
并在我的_form.html.erb
<%= form_for(@client) do |f| %>
<%= f.text_field :name %>
<%= f.fields_for :reservations do |reservation_form| %>
<%= reservation_form.text_field :start_date %>
<%= reservation_form.text_field :end_date %>
<% end %>
<%= f.submit %>
<% end %>