-1
請您幫助理解爲什麼類別不能以正確的方式工作?所以,我看了不少了很多的手冊,並沒有找到如何解決它爲帖子添加類別 - 導軌
我創建遷移類別
class CreateCategories < ActiveRecord::Migration
def change
create_table :categories do |t|
t.string :name
t.text :description
t.integer :count
t.timestamps null: false
end
end
end
而對於職位增加了新的領域 - 類
的類別,然後將創建的模型
class Category < ActiveRecord::Base
has_many :posts
end
編輯崗位模型
class Post < ActiveRecord::Base
acts_as_ordered_taggable
belongs_to :category
validates :title, presence: true
validates :category, presence: true
..
end
創建的模板
<%= form_for @post do |f| %>
<p>
<%= f.label :title %> <br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :category %>
<%= f.select :category, Category.all.collect {|c| [c.name, c.name]} %>
</p>
<% end %>
編輯了一下後控制器
def update
@post = Post.find(params[:id])
if @post.update(params[:post].permit(:title, :thumbnail, :body, :description, :tag_list, :@post.category))
redirect_to @post
else
render 'edit'
end
end
只是爲了請求參數確定:你一個叫做'category_id'字段添加到你的'posts'表? (你提到'category',但它應該被稱爲'category_id') – nathanvda
謝謝,它解決了問題 – Antoni