连接三个控制器导轨

时间:2012-03-02 17:51:28

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1

我有三个控制器 - 用户,故事,类别。我希望当我以管理员身份登录时创建类别,然后为每个类别编写故事。我做了部分任务,但在表中的数据库中,故事category_id是空的,我无法理解如何解决它。这是我的代码的一部分:

故事/ new.html.erb:

<%= form_for(@story) do |f| %>
  <div class="field">
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :category %><br />
    <%= f.collection_select :category_id, @categories, :id, :title %>
  </div>
  <div class="field">
    <%= f.label :content %><br />
    <%= f.text_area :content %>
  </div>
  <div class="actions">
    <%= f.submit "Add" %>
  </div>
<% end %>

stories_controller.rb:

class StoriesController < ApplicationController
  before_filter :admin_user

  def new
    @story = Story.new
    @categories = Category.all
    @title = "Add news"
  end

  def create
    @categories = Category.all
    @story = current_user.stories.build(params[:story])
    if @story.save
      flash[:success] = "Successfullly added news"
      redirect_to @story
    else
      @title = "Add news"
      render 'new'
    end
  end

  private

  def admin_user
    redirect_to(root_path) unless current_user.admin?
  end
end

story.rb:

class Story < ActiveRecord::Base
  attr_accessible :title, :content

  belongs_to :user
  belongs_to :category

  validates :title, :presence => true
  validates :content, :presence => true
  validates :user_id, :presence => true

  default_scope :order => 'stories.created_at DESC'
end

category.rb:

class Category < ActiveRecord::Base
  attr_accessible :title

  has_many :stories

  validates :title, :presence     => true,
            :length       => { :within => 6..40 }
end

user.rb:

class User < ActiveRecord::Base
  attr_accessor :password
  attr_accessible :name, :email, :password, :password_confirmation

  has_many :stories

  validates :name,  :presence => true,
            :length   => { :maximum => 50 }
  validates :email, :presence => true

  email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

  validates :email, :presence   => true,
                :format     => { :with => email_regex },
                :uniqueness => true

  validates :password, :presence     => true,
                   :confirmation => true,
                   :length       => { :within => 6..40 }

  before_save :encrypt_password

  def has_password?(submitted_password)
    encrypted_password == encrypt(submitted_password)
  end

  def self.authenticate(email, submitted_password)
    user = find_by_email(email)
    return nil  if user.nil?
    return user if user.has_password?(submitted_password)
  end

  def self.authenticate_with_salt(id, cookie_salt)
    user = find_by_id(id)
    (user && user.salt == cookie_salt) ? user : nil
  end

  private

  def encrypt_password
    self.salt = make_salt unless has_password?(password)
    self.encrypted_password = encrypt(password)
  end

  def encrypt(string)
    secure_hash("#{salt}--#{string}")
  end

  def make_salt
    secure_hash("#{Time.now.utc}--#{password}")
  end

  def secure_hash(string)
    Digest::SHA2.hexdigest(string)
  end
end

1 个答案:

答案 0 :(得分:1)

您需要将attr_accessible:category_id添加到您的故事模型

它在控制器中阻止质量分配创建方法。或者,您可以从params散列中提取category_id并将其分配到单独的行中。