0

My Rails 애플리케이션에는 두 가지 모델이 있습니다. 위치 및 게시물, 위치는 많은 게시물이 있습니다. 사용하고 있습니다. 가계 보석입니다.게시물 및 위치 조상 보석과의 연관

class Post < ActiveRecord::Base 
belongs_to :location, :counter_cache => true 
end 

class Location < ActiveRecord::Base 
include Tree 
has_ancestry :cache_depth => true 
has_many :posts 
end 

내 포스트 컨트롤러

나는 위치 _form.html.erb

<%= form_for(@post) do |f| %> 
<% if @post.errors.any? %> 
<div id="error_explanation"> 
    <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved: </h2> 

    <ul> 
    <% @post.errors.full_messages.each do |msg| %> 
    <li><%= msg %></li> 
    <% end %> 
    </ul> 
</div> 
<% end %> 

<div class="field"> 
<%= f.label :name %><br /> 
<%= f.text_field :name %> 
</div> 

<%= select :location_id, Location.all.at_depth(4) { |l| [ l.name, l.id ] } %> 

<div class="actions"> 
<%= f.submit %> 
</div> 

브라우저 쇼 오류에 속해있는 새로운 포스트를 만들 생각하면 전시 인 메시지 울부 짖는 소리

ArgumentError in Posts#new 
+0

정확한 오류 메시지를 게시하십시오

는 또한 컨트롤러의 :location_id 매개 변수를 허용 목록에 추가해야합니다. – Pavan

+0

** Posts # new ** <% = select : location_id, Location.all.at_depth (4) {| l | [l.name, l.id]} %> –

+0

** 포스트 컨트롤러 **에 문제가 있습니까? _def new_ –

답변

0

이 당신의 오류를 수정하지만 확실하지 :

는에 select 라인 변경 드롭 다운 작업을하려면 : 당신이 formbuilder을 원하기 때문이다

<%= f.select :location_id, Location.all.at_depth(4) { |l| [ l.name, l.id ] } %> 

f form 요소의 생성을 처리합니다.

def post_params 
    params.require(:post).permit(:name, :location_id) 
end 
+0

내 게시물 indexhtm.erb 게시물 위치를 표시하는 방법에 어떤 코드를 추가 하시겠습니까? **><% = post.location.name %> ** 작동하지 않는 경우 ** <% = post.category_id %> ** 표시 위치 이름이 어떻게 표시됩니까? –

+0

게시물 show.html.erb'<% = @ post.location.name %>'오류 ** NoMethodError in Posts # 표시 ** –

+0

도와 주시겠습니까? @zwippie –