需要显示名称vs" client_id" - Rails 4.0 App

时间:2014-03-11 20:43:05

标签: ruby-on-rails

我有一个简单的问题,但我找不到任何解决方案。我是rails& amp;的全新手ruby,如果我要求你在任何回复中都非常明确,请原谅我。所有这些不同的上部&小写("客户" vs"客户"?),单数和&复数名称(bentry vs bentries?非常混乱,如果你认为我是愚蠢的话,请提前原谅我。

我有一个表,其客户端具有id(主键)和字段" lastnameorbizname"。我有第二个计费条目表(bentries),它有一个名为" client_id"的外键。等于客户端的主键。我可以在一个表单中创建客户端,然后以另一种形式为我的任何现有客户创建关联的计费条目。经过几个小时的搜索,我想出了collection_select,这就是......

<%= collection_select(:bentry, :client_id, Client.all.order('lastnameorbizname ASC'), :id, :full_name, propmpt: false ) %>

但是当我尝试显示结算条目&#34; index&#34;时,我可以生成表格中所有条目的列表,但我希望当前显示的列&#34; client_id&#34;改为显示&#34; lastnameorbizname&#34;来自客户表。

我所拥有的至少显示正确的client_id是......

<td><%= bentry.client_id %></td>

我试过......

<td><%= @bentry.client.lastnameorbizname %></td>

会产生以下错误...&#34;未定义的方法`client&#39;为零:NilClass&#34;。

我试图解决...

<td><%= @bentry.client.lastnameorbizname unless @bentry.client.lastnameorbizname.nil? %></td>

生成相同的错误消息。

我的bentries_controller中没有任何内容设置为变量来保存客户端的lastnameorbizname ...我应该吗?

我过度了!请帮忙,假设我不知道。

这是client.rb模型......

class Client < ActiveRecord::Base

attr_accessible :firstname, :lastnameorbizname, :active

has_many :bentries

validates :lastnameorbizname, presence: true

def full_name
  "#{lastnameorbizname}, #{firstname}"
end

end

和bentry.rb模型......

class Bentry < ActiveRecord::Base

attr_accessible :billDate, :timeBilled, :expenseBilled, :client_id 

belongs_to :client

end

最后,bentries_controller.rb ......

class BentriesController < ApplicationController
before_action :set_bentry, only: [:show, :edit, :update, :destroy]

 # GET /bentries
 # GET /bentries.json
 def index
 @bentries = Bentry.all.order(:billDate)

 end

 # GET /bentries/1
 # GET /bentries/1.json
 def show

 end

 # GET /bentries/new
 def new
 @bentry = Bentry.new
 @bentries = Client.all
 respond_to do |format|
   format.html # new.html.erb
   format.xml  { render :xml => @bentry }
 end

 end

 # GET /bentries/1/edit
 def edit
 end

 # POST /bentries
 # POST /bentries.json
 def create
  @bentry = Bentry.new(bentry_params)

  respond_to do |format|
    if @bentry.save
      format.html { redirect_to @bentry, notice: 'Billing Entry was successfully   created.' }
     format.json { render action: 'show', status: :created, location: @bentry }
   else
     format.html { render action: 'new' }
     format.json { render json: @bentry.errors, status: :unprocessable_entity }
   end
end
end

# PATCH/PUT /bentries/1
# PATCH/PUT /bentries/1.json
def update
  respond_to do |format|
    if @bentry.update(bentry_params)
      format.html { redirect_to @bentry, notice: 'Billing Entry was successfully updated.' }
      format.json { head :no_content }
    else
      format.html { render action: 'edit' }
      format.json { render json: @bentry.errors, status: :unprocessable_entity }
    end
  end
end

# DELETE /bentries/1
# DELETE /bentries/1.json
def destroy
  @bentry.destroy
  respond_to do |format|
    format.html { redirect_to bentries_url }
    format.json { head :no_content }
  end
end

private
  # Use callbacks to share common setup or constraints between actions.
  def set_bentry
    @bentry = Bentry.find(params[:id])
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def bentry_params
    params.require(:bentry).permit(:billDate, :timeBilled, :expenseBilled, :client_id)
  end
end

Per @ craig.kaminsky的请求我已经在index.html.erb视图中为bentries页面添加了代码......

<h3>Billing Entries by Date</h3>

<table>
  <thead>
    <tr style="text-decoration: underline">
      <th>Bill Date &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | </th>
      <th>Time Billed &nbsp; &nbsp; &nbsp; | </th>
      <th>Expenses Billed &nbsp; &nbsp; &nbsp; | </th>
      <th>Client ID &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
 </thead>
 <hr />
 <tbody>
    <% @bentries.each do |bentry| %>
      <tr>
        <td><%= link_to bentry.billDate.strftime('%B, %d, %Y'), edit_bentry_path(bentry) %></td>
        <td><%= number_to_currency(bentry.timeBilled) %></td>
        <td><%= number_to_currency(bentry.expenseBilled) %></td>
        <td><%= @bentry.client.lastnameorbizname %></td>  <!--  Orig content ...   <%= bentry.client_id %>   -->
        <!--  <td><%= link_to 'Show', bentry %></td>  << this is superfulous.  -->
        <!-- <td><%= link_to 'Edit', edit_bentry_path(bentry) %></td>  << linked to date entry above instead -->
        <td><%= link_to 'Delete', bentry, method: :delete, data: { confirm: 'This CANNOT be undone...Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>

2 个答案:

答案 0 :(得分:1)

正如@raviolicode在评论中提到的那样,您需要确保@bentrybentry相对定,因为在您的帖子中您注意到代码示例中的这两个差异

<td><%= bentry.client_id %></td>
# vs.
<td><%= @bentry.client.lastnameorbizname %></td>

注意您添加@符号的方式。赔率bentry.client.lastnameorbizname不会引发错误。我的感觉是,您在@bentries的索引方法中设置的BentriesController进行迭代,需要将该变量作为bentry访问,而不使用@符号。

答案 1 :(得分:0)

我不知道它为什么会起作用,所以如果有人可以解释,那就太好了,但这就是有效的。对我的index.html.erb进行了一次单独更改,以确定它...

<td><%= bentry.client.try(:lastnameorbizname) %></td> 

只需添加“try(...)”修复它。没有更多的东西添加到控制器或上面没有显示的模型。

这是一个很好的“ruby / rails方式”修复它吗?