2
,我有以下設置在扶手:未定義的方法構建4的has_many協會
Document has_many Sections
Section belongs_to Document
該科的形式在文件/顯示視圖完成......這個動作文檔控制器:
def show
@document = current_user.documents.find(params[:id])
@section = Section.new if logged_in?
end
文檔中的部分形式/顯示如下:
<%= form_for(@section) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_area :content, placeholder: "Compose new section..." %>
</div>
<%= hidden_field_tag :document_id, @document.id %>
<%= f.submit "Post", class: "btn btn-primary" %>
<% end %>
在那裏你可以看到一個hidde n_field_tag正在發送DOCUMENT_ID
的sections_controller如下:
class SectionsController < ApplicationController
before_action :logged_in_user, only: [:create, :destroy, :show, :index]
def create
@document = Document.find(params[:document_id])
@section = @document.build(section_params)
if @section.save
flash[:success] = "Section created!"
redirect_to user_path(current_user)
else
render 'static_pages/home'
end
end
def destroy
end
def index
end
private
def section_params
params.require(:section).permit(:content)
end
end
我碰到下面的錯誤,我一直沒能解決。
**NoMethodError (undefined method `build' for #<Document:0x00000004e48640>):
app/controllers/sections_controller.rb:6:in `create'**
我相信它一定是簡單的東西我忽略但似乎無法找到它。任何幫助,將不勝感激:
感謝您的快速回復,非常感謝,它的工作:) – Zakoff 2014-11-01 20:30:54
我必須等待超過10分鐘,然後才能以我的業力/積分水平接受您的回答。我會回來接受答案:) – Zakoff 2014-11-01 20:31:33
@Zakoff我正在找一份兼職RoR工作,你有什麼可以提供的嗎? – 2015-08-05 09:45:35