Rails创建方法失败

时间:2015-05-17 10:27:16

标签: ruby-on-rails

我正在尝试创建连接到“用户组”和用户的“群组帖子”。我的create方法的第一行失败,出现此错误:

Couldn't find UserGroup with 'id'= 

我一直在关注博客构建教程,认为我的“团队帖子”就像评论一样,而不是附加到附加到“用户组”的文章上。

我对Rails很新,所以它可能只是一个语法问题。任何建议将不胜感激。

这是组后创建方法:

def create
    @user_group = UserGroup.find(params[:user_group_id])
    @group_post = current_user.group_posts.new(group_post_params)
    if @group_post.save
        respond_to do |format|
            format.html {redirect_to user_group_path(@user_group), notice: "Group Post created!"}
        end
    else
        redirect_to user_group_path(@user_group), notice: "Something went wrong."
    end
end

private

    def group_post_params
        params.require(:group_post).permit(:content, :post_type, :user_group_id)
    end

以下是用户组模型:

class UserGroup < ActiveRecord::Base

    has_many :group_members, dependent: :destroy
    has_many :members, through: :group_members, source: :user

    has_many :group_posts, dependent: :destroy

    validates :name, presence: true, length: {minimum: 5}
    validates :searchable, presence: true, length: {minimum: 5}

    def owners
        members.includes(:group_members).where('group_members.owner = ?', true)
    end

    def regular_users
        members.includes(:group_members).where('group_members.owner = ?', false)
    end

end

这是小组发布模型:

class GroupPost < ActiveRecord::Base

    include PublicActivity::Model
    belongs_to :user
    belongs_to :user_group

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

end

最后是路线:

Rails.application.routes.draw do

  devise_for :users, controllers: {registrations: 'registrations'}

  root 'pages#home'

  resources :users, only: [:index, :show]
  resources :friendships, only: [:create, :destroy, :accept] do
    member do
        put :accept
    end
  end

  resources :posts, only: [:create, :edit, :update, :destroy]
  resources :group_posts, only: [:create, :edit, :update, :destroy]
  resources :activities, only: [:index] do
    member do
      put "upvote" =>"activities#upvote"
    end
  end

  resources :user_groups do
    resources :group_posts
    resources :group_members
  end

end

以下是部分提交小组帖子的表格:

<div class="posts-panel">
    <%= form_for(@group_post) do |f| %>
        <%= render 'partials/error_messages', object: f.object %>
        <div class="form-group">
            <%= f.text_area :content, placeholder: "Compose new post...", class: "form-control" %>
        </div>
        <div class="form-group">
            <%= f.select :post_type, [['Request', 'request'],['Report', 'report']], {}, {class: "form-control"}  %>
        </div>
        <div class="form-group">
            <%= hidden_field_tag :user_group_id, @usergroup.id %>
            <%= f.submit "Post", class: "btn btn-primary" %>
        </div>
    <% end %>
</div>

完成参数:

{"utf8"=>"✓", "authenticity_token"=>"thkRQYNcl+ySSoWIE83V22DEqYdttg+TF4coFsmasXkt2mylgB2YG/vAl2KYRey/djTqL5iNSTIyWJpsSWyCQQ==", "group_post"=>{"content"=>"This is it", "post_type"=>"prayer"}, "user_group_id"=>"1", "commit"=>"Post", "controller"=>"group_posts", "action"=>"create"}

1 个答案:

答案 0 :(得分:0)

user_group_id不在params哈希中。将以下行添加到create方法可以解决问题:

@group_post.user_group = @user_group