基本Rails - 如何自动将新数据库条目分配给它所属的关联条目?

时间:2015-05-27 15:33:15

标签: ruby-on-rails ruby activerecord associations models

我希望自动将新数据库条目与其所属的数据库条目相关联,而无需在表单上进行选择,因为用户只能来自类别页面,因此一旦你&#39 ;在类别中,您决定在该类别中创建新条目,新创建的条目在提交时自动在该类别中。有人可以提供任何帮助吗?

我的模型如下:

class Category < ActiveRecord::Base

  has_many :guides

end


class Guide < ActiveRecord::Base

  belongs_to :user
  belongs_to :category
  has_many :ratings

  def average_rating
    average = ratings.inject(0.0){ |sum, el| sum + el.value }.to_f / ratings.size
    average.round(2)
  end

end

为类别创建新指南的链接非常标准,但我认为添加实例变量可能会自动将条目与类别相关联,尽管它没有:

<%= link_to 'New Guide', new_guide_path(@category) %>

以下是指南的控制器:

class GuidesController < ApplicationController
  before_action :set_guide, only: [:show, :edit, :update, :destroy]

  # GET /guides
  # GET /guides.json
  def index
    @guides = Guide.all
  end

  # GET /guides/1
  # GET /guides/1.json
  def show
  end

  # GET /guides/new
  def new
    @guide = Guide.new
  end

  # GET /guides/1/edit
  def edit
  end

  # POST /guides
  # POST /guides.json
  def create
    @guide = Guide.new(guide_params)

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

  # PATCH/PUT /guides/1
  # PATCH/PUT /guides/1.json
  def update
    respond_to do |format|
      if @guide.update(guide_params)
        format.html { redirect_to @guide, notice: 'Guide was successfully updated.' }
        format.json { render :show, status: :ok, location: @guide }
      else
        format.html { render :edit }
        format.json { render json: @guide.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /guides/1
  # DELETE /guides/1.json
  def destroy
    @guide.destroy
    respond_to do |format|
      format.html { redirect_to guides_url, notice: 'Guide was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_guide
      @guide = Guide.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def guide_params
      params.require(:guide).permit(:name, :category_id, :user_id, :stepOneText, :stepOnePhoto, :stepTwoText, :stepTwoPhoto, :stepThreeText, :stepThreePhoto)
    end
end

表单也非常标准,我应该在这里自动将其分配给它所属的类别条目吗?

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

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


  <div class="field">
    <%= f.label :name %>
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :stepOneText %>
    <%= f.text_field :stepOneText %>
  </div>
  <div class="field">
    <%= f.label :stepOnePhoto %>
    <%= f.text_field :stepOnePhoto %>
  </div>
  <div class="field">
    <%= f.label :stepTwoText %>
    <%= f.text_field :stepTwoText %>
  </div>
  <div class="field">
    <%= f.label :stepTwoPhoto %>
    <%= f.text_field :stepTwoPhoto %>
  </div>
  <div class="field">
    <%= f.label :stepThreeText %>
    <%= f.text_field :stepThreeText %>
  </div>
  <div class="field">
    <%= f.label :stepThreePhoto %>
    <%= f.text_field :stepThreePhoto %>
  </div>

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

2 个答案:

答案 0 :(得分:1)

在我看来,你可以在这里寻找类似嵌套路线的东西:

var line = svg.append("g")
    .append("path")
    .attr("d", lineh(dataset)) // <- Note the change
    .attr("class", "line");

然后使用您的新路线

resources :categories do
  resources :guides
end

这样可以让您在收到表格时更容易获得指南的类别。

答案 1 :(得分:0)

假设您的指南表上有一个存储类别ID的列,并且您的路由按照建议嵌套,您应该能够添加

@guide.category_id = @category.id

向导游控制器创建操作。并在您的表单中将第一行更改为

<%= form_for[@category, @guide] do |f| %>

现在这应该可行

<%= link_to 'new guide', new_category_guide_path(@category) %>

当前类别应在创建时分配给您的指南。