Salut à tous, je voudrais mettre en place une liste select comme dans cet exemple :
http://calm-sierra-1168.herokuapp.com/welcome/index
J'ai donc suivi ce tutoriel : https://kernelgarden.wordpress.com/2014/02/26/dynamic-select-boxes-in-rails-4/
Et j'ai obtenu ceci :
app/assets/incidents_controller.rb :app/views/incidents/sous_categories.js.coffee
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128 # # Copyright (c) 2015 by Montesinos Jeremy. All Rights Reserved. # class IncidentsController < ApplicationController before_action :set_incident, only: [:show, :edit, :update, :destroy, :reply] # GET /incidents # GET /incidents.json def index @incidents = Incident.all end # GET /incidents/1 # GET /incidents/1.json def show end # GET /incidents/new def new @incident = Incident.new @categories = Category.all @sous_categories = SousCategory.where('category_id = ?', :category_id) end # GET /incidents/1/edit def edit end def sous_categories @sous_categories = SousCategory.where('category_id = ?', params[:id]) respond_to do |format| format.json { render json: @sous_categories } end end # POST /incidents # POST /incidents.json def create @incident = Incident.new(incident_params) @incident.update(evenement_type_user: 1) @incident.update(evenement_type_tech: 1) @incident.update(user_id: "#{current_user.id}") respond_to do |format| if @incident.save format.html { redirect_to @incident, notice: 'Incident crée.' } format.json { render :show, status: :created, location: @incident } else format.html { render :new } format.json { render json: @incident.errors, status: :unprocessable_entity } end end end def read_it @incident = Incident.find(params[:id]) if @incident.evenement_type_tech == 2 @incident.update(evenement_type_user: 3) @incident.update(evenement_type_tech: 3) end redirect_to incident_path(@incident) end def reject_it @incident = Incident.find(params[:id]) @incident.update(evenement_type_tech: 10) @incident.update(evenement_type_user: 10) redirect_to_back end def cloture_it @incident = Incident.find(params[:id]) if @incident.user_id == current_user.id @incident.update(evenement_type_user: 7) @incident.update(evenement_type_tech: 7) elsif @incident.tech_id == current_user.id @incident.update(evenement_type_user: 8) @incident.update(evenement_type_tech: 9) end redirect_to current_user end def redirect_to_back(default = '/') if !request.env['HTTP_REFERER'].blank? && request.env['HTTP_REFERER'] != request.env['REQUEST_URI'] redirect_to :back else redirect_to default end end # PATCH/PUT /incidents/1 # PATCH/PUT /incidents/1.json def update respond_to do |format| if @incident.update(incident_params) format.html { redirect_to @incident, notice: 'Incident mis à jour.' } format.json { render :show, status: :ok, location: @incident } else format.html { render :edit } format.json { render json: @incident.errors, status: :unprocessable_entity } end end end # DELETE /incidents/1 # DELETE /incidents/1.json def destroy @incident.destroy respond_to do |format| format.html { redirect_to incidents_url, notice: 'Incident supprimé.' } format.json { head :no_content } end end private # Use callbacks to share common setup or constraints between actions. def set_incident @incident = Incident.find(params[:id]) end # Never trust parameters from the scary internet, only allow the white list through. def incident_params params.require(:incident).permit(:content, :title, :user_id, :tech_id, :category_id, :sous_category_id) end end
config/routes.rb :
Code : Sélectionner tout - Visualiser dans une fenêtre à part :$("#incident_sous_category_id").empty().append("<%= escape_javascript(render(:partial => @sous_categories)) %>")
_form_new.html.erb :
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96 Rails.application.routes.draw do resources :categories get 'pages/home' resources :rights resources :fonctions resources :sessions, :only => [:new, :create, :destroy] resources :incidents do member do put :cloture_it put :reopen_it put :read_it put :reject_it get :cloture_it get :reopen_it get :read_it get :reject_it end resources :responses end resources :teches resources :users do member do get :allincidents end end get '/', :to => 'pages#home' get '/users', :to => 'users#index' get '/categories', :to => 'categories#index' get '/incidents', :to => 'incidents#index' get '/signup', :to => 'users#new' get '/signin', :to => 'sessions#new' get '/signout', :to => 'sessions#destroy' get '/rights', :to => 'rights#index' get '/fonctions', :to => 'fonctions#index' get 'incidents/sous_categories', as: 'sous_categories' get 'incidents/show' # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". # You can have the root of your site routed with "root" # root '#index' # Example of regular route: # get 'products/:id' => 'catalog#view' # Example of named route that can be invoked with purchase_url(id: product.id) # get 'products/:id/purchase' => 'catalog#purchase', as: :purchase # Example resource route (maps HTTP verbs to controller actions automatically): # resources :products # Example resource route with options: # resources :products do # member do # get 'short' # post 'toggle' # end # # collection do # get 'sold' # end # end # Example resource route with sub-resources: # resources :products do # resources :comments, :sales # resource :seller # end # Example resource route with more complex sub-resources: # resources :products do # resources :comments # resources :sales do # get 'recent', on: :collection # end # end # Example resource route with concerns: # concern :toggleable do # post 'toggle' # end # resources :posts, concerns: :toggleable # resources :photos, concerns: :toggleable # Example resource route within a namespace: # namespace :admin do # # Directs /admin/products/* to Admin::ProductsController # # (app/controllers/admin/products_controller.rb) # resources :products # end end
and app/assets/javascripts/incidents.coffee :
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
32
33
34
35
36
37
38 <%= form_for @incident, html: { multipart: true } do |f| %> <% @categories = Category.all %> <div class="field"> <%= f.label "Objet" %><br> <%= f.text_field :title %> <% if @incident.errors[:title].first != nil %> <div class="alert alert-danger"> <%= "Titre " + @incident.errors[:title].first%><br> </div> <% end %> </div> <div class="field"> <%= f.label "Description" %><br> <%= f.text_area :content %> <% if @incident.errors[:content].first != nil %> <div class="alert alert-danger"> <%= "Description " + @incident.errors[:content].first %><br> </div> <% end %> </div> <div class="field"> <%= f.label "Catégorie" %><br> <%= select_tag "incident[category_id]", options_for_select(@categories.collect{|category| [category.name.titleize, category.id]}, 1), {}%> <%= select_tag "incident[sous_category_id]", options_for_select(@sous_categories.collect{|sous_category| [sous_category.name.titleize, sous_category.id]}, 0), {} %> <br> </div> <div class="field" id="rdioBtn"> <%= f.label "Niveau d'urgence" %><br> </div> <div class="field"> <%= f.label "Images" %><br> <%= f.file_field :image %> </div> <div class="actions"> <%= f.submit "Envoyer"%> </div> <% end %>
Voyez vous quelque chose qui cloche dans mon code car je cherche depuis ce matin et je n'y arrive pas ... Merci beaucoup !
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 # Place all the behaviors and hooks related to the matching controller here. # All this logic will automatically be available in application.js. # You can use CoffeeScript in this file: http://coffeescript.org/ $ -> $(document).on 'change', '#incident_category_id', (evt) -> $.ajax 'sous_categories', type: 'GET' dataType: 'script' data: { category_id: $('#incident_sous_category_id').val() } error: (jqXHR, textStatus, errorThrown) -> console.log("AJAX Error: #{textStatus}, #{jqXHR}, #{errorThrown}") success: (data, textStatus, jqXHR) -> console.log("Dynamic category select OK!")
Partager