來做好可以為 girb 編輯 level_cd 欄位的 view
app/views/girbs/edit.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 :level, "Level:", class: "col-md-3 control-label" %>
<div class="col-md-7">
<%= f.collection_select :level_cd, Girb.all.select(:level_cd).distinct, :level_cd, :level, include_blank: true %>
</div>
</div>
<div class="form-group">
<%= f.label :like, "Like:", class: "col-md-3 control-label" %>
<div class="col-md-7">
<%= f.number_field :like, class: "form-control" %>
</div>
</div>
<div class="form-group">
<div class="col-md-3 control-label">
</div>
<div class="col-md-7">
<%= f.submit "Submit", class: "btn btn-default btn-primary" %>
</div>
</div>
<% end %>
</div>
app/views/girbs/show.html.erb
<div class="container">
<h2>Show Detail:</h2>
<%= 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 :level_cd, "Level:", class: "col-md-3 control-label" %>
<div class="col-md-7">
<%= f.text_field :level, 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>
<% 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 %>
<% if @girb.comments.all.count != 0 %>
<h2>Comments:</h2>
<% end %>
<% @girb.comments.each do |comment| %>
<% if comment.id != nil %>
<div class="panel panel-default">
留言者:<%= comment.commenter %><p>
內容:<%= comment.body %>
</div>
<% end %>
<% end %>
</div>
app/controllers/girbs_controller.rb
class GirbsController < ApplicationController
def index
@tab = params[:tab]
if params[:search]
@girbs = Girb.search(params[:search]).order("name DESC")
else
if @tab == "asc"
@girbs = Girb.all.order("name ASC")
@tab = "asc"
elsif @tab == "desc"
@girbs = Girb.all.order("name DESC")
else
@girbs = Girb.all
end
end
end
def label_fun_level
"self.level"
end
def like_asc
@at_like = false
@girbs = Girb.all.order("like ASC")
render :index
end
def like_desc
@at_like = true
@girbs = Girb.all.order("like DESC")
render :index
end
def like
@girb = Girb.find(params[:id])
@girb.like += 1;
@girb.save
# redirect_to(girbs_path)
redirect_to :back
end
def on_click_bang
@girb = Girb.find(params[:id])
@girb.bangs.create
# redirect_to(girbs_path)
redirect_to :back
end
def new
@girb = Girb.new
end
def create
@girb = Girb.new(girb_params)
if @girb.save
redirect_to(girbs_path)
else
render :new
end
end
def show
@girb = Girb.find(params[:id])
# @comment = @girb.comments.build
end
def edit
@girb = Girb.find(params[:id])
end
def update
@girb = Girb.find(params[:id])
if @girb.update(girb_params)
redirect_to girbs_path
else
render :edit
end
end
def destroy
@girb = Girb.find(params[:id])
@girb.destroy
redirect_to girbs_path
end
private
def girb_params
params.require(:girb).permit(:name, :email, :like, :level_cd)
end
end
OK 的話,現在應該可以成功的編輯 level_cd 這個欄位了
這邊注意一下,雖然編輯的是 level_cd
但呈現面是 level