Rails:从表单发布到M:M表

时间:2015-06-22 04:29:00

标签: ruby-on-rails activerecord

在组索引页面上,我会显示一个小组讨论列表和一个用于创建新讨论的表单。

class Group < ActiveRecord::Base
  has_many :discussions
end

class Discussion < ActiveRecord::Base
  belongs_to :user
  belongs_to :group
end

控制器:

class DiscussionsController < ApplicationController
  def index
    @group = Group.find(params[:group_id])
    @discussion = current_user.discussions.build({group_id: @group.id}) if logged_in?
    @discussions = @group.discussions
  end

  def create
    @discussion = current_user.discussions.build(discussion_params)
    if @discussion.save
      flash[:success] = "Discussion started."
      redirect_to root_url
    end
  end

  private

    def discussion_params
      params.require(:discussion).permit(:title, :content)
    end
end

我的表格如下。

<%= form_for([@group, @discussion]) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
  <div class="field">
    <%= f.text_field :title, placeholder: "Title" %>
  </div>
  <div class="field">
    <%= f.text_area :content, placeholder: "Start a discussion..." %>
  </div>
  <%= f.submit "Post", class: "btn btn-primary" %>
<% end %>

这样可以正常工作但是在提交表单时,会记录没有group_id的行。我怀疑问题在于我在创建操作中构建讨论对象的方式,但我不确定如何以不同方式执行它。

Started POST "/groups/635601/discussions" for 127.0.0.1 at 2015-06-22 15:16:07 +1000
  ActiveRecord::SchemaMigration Load (0.3ms)  SELECT "schema_migrations".* FROM "schema_migrations"
Processing by DiscussionsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"tJWJQKb6NcS1EAaeeLramzQp82xf4hLrXt+UTzKQo8+i1ePoNLYr8jXYSgBLpSxNvoPOAfNxnim1Ln6fKJ0uiw==", "discussion"=>{"title"=>"Test1", "content"=>"Test2"}, "commit"=>"Post", "group_id"=>"635601"}
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 406]]
   (0.1ms)  begin transaction
  SQL (0.4ms)  INSERT INTO "discussions" ("title", "content", "user_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)  [["title", "Test1"], ["content", "Test2"], ["user_id", 406], ["created_at", "2015-06-22 05:16:07.883843"], ["updated_at", "2015-06-22 05:16:07.883843"]]
   (13.9ms)  commit transaction
Redirected to http://localhost:3000/
Completed 302 Found in 81ms (ActiveRecord: 15.5ms)

我可以在params集合中看到group_id在那里但在哈希之外。

1 个答案:

答案 0 :(得分:0)

正如我所说,您需要调整create这样的操作才能保存group_id

def create
@group = Group.find(params[:group_id])
@discussion = current_user.discussions.build(discussion_params)
@discussion.group_id = @group.id
 if @discussion.save
   flash[:success] = "Discussion started."
   redirect_to root_url
 end
end

<强> OR

您可以在hidden_field中添加form_for,以便像这样保存group_id

<%= f.hidden_field :group_id, :value => @group.id %>

在这种情况下,您的create操作就像这样

def create
@group = Group.find(params[:group_id])
@discussion = current_user.discussions.build(discussion_params)
 if @discussion.save
   flash[:success] = "Discussion started."
   redirect_to root_url
 end
end

您还需要将group_id添加到discussion_params

def discussion_params
  params.require(:discussion).permit(:title, :content, :group_id)
end

而且,是的,这完全是Rails的做法。