如何獲取在我的視圖中呈現活動記錄列的默認值?在Rails視圖中顯示活動記錄默認值
我已經在first_lists
表中創建一個默認值的content:string
列:
create_table "first_items", force: :cascade do |t|
t.string "content", default: "Add a task"
t.integer "list_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
然後在first_items_controller.rb:
class FirstItemsController < ApplicationController
def update
@item = FirstItem.find(params[:id])
if @item.update(item_params)
render
else
@list = @item.list
render 'list/show'
end
end
private
def item_params
params.require(:first_item).permit(:content)
end
end
然後我試圖渲染視圖作爲部分從名單/顯示頁面:
<section class="todoapp">
<header class="header">
<h1 id="list-h1"><%= @list.title %></h1>
<!-- <h1 id="list-h1"><%#= @list.name %></h1> -->
</header>
<section class="main">
<ul class="todo-list">
<%= render @list.first_items %>
<%= render @list.second_items %>
<%= render @list.third_items %>
<%= render @list.fourth_items %>
</ul>
<button type="submit" class="btn btn-warning btn-lg" id="save-button">Save</button>
</section>
</section>
_firs t_items.html.erb
<li class="item" data-id-first="<%= first_item.id %>">
<div class="panel panel-danger box-shadow--8dp">
<div class="panel-heading">
<h5 class="panel-header-title">Mission Critical Task One</h5>
</div>
<div class="panel-body">
<%= form_for(first_item, :remote => true) do |f| %>
<%= f.text_field :content, :class => "edit form-control", :id => "first-form" %>
<% end %>
</div>
<div class="panel-footer"><input class="toggle panel-title" id="form-input" type="checkbox" unchecked=""> Complete</div>
</div>
</li>
當我進入`lists /:id'頁面時,頁面上沒有任何東西在呈現。我希望我的默認值可以填寫在表單中。
使用此設置,我可以得到所需的輸出,如果我進入控制檯並手動創建:content
。有沒有什麼辦法用默認列字符串實例化這些部分?
編輯 這裏的lists_controller.rb
class ListsController < ApplicationController
def index
@lists = List.all
@list = List.new
end
def create
@list = List.new(list_params)
if @list.save
if request.xhr?
render :layout => false
else
redirect_to root_path
end
else
@lists = List.all
render :index
end
end
def show
@list = List.find(params[:id])
end
private
def list_params
params.require(:list).permit(:name)
end
end
感謝似乎而不是已經奏效。我甚至嘗試過'價值:「添加任務」'。請注意,您的答案中存在拼寫錯誤; 'context'應該是'內容';) – BillyBib