日本#index中没有方法错误

时间:2015-06-14 03:47:48

标签: ruby-on-rails ruby

我生成了一个名为#!/bin/sh GUNICORN=/usr/local/bin/gunicorn ROOT=/path/to/project PID=/var/run/gunicorn.pid APP=main:application if [ -f $PID ]; then rm $PID; fi cd $ROOT exec $GUNICORN -c $ROOT/gunicorn.conf.py --pid=$PID $APP 的脚手架。这是我rounds的样子:

JapasController

Japa模特:

class JapasController < ApplicationController
  before_action :set_japa, only: [:show, :edit, :update, :destroy]
  before_action :correct_user, only: [:edit, :update, :destroy]
  before_action :authenticate_user!, except: [:index, :show]
  def index
    @japas = Japa.all
    @users = User.all
  end
  def show
  end
  def new
    @japa = current_user.japas.build
  end
  def edit
  end
  def create
    @japa = current_user.japas.build(japa_params)
    respond_to do |format|
      if @japa.save
        format.html { redirect_to @japa, notice: 'Japa was successfully created.' }
        format.json { render :show, status: :created, location: @japa }
      else
        format.html { render :new }
        format.json { render json: @japa.errors, status: :unprocessable_entity }
      end
    end
  end
  def update
    respond_to do |format|
      if @japa.update(japa_params)
        format.html { redirect_to @japa, notice: 'Japa was successfully updated.' }
        format.json { render :show, status: :ok, location: @japa }
      else
        format.html { render :edit }
        format.json { render json: @japa.errors, status:     :unprocessable_entity }
      end
    end
  end
  def destroy
    @japa.destroy
    respond_to do |format|
      format.html { redirect_to japas_url, notice: 'Japa was successfully destroyed.' }
      format.json { head :no_content }
    end
  end
private
  # Use callbacks to share common setup or constraints between actions.
  def set_japa
    @japa = Japa.find(params[:id])
  end
  def correct_user
    @japa = current_user.japas.find_by(id: params[:id])
    redirect_to japas_path, notice: "Not authorized to edit this japacount" if @japa.nil?
  end
  # Never trust parameters from the scary internet, only allow the white list through.
  def japa_params
    params.require(:japa).permit(:rounds, :comment, :user_id)
  end
end

用户模型:

 class Japa < ActiveRecord::Base
    belongs_to :user
 end

以下是class User < ActiveRecord::Base devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable has_many :japas end 的索引模板:

Japas

加载时,以下是我的错误消息: <p id="notice"><%= notice %></p> <center> <h1>Listing Japa</h1> <table class = 'table table-hover' style = 'width:700px;'> <thead> <tr> <th>Time</th> <th>Rounds</th> <th>Goal</th> <th colspan="3"></th> </tr> </thead> <tbody> <% @users.each do |user| %> <tr> <td><%= user.japas.created_at %></td> <td><%= user.japas.rounds %></td> <td> / 16 </td> <td><%= link_to 'Show', japa %></td> <% if japa.user == current_user %> <td><%= link_to 'Edit', edit_japa_path(japa) %></td> <td><%= link_to 'Destroy', japa, method: :delete, data: { confirm: 'Are you sure?' } %></td> <%end%> </tr> <tr> <td>Total Sum</td> </tr> <% end %> </tbody> </table> </center> <br> <% if user_signed_in? %> <%= link_to 'New Japa', new_japa_path %> <%end%>

http://localhost:3000/japas

这是我收到的错误页面的图片: Screenshot of Error page

2 个答案:

答案 0 :(得分:0)

您无法使用活动记录关联集合调用created_at之类的属性/方法。 User有许多Japa,因此您可以使用其余代码在错误区域中重新编写视图。只需在user.japas内迭代。

<% @users.each do |user| %>
   <% user.japas.each do |japa| %>   
  <tr>
     <td><%= japa.created_at %></td>
     <td><%= japa.rounds %></td>
  </tr>
  <% end %>
<% end %>

答案 1 :(得分:0)

根据您对评论的要求,您可以尝试此操作。

您必须更改控制器操作,如下所示才能获得Array ( [0] => home/club/member [1] => home/club/staff [3] => home/premier/member ) 的{​​{1}}

japas

在视图中使用@japas进行迭代,如下所示

current_user