【问题标题】:Basic Rails - how do automatically assign a new database entry to an associated entry it belongs to?Basic Rails - 如何自动将新的数据库条目分配给它所属的关联条目?
【发布时间】:2015-05-27 15:33:15
【问题描述】:

我想自动将新的数据库条目与其所属的数据库条目相关联,而无需在表单上做出选择,因为用户只能来自类别页面,这样一旦您进入类别并且您决定在该类别中创建一个新条目,新创建的条目在提交时自动位于该类别中。任何人都可以提供任何帮助吗?

我的模型如下:

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 %>

【问题讨论】:

  • 你是说关联用户决定了类别?如果是这样,您可以在创建指南时使用控制器中的信息来设置类别吗?否则如何确定类别?
  • 当您在控制器中分配@guide 时,您需要从类别中构建它。像@guide = category.guides.build 这样的东西。您需要先分配类别(例如,category = Category.find(params[:id]),如果您将 id 传递给控制器​​)​​。需要查看相关的控制器和参数才能更具体。
  • 已添加向导控制器。很想看看你的建议。同时将尝试实施上述一些评论。干杯
  • 将哪些参数传递给新方法?
  • 不清楚你的意思,已经在上面的控制器中编辑了,你可以看看。

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


【解决方案1】:

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

resources :categories do
  resources :guides
end

然后使用你的新路线

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

这应该可以在取回表单时更容易地取回指南的类别。

【讨论】:

  • 现在在以下'指南上出现无方法错误:

    '
  • 尝试将guide_path(guide) 更改为category_guide_path(guide)
  • 也不起作用No route matches {:action=&gt;"show", :controller=&gt;"guides", :format=&gt;nil,
【解决方案2】:

假设您的指南表中有一个存储类别 ID 的列,并且您的路线按照建议嵌套,您应该可以添加

@guide.category_id = @category.id

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

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

现在应该可以了

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

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

【讨论】:

    猜你喜欢
    • 2016-07-06
    • 2021-03-27
    • 1970-01-01
    • 1970-01-01
    • 2011-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多