Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 集合(u select)不';不能通过表格保存_Ruby On Rails_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 集合(u select)不';不能通过表格保存

Ruby on rails 集合(u select)不';不能通过表格保存,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,我知道有很多类似的帖子,人们都有同样的问题,但没有一个能帮到我。如果我创建了一篇新文章,那么它就没有类别了。但是,如果我编辑了前面在seed.rb中创建的文章,那么类别就会更新。 怎么了 类别表: class CreateCategories < ActiveRecord::Migration def change create_table :categories do |t| t.string :name t.timestamps end

我知道有很多类似的帖子,人们都有同样的问题,但没有一个能帮到我。如果我创建了一篇新文章,那么它就没有类别了。但是,如果我编辑了前面在seed.rb中创建的文章,那么类别就会更新。 怎么了

类别表:

class CreateCategories < ActiveRecord::Migration
  def change
    create_table :categories do |t|
      t.string :name
      t.timestamps
    end
  end
end
class-CreateCategories
category.rb

class Category < ActiveRecord::Base
    has_many :articles
end
类别
第1.rb条

class Article < ActiveRecord::Base
   belongs_to :category
end
类文章
然后我有一个表格文件

<%= form_for(@article) do |f| %>

  <div class="title">
        <%= f.label :title %>
        <%= f.text_field :title %>
  </div>
  <div class="content">
        <%= f.label :content %>
        <%= f.text_field :content %>
  </div>
  <div class="category">
        <%= f.label :category %>
        <%= collection_select(:article, :category_id, Category.all, :id, :name) %>
  </div>
  <div class="actions">
        <%= f.submit %>
  </div>
<% end %>

有一个控制器文件:

class CategoriesController < ApplicationController
  def index
    @categories = Category.all
  end

  def show
    @category = Category.find(params[:id])
    @articles = @category.articles
  end

  def new
    @category = Category.new
  end

  def create
    @category = Category.new(category_params)
    if @category.save
      redirect_to(:action => 'index')
    else
      render('new')
    end
  end

  def edit
    @category = Category.find(params[:id])
  end

  def update
    @category = Category.find(params[:id])
    if @category.update_attributes(category_params)
      redirect_to(:action => 'show', :id => @category.id)
    else
      render('index')
    end
  end

  def delete
    @category = Category.find(params[:id])
  end

  def destroy
    Category.find(params[:id]).destroy
    redirect_to(:action => 'index')
  end

  private

  def category_params
    params.require(:category).permit(:name)
  end
end
class ArticlesController < ApplicationController
  def index
    @articles = Article.all
  end

  def show
        @article = Article.find(params[:id])
  end

  def new
        @article = Article.new
  end

  def create
    @article = Article.new(article_params)
    if @article.save
        redirect_to(:action => 'index')
    else
        render('new')
    end
  end

  def edit
    @article = Article.find(params[:id])
  end

  def update
    @article = Article.find(params[:id])
    if @article.update_attributes(article_params)
        redirect_to(:action => 'show', :id => @article.id)
    else
        render('index')
    end
  end

  def delete
    @article = Article.find(params[:id])
  end

  def destroy
    Article.find(params[:id]).destroy
    redirect_to(:action => 'index')
  end

  private
  def article_params
    params.require(:article).permit(:title, :content)
  end
end
class CategoriesController'index')
其他的
呈现('新')
终止
终止
定义编辑
@category=category.find(参数[:id])
终止
def更新
@category=category.find(参数[:id])
如果@category.update_属性(category_参数)
重定向到(:action=>'show',:id=>@category.id)
其他的
呈现('索引')
终止
终止
def删除
@category=category.find(参数[:id])
终止
def销毁
Category.find(参数[:id]).destroy
将_重定向到(:action=>'index')
终止
私有的
def类别参数
参数要求(:类别)。允许(:名称)
终止
终止
物品控制器文件:

class CategoriesController < ApplicationController
  def index
    @categories = Category.all
  end

  def show
    @category = Category.find(params[:id])
    @articles = @category.articles
  end

  def new
    @category = Category.new
  end

  def create
    @category = Category.new(category_params)
    if @category.save
      redirect_to(:action => 'index')
    else
      render('new')
    end
  end

  def edit
    @category = Category.find(params[:id])
  end

  def update
    @category = Category.find(params[:id])
    if @category.update_attributes(category_params)
      redirect_to(:action => 'show', :id => @category.id)
    else
      render('index')
    end
  end

  def delete
    @category = Category.find(params[:id])
  end

  def destroy
    Category.find(params[:id]).destroy
    redirect_to(:action => 'index')
  end

  private

  def category_params
    params.require(:category).permit(:name)
  end
end
class ArticlesController < ApplicationController
  def index
    @articles = Article.all
  end

  def show
        @article = Article.find(params[:id])
  end

  def new
        @article = Article.new
  end

  def create
    @article = Article.new(article_params)
    if @article.save
        redirect_to(:action => 'index')
    else
        render('new')
    end
  end

  def edit
    @article = Article.find(params[:id])
  end

  def update
    @article = Article.find(params[:id])
    if @article.update_attributes(article_params)
        redirect_to(:action => 'show', :id => @article.id)
    else
        render('index')
    end
  end

  def delete
    @article = Article.find(params[:id])
  end

  def destroy
    Article.find(params[:id]).destroy
    redirect_to(:action => 'index')
  end

  private
  def article_params
    params.require(:article).permit(:title, :content)
  end
end
class-ArticlesController'index')
其他的
呈现('新')
终止
终止
定义编辑
@article=article.find(参数[:id])
终止
def更新
@article=article.find(参数[:id])
如果@article.update_属性(article_参数)
重定向到(:action=>'show',:id=>@article.id)
其他的
呈现('索引')
终止
终止
def删除
@article=article.find(参数[:id])
终止
def销毁
物品。查找(参数[:id])。销毁
将_重定向到(:action=>'index')
终止
私有的
定义项目参数
参数要求(:文章)。许可证(:标题,:内容)
终止
终止

您似乎没有在
文章#创建
操作中建立关联。类别id正在通过您的表单发送,但您仍需要建立活动记录关联。您可以在文章控制器中尝试以下内容:

def create
    @article = Article.new(article_params)
    @category = Category.find(params[:category_id])
    @article.category = @category
    if @article.save
        redirect_to(:action => 'index')
    else
        render('new')
    end
  end 
请记住,创建关联有多种方法。您的Article类具有以下操作关联的功能:

@article.category  
@article.category= 
@article.build_category 
@article.create_category
@article.create_category!

使用
f.collection\u select
仍然不做任何事情请显示文章\u controller-类别\u controller在此处不相关。我将文章\u controller添加到主帖子中。好的,我想我发现了问题:P
参数要求(:文章)。许可证(:标题,:内容)
应该是
参数要求(:文章)。许可证(:标题,:内容,:类别)