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

davidminaki · 2013年11月07日 · 最后由 wangping 回复于 2013年11月08日 · 4005 次阅读

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 %> 请指教。

希望@ xdite 进来解答

controller 呢?

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

这个 group 没有 owner。

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

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

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

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