Rails 希望 @ xdite 进来解答,在读 rails101,请教一个问题

davidminaki · 发布于 2013年11月07日 · 最后由 wangping 回复于 2013年11月08日 · 2108 次阅读
9590

group model 代码

class Group < ActiveRecord::Base
  has_many :posts
  belongs_to :owner, :class_name => "User", :foreign_key => :user_id

  has_many :group_users
  has_many :participated_groups, :through => :group_users, :source => :group

  validates :title, :presence => true

  def editable_by?(user)
    user && user == owner
  end
end

user model 代码

class User < ActiveRecord::Base


  has_many :groups
  has_many :posts

  extend OmniauthCallbacks

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  def join!(group)
    participated_groups << group
  end

  def quit!(group)
    participated_groups.delete(group)
  end

  after_create :join_owner_to_group

  def is_member_of?(group)
    participated_groups.include?(group)
  end

  def join_owner_to_group
    members << owner
  end
end

group view代码

<div class="span12">
  <div class="group">
    <%= link_to("New group", new_group_path , :class => "btn btn-mini btn-primary pull-right")%>
  </div>
  <table class="table">
    <thead> <tr>
      <td> # </td>
      <td> Title </td>
      <td> Descroption </td>
      <td> Owner </td>
    </tr>
    </thead>
    <tbody>
    <% @groups.each do |group| %>
        <tr>
          <td> # </td>
          <td> <%= link_to(group.title, group_path(group)) %> </td>
          <td> <%= group.description %> </td>
          <td> <%= group.owner.name %> </td>
          <td>
            <% if current_user && group.editable_by?(current_user) %>
                <%= link_to("Edit", edit_group_path(group), :class => "btn btn-mini")%>
                <%= link_to("Delete", group_path(group), :class => "btn btn-mini",:method => :delete, :confirm => "Are you sure?" ) %>
            <% end %>
          </td>
        </tr>
    <% end %>
    </tbody>
  </table>
</div>

然后运行报错

NoMethodError at /

undefined method `name' for nil:NilClass 错误代码指向这一行

<%= group.owner.name %> 请指教。
共收到 7 条回复
9590

希望@ xdite进来解答

9861

controller 呢?

9861

你是直接查group的把,group跟ower是多对一的关系,你得把controller的代码拿出来看看把

1

这个 group 没有 owner。

9590

controller代码如下:

class GroupsController < ApplicationController

  before_action :login_required, :only => [:new, :create, :edit,:update,:destroy]

  def index
    @groups = Group.all
  end

  def show
    @group = Group.find(params[:id])
  end


  def new
    @group = Group.new
  end


  def create
    @group = current_user.groups.build(group_params)
    if @group.save
      redirect_to groups_path
    else
      render :new
    end
  end

  def edit
    @group = current_user.groups.find(params[:id])
  end


  def update
    @group = current_user.groups.find(params[:id])
    if @group.update(group_params)
      redirect_to group_path(@group)
    else
      render :edit
    end
  end

  def destroy
    @group = current_user.groups.find(params[:id])
    @group.destroy
    redirect_to groups_path
  end


  private

  def group_params
    params.require(:group).permit(:title, :description)
  end


  def join
    @group = Group.find(params[:id])
    if !current_user.is_member_of?(@group)
      current_user.join!(@group)
    else
      flash[:warning] = "You already joined this group."
    end
    redirect_to group_path(@group)
  end


  def quit
    @group = Group.find(params[:id])
    if current_user.is_member_of?(@group)
      current_user.quit!(@group)
    else
      flash[:warning] = "You are not member of this group."
    end
    redirect_to group_path(@group)
  end

end
9590

@Rei 谢谢问题解决,确实是没有ower

9861

我怎么觉得是不能用group得到ower,二可以ower可以得到gruop的

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册