Bonjour,

Je transmet à mon controller "monitoring" un objet "product" via son ID.
Jusque là tout va bien, je peux afficher dans ma vue "new" le nom du produit.

Par contre quand je veux créer l'objet "monitoring" (relation belongs_to product) mon id_product dans ma base de donnée est égal à 0...

Et du coup je ne vois pas trop comment faire pour que mon create récupère lui aussi cet id...

Voici mon code :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 
# GET /monitorings/new
# GET /monitorings/new.json
def new
    @monitoring = Monitoring.new
    @product = Product.find(params[:id])
 
    @monitoring.id_product = @product.id
 
    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @monitoring }
    end
end
 
# POST /monitorings
# POST /monitorings.json
 
def create
    @monitoring = Monitoring.new(params[:monitoring])
 
    respond_to do |format|
      if @monitoring.save
        format.html { redirect_to stock_index_url, notice: 'Quantité modifiée' }
        format.json { render json: stock_index_url, status: :created, location: @monitoring }
      else
        format.html { render action: "new" }
        format.json { render json: @monitoring.errors, status: :unprocessable_entity }
      end
    end
end
Merci pour le coup de main