2016-06-12 12 views
0

単純なフォームからtext_inputを取得しようとしています。 paramsを呼び出すときしかし、事はさ[:PARAM_NAME]:私はここに必要なのは、文字列「マラケシュ」であることを知ってフォームのパラメータがテキストの代わりにjsonを返す

{"{: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

アム初心者でいますが、ネストされたとして、あなたがのparamsのようなものを呼び出すはずの、確実である[:がvoiture]を[:PARAM_NAME] – Maxence

+0

私はありません入力について話しています実際にヴォイチュアに関連しています。例:<%= text_field:nom%>以外<%= f.text_field:nom%> – Remis07

+0

text_field_tagをtext_field_tagに置き換えてみてください – Maxence

答えて

0

あなたは

f.text_field :nom 
をやるべき

しかし、あなたがやっているすべては、あなたがこの正しい方法を行っている場合は、あまりにも確認

text_field :nom 

されていませんが、あなたがこのnested forms

を見てみる必要がありますそして、あなたは、ネストされたフォームを保存しようとしている場合は、あなたの場合、私はかなり単純なケースのためにそれをしています。ここaccepts_nested_attributes_forclient.rb

class Client < ActiveRecord::Base 
    has_many :reservations 
    accepts_nested_attributes_for :reservations 
end 

をメモで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 

と私の_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 %> 
+0

こんにちは@saad、私はあなたの助けに感謝します。それはもっと複雑です。 PLASEはhttp://stackoverflow.com/questions/37779041/trying-to-make-a-form-using-nested-attributesを見てください。再度、感謝します ! – Remis07

関連する問題