iT邦幫忙

DAY 18
0

Rails. Really, I love study.系列 第 18

傳情。

  • 分享至 

  • xImage
  •  

"可以偷偷留言給我喔"

來做留言功能

對於每個 girb ,要可以有可以留言的功能
首先,新增一個留言的 model 以及 contoller

rails g controller comments





rails g model comment commenter:string body:text girb_id:integer





rake db:migrate

關聯模組

app/models/girb.rb

class Girb < ActiveRecord::Base

  has_many :bangs, dependent: :destroy
  has_many :comments, dependent: :destroy

  def self.search(query)
    where("name like ?", "%#{query}%")
  end

end





app/models/comment.rb

class Comment < ActiveRecord::Base
  belongs_to :girb
end





config/routes.rb

Rails.application.routes.draw do

  resources :girbs do
    resources :comments
    member do
      post :like, :on_click_bang
    end
    collection do
      get :like_asc
      get :like_desc
    end
  end
  
end

去更改原本 girbs/show.html.erb 的畫面,讓它可以留言

app/views/girbs/show.html.erb

<div class="container">
  <%= form_for @girb, :html => {:class => "form-horizontal center"} do |f| %>
  <div class="form-group">
    <%= f.label :name, "Name:", class: "col-md-3 control-label" %>
    <div class="col-md-7">
      <%= f.text_field :name, class: "form-control" %>
    </div>
  </div>
  <div class="form-group">
    <%= f.label :email, "Email:", class: "col-md-3 control-label" %>
    <div class="col-md-7">
      <%= f.text_field :email, class: "form-control" %>
    </div>
  </div>
  <div class="form-group">
    <%= f.label :like, "Like:", class: "col-md-3 control-label" %>
    <div class="col-md-7">
      <%= f.text_field :like, class: "form-control" %>
    </div>
  </div>
  <div class="form-group">
    <div class="col-md-3">
    </div>
    <div class="col-md-7">
    </div>
  </div>
  <% end %>
  <h2>Add a comment:</h2>
  <%= form_for([@girb, @girb.comments.build], :html => {:class => "form-horizontal center"} ) do |f2| %>
  <div class="form-group">
    <%= f2.label :commenter, "留言者:", class: "col-md-3 control-label" %>
    <div class="col-md-7">
      <%= f2.text_field :commenter, class: "form-control"  %>
    </div>
  </div>
  <div class="form-group">
    <%= f2.label :body, "內容:", class: "col-md-3 control-label" %>
    <div class="col-md-7">
      <%= f2.text_area :body, class: "form-control" %>
    </div>
  </div>
  <div class="form-group">
    <div class="col-md-3 control-label">
    </div>
    <div class="col-md-7">
      <%= f2.submit "留言", class: "btn btn-default btn-primary" %>
      <span class="pull-right">
      <%= link_to "back...", :back %></span>
    </div>
  </div>
  <% end %>
</div>





app/controllers/comments_controller.rb

class CommentsController < ApplicationController

  def create
    @girb = Girb.find(params[:girb_id])
    @comment = @girb.comments.create(comment_params)
    redirect_to girb_path(@girb)
  end

  private
  def comment_params
    params.require(:comment).permit(:commenter, :body)
  end
end


上一篇
人氣。
下一篇
足跡。
系列文
Rails. Really, I love study.30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言