IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Ruby on Rails Discussion :

respond_to et fichier controleur


Sujet :

Ruby on Rails

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Avril 2008
    Messages
    79
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2008
    Messages : 79
    Points : 51
    Points
    51
    Par défaut respond_to et fichier controleur
    Salut,

    Je viens d'essayer le tutoriel http://www.netbeans.org/kb/60/ruby/getting-started.html en modifiant les champs (body et title =>nom, prenom, numero, adresse, cp, ville, tel, email) . Je commence a mieux comprendre le principe, la séparation des tâches entre controleur, modèle...

    Par contre, j'ai quelques problèmes avec "respond_to" (dans le fichier posts_controller.rb) . Je ne comprends pas bien le principe.

    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
     
     
    class PostsController < ApplicationController
      # GET /posts
      # GET /posts.xml
      def index
        @posts = Post.find(:all)
     
        respond_to do |format|
          format.html # index.html.erb
          format.xml  { render :xml => @posts }
        end
      end
     
      # GET /posts/1
      # GET /posts/1.xml
      def show
     
        #URL DE LA FORME /posts/id
        #Nom du modele.parametre(id)
        #alors => donne page
        #associer action truc...
        @post = Post.find(params[:id])
     
        respond_to do |format|
     
          format.html # show.html.erb
          format.xml  { render :xml => @post }
        end
      end
     
      # GET /posts/new
      # GET /posts/new.xml
      def new
        @post = Post.new
     
        respond_to do |format|
          format.html # new.html.erb
          format.xml  { render :xml => @post }
        end
      end
     
     
      # GET /posts/1/edit
      def edit
        @post = Post.find(params[:id])
      end
     
      # POST /posts
      # POST /posts.xml
      def create
        @post = Post.new(params[:post])
     
        respond_to do |format|
          if @post.save
            flash[:notice] = 'Post was successfully created.'
            format.html { redirect_to(@post) }
            format.xml  { render :xml => @post, :status => :created, :location => @post }
          else
            format.html { render :action => "new" }
            format.xml  { render :xml => @post.errors, :status => :unprocessable_entity }
          end
        end
      end
     
      # PUT /posts/1
      # PUT /posts/1.xml
      def update
        @post = Post.find(params[:id])
     
        respond_to do |format|
          if @post.update_attributes(params[:post])
            flash[:notice] = 'Post was successfully updated.'
            format.html { redirect_to(@post) }
            format.xml  { head :ok }
          else
            format.html { render :action => "edit" }
            format.xml  { render :xml => @post.errors, :status => :unprocessable_entity }
          end
        end
      end
     
      # DELETE /posts/1
      # DELETE /posts/1.xml
      def destroy
        @post = Post.find(params[:id])
        @post.destroy
     
        respond_to do |format|
          format.html { redirect_to(posts_url) }
          format.xml  { head :ok }
        end
      end
     
     
    end
    Du coup, dans ma page principale index.html, je ne comprends pas d'ou sortent le edit_post_path(post) et le new_post_path ?

    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
    <table>
      <tr>
        <th>Nom</th>
        <th>Prenom</th>
        <th>Numero</th>
        <th>Adresse</th>
        <th>Cp</th>
        <th>Ville</th>
        <th>Tel</th>
      </tr>
     
    <% for post in @posts %>
      <tr>
        <td><%=h post.nom %></td>
        <td><%=h post.prenom %></td>
        <td><%=h post.numero %></td>
        <td><%=h post.adresse %></td>
        <td><%=h post.cp %></td>
        <td><%=h post.ville %></td>
        <td><%=h post.tel %></td>
        <!-- reference a notre id-->
        <td><%= link_to 'Consulter', post %></td>
        <td><%= link_to 'Modifier', edit_post_path(post) %></td>
        <td><%= link_to 'Supprimer', post, :confirm => 'Etes vous sur ?', :method => :delete %></td>
      </tr>
    <% end %>
    </table>
     
    <br />
    <%= link_to 'Nouveau Message', new_post_path %>
    Si quelqu'un pouvait m'expliquer ... merci d'avance

  2. #2
    Membre éprouvé

    Profil pro
    Inscrit en
    Mai 2005
    Messages
    657
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2005
    Messages : 657
    Points : 910
    Points
    910
    Par défaut
    respond_to permet d'envoyer une réponse différente au client suivant le type mime (ou extension de fichier souvent en réalité) qu'il demande.

    Autant c'est sûrement très utile pour faire des webservices, autant le choix de le mettre dans le template du scaffold est stupide (enfin c'est mon point de vue).
    Tu peux tout simplement supprimer ce bloc (puisqu'il ne fait rien de spécial pour le html).


    Pour les méthode toto_path, ce sont des routes. En l'occurrence, ce sont les routes par défaut pour les controlleurs RESTful (ressources). Tu peux trouver la doc qui correspond ici et une cheatsheet qui résume efficacement ça ici.
    Toute la documentation Ruby on Rails : gotapi.com/rubyrails
    Mes articles :
    > HAML : langage de template pour Ruby on Rails

  3. #3
    Membre du Club
    Profil pro
    Inscrit en
    Avril 2008
    Messages
    79
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2008
    Messages : 79
    Points : 51
    Points
    51
    Par défaut
    Pour le "respond_to", je n'ai pas choisi. Le fichier a été généré et a utilisé "respond_to".

    Donc, concrètement, si je suis mon tutoriel http://www.stoneageblog.com/articles...ruby-on-rails/ , comment dois je procéder à partir de Génération automatique de l'application ? Admettons que je n'ai rien dans "controller", je dois générer un controleur associé à mon modèle "cd" ? Je suis un petit peu perdu ...

    Merci pour les infos concernant la partie path

  4. #4
    Membre éprouvé

    Profil pro
    Inscrit en
    Mai 2005
    Messages
    657
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2005
    Messages : 657
    Points : 910
    Points
    910
    Par défaut
    Ton tutoriel n'est pas a jour
    Depuis la version 2 de Rails le scaffold ne prend plus que le nom de la ressource en paramètre (autrement dit il genere le modèle et le controlleur en une seule fois).
    Toute la documentation Ruby on Rails : gotapi.com/rubyrails
    Mes articles :
    > HAML : langage de template pour Ruby on Rails

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Variables controleur dans un fichier externe
    Par allstar dans le forum AngularJS
    Réponses: 1
    Dernier message: 09/01/2014, 21h07
  2. Réponses: 8
    Dernier message: 02/11/2010, 16h24
  3. fichier mappé en mémoire
    Par WinBernardo dans le forum Delphi
    Réponses: 7
    Dernier message: 01/12/2006, 09h38
  4. Lire 1 bit d'un fichier en C
    Par Anonymous dans le forum C
    Réponses: 3
    Dernier message: 23/05/2002, 18h31
  5. Fichier PDOXUSRS.NET
    Par yannick dans le forum Paradox
    Réponses: 5
    Dernier message: 05/04/2002, 09h45

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo