3

저는 simple_form을 사용하고 있으며 분류 테이블을 사용하여 범주와 아티클 간의 연관성을 만들고 싶습니다.보호 된 속성을 대량 지정할 수 없습니다. category_ids

하지만이 오류가 있습니다 : 보호 속성을 지정할 수 없습니다 : category_ids. 응용 프로그램/컨트롤러/articles_controller.rb : 36 : '갱신'

articles_controller.rb

def update 
    @article = Article.find(params[:id]) 
     if @article.update_attributes(params[:article]) ---line with the problem 
     flash[:success] = "Статья обновлена" 
     redirect_to @article 
     else 
     render :edit 
     end 
end 

article.rb

has_many :categorizations 
has_many :categories, through: :categorizations 

category.rb에서

has_many :categorizations 
has_many :articles, through: :categorizations 
,515,

categorization.rb

belongs_to :article 
belongs_to :category 

article_id를 범주화 및 CATEGORY_ID 필드를 갖는다.

<%= f.association :categories %> 

만든

내 _form.html.erb

<%= simple_form_for @article, html: { class: "form-horizontal", multipart: true } do |f| %> 
    <%= f.error_notification %> 
    <%= f.input :title %> 
    <%= f.association :categories %> 
    <%= f.input :teaser %> 
    <%= f.input :body %> 
    <%= f.input :published %> 
<% if @article.published? %> 
    <%= f.button :submit, value: "Внести изменения" %> 
<% else %> 
    <%= f.button :submit, value: "Опубликовать" %> 
    <% end %> 
<% end %> 

답변

5

article.rb에 attr_accessible이 있습니까?

때문에 추가 할 경우 또한 ... 당신이 정말 모든 형태의이 원하는 만들

 attr_accessible :title, :category_ids 

를 추가 할 경우이 : 다음

@article = Article.new 
@article.assign_attributes({ :category_ids => [1,2], :title => 'hello' }) 
@article.category_ids # => [] 
@article.title # => 'hello' 

@article.assign_attributes({ :category_ids => [1,2], :title => 'hello' }, :as => :admin) 
@article.category_ids # => [1,2] 
@article.title # => 'hello' 
@article.save 

또는

attr_accessible :title, :category_ids, :as => :admin 

@article = Article.new({ :category_ids => [1,2], :title => 'hello' }) 
@article.category_ids # => [] 
@article.title # => 'hello' 

@article = Article.new({ :category_ids => [1,2], :title => 'hello' }, :as => :admin) 
@article.category_ids # => [1,2] 
@article.title # => 'hello' 
@article.save 
+0

안녕하세요, 감사합니다! 그게 내가 attr_accessible했다 : category_id 대신에 : category_ids –

3

양식 필드 속성 category_id을 설정하는 것입니다,하지만 속성이 보호됩니다. 모델에서 다음과 같은 코드 행을 가져야합니다.

attr_accessible :title, :teaser, :body, :published 

이러한 속성은 대량 할당에 허용됩니다.

attr_accessible :title, :teaser, :body, :published, :category_id 

이 문제를 해결해야한다 : 당신은 당신이 attr_accessible 방법에 이러한 속성을 추가해야 category_id 설정 양식을합니다.

+0

안녕하세요, Paul, 답변 해 주셔서 감사합니다. 네, 저는이 라인을 가지고 있었고 drhenner가 지적한 바와 같이 attr_accessible에 문제가있었습니다 : category_id가 있어야합니다 : category_ids –

관련 문제