【问题标题】:Using checkboxes in a belongs to and has one relationship在 a 中使用复选框属于并具有一种关系
【发布时间】:2013-05-12 04:33:00
【问题描述】:

在我的“帐户”模型的“显示”页面上,我有一个“清单”模型。我希望能够在帐户的显示页面中检查清单上的每个布尔值。 我收到以下错误:

NoMethodError in Accounts#show
undefined method `model_name' for NilClass:Class
  • app/models/account.rb

    class Account < ActiveRecord::Base
      has_one :checklist
    end
    
  • app/models/checklist.rb

    class Checklist < ActiveRecord::Base
      belongs_to :account
      belongs_to :user
    
      validates :account_id, :presence => true
      validates :user_id, :presence => true
    end
    
  • app/models/user.rb

    class User < ActiveRecord::Base
      has_many :checklists
    end
    
  • app/controllers/account_controller.rb

    class AccountController < ApplicationController
      def show
        @account = Account.find(params[:id])
        @checklist = @account.checklist
      end
    end
    
  • app/views/account/show.html.erb

    <% simple_form_for(@checklist) do |checklist| %>
      <div>
        <%= checklist.user.email %>
        <div class="pull-right">
          <%= checklist.created_at.strftime("%b. %d %Y") %>
        </div></br>
        <ul>
          <li><%= checklist.contract_signed %></li>
          <li><%= checklist.contract_details %></li>
          <li>…</li>
        </ul>
        <%= f.submit "Update", class: 'btn' %>
      </div>
    <% end %>
    
  • app/controllers/checklists_controller.rb

    class ChecklistsController < ApplicationController
      before_filter :authenticate_user!
    
      def create
        @account = Account.find(params[:account_id])
        @checklist = @account.checklist.build(params[:checklist])
        @checklist.user = current_user
    
        respond_to do |format|
          if @checklist.save
            format.html { redirect_to(@account, :notice => 'Checklist Saved.') }
          else
            format.html { redirect_to(@account, :notice => 'There was an errors') }
          end
        end
      end
    
    
      def destroy
        @checklist = current_user.checklists.find(params[:id])
        @account = Account.find(params[:account_id])
        @checklist.destroy
    
        respond_to do |format|
          format.html { redirect_to @account }
        end
      end
    end
    

【问题讨论】:

  • 我认为问题是@checklist 为零
  • 我知道它是零。我不确定如何修复它,因为在创建之前它总是为零。

标签: ruby-on-rails forms checkbox associations


【解决方案1】:

我不确定我是否完全理解您的视图逻辑,但我认为您需要做的是在进入控制器的创建部分之前构建清单。

例如

  def show
    @account = Account.find(params[:id])
    @checklist = @account.build_checklist
  end

这将允许您调用正在显示的视图中的属性。

你也可以调整create方法,使用new代替build

def create
  @account = Account.find(params[:account_id])
  @checklist = @account.checklist.new(params[:checklist])
  @checklist.user = current_user

  respond_to do |format|
      if @checklist.save
        format.html { redirect_to(@account, :notice => 'Checklist Saved.') }
      else
        format.html { redirect_to(@account, :notice => 'There was an error saving your comment (empty comment or comment way to long).') }
      end
  end
end

希望这会有所帮助:)

【讨论】:

  • 好吧……说得通。我已经做出了改变。出现错误显示:第 4 行引发:#:0x007f9e395276d0> 的未定义方法 `checklists_path'
  • 您是否接受在您的 Account 类中设置的嵌套属性?
猜你喜欢
  • 2011-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-14
  • 1970-01-01
  • 2020-09-20
相关资源
最近更新 更多