如何通过表单中的下拉菜单设置新对象的belongs_to关联?

时间:2013-04-01 22:28:38

标签: ruby-on-rails ruby-on-rails-3 model associations model-associations

所以我是Rails的新手,我刚刚完成了Michael的Hartl的railstutorial.org。 我现在正在构建我的第一个应用程序,而且我遇到了我的第一个路障。

所以基本上我有泡泡(相当于主题)和谣言。 谣言属于泡泡,泡泡有很多谣言。 在创建新的谣言时,用户必须选择它所属的Bubble。

我创建了我的2个模型如下:

class Bubble < ActiveRecord::Base
  attr_accessible :description, :name

  has_many :rumors, dependent: destroy

  validates :description, presence: true, length: { maximum: 300 }
  validates :name, presence: true, length: { maximum: 50 }
end

class Rumor < ActiveRecord::Base
  attr_accessible :content, :title, :bubble_id

  belongs_to :bubble

  validates :title, presence: true, length: { maximum: 30 }
  validates :content, presence: true, length: { maximum: 300 }
  validates :bubble_id, presence: true
end

创建新谣言的表单在主页上呈现:

<%= simple_form_for(@rumor) do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">
    <%= f.input :title %>
    <%= f.input :content %>
    <%= f.input :bubble_id, collection: @bubbles %>
  </div>

  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>

因此我在StaticPagesController中定义了变量:

def home
  @bubbles = Bubble.all
  @rumor = Rumor.new
  respond_to do |format|
    format.html # new.html.erb
    format.json { render json: @bubble }
  end
end

我的泡泡控制器如下: class BubblesController&lt; ApplicationController中

  before_filter :authenticate_user!, only: [:create, :edit, :destroy]

  # GET /bubbles
  # GET /bubbles.json
  def index
    if params[:tag]
      @bubbles = Bubble.tagged_with(params[:tag])
    else
      @bubbles = Bubble.all
    end
    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @bubbles }
    end
  end

  # GET /bubbles/1
  # GET /bubbles/1.json
  def show
    @bubble = Bubble.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @bubble }
    end
  end

  # GET /bubbles/new
  # GET /bubbles/new.json
  def new
    @bubble = Bubble.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @bubble }
    end
  end

  # GET /bubbles/1/edit
  def edit
    @bubble = Bubble.find(params[:id])
  end

  # POST /bubbles
  # POST /bubbles.json
  def create
    @bubble = Bubble.new(params[:bubble])

    respond_to do |format|
      if @bubble.save
        format.html { redirect_to @bubble, notice: 'Bubble was successfully created.' }
        format.json { render json: @bubble, status: :created, location: @bubble }
      else
        format.html { render action: "new" }
        format.json { render json: @bubble.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /bubbles/1
  # PUT /bubbles/1.json
  def update
    @bubble = Bubble.find(params[:id])

    respond_to do |format|
      if @bubble.update_attributes(params[:bubble])
        format.html { redirect_to @bubble, notice: 'Bubble was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @bubble.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /bubbles/1
  # DELETE /bubbles/1.json
  def destroy
    @bubble = Bubble.find(params[:id])
    @bubble.destroy

    respond_to do |format|
      format.html { redirect_to bubbles_url }
      format.json { head :no_content }
    end
  end
end

谣言控制者:

class RumorsController < ApplicationController

  before_filter :authenticate_user!, only: [:new, :create, :edit, :destroy]

  # GET /rumors
  # GET /rumors.json
  def index
    @rumors = Rumor.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @rumors }
    end
  end

  # GET /rumors/1
  # GET /rumors/1.json
  def show
    @rumor = Rumor.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @rumor }
    end
  end

  # GET /rumors/new
  # GET /rumors/new.json
  def new
    @rumor = Rumor.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @rumor }
    end
  end

  # GET /rumors/1/edit
  def edit
    @rumor = Rumor.find(params[:id])
  end

  # POST /rumors
  # POST /rumors.json
  def create
    @rumor = Rumor.new(params[:rumor])

    respond_to do |format|
      if @rumor.save
        format.html { redirect_to @rumor, notice: 'Rumor was successfully created.' }
        format.json { render json: @rumor, status: :created, location: @rumor }
      else
        format.html { render action: "new" }
        format.json { render json: @rumor.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /rumors/1
  # PUT /rumors/1.json
  def update
    @rumor = Rumor.find(params[:id])

    respond_to do |format|
      if @rumor.update_attributes(params[:rumor])
        format.html { redirect_to @rumor, notice: 'Rumor was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @rumor.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /rumors/1
  # DELETE /rumors/1.json
  def destroy
    @rumor = Rumor.find(params[:id])
    @rumor.destroy

    respond_to do |format|
      format.html { redirect_to rumors_url }
      format.json { head :no_content }
    end
  end
end

我仍然很难弄清楚如何通过表单建立关联,所以任何帮助都会受到赞赏,即使它不是我问题的完整解决方案。

0 个答案:

没有答案