0

这是我的控制器
参数在创建操作下显示为空(创建操作下的第二行)
我不知道我哪里出错了?我总是收到这个错误

undefined method `name' for nil:NilClass

class Console::RegionContactsController < Console::BaseController

  def index
    @all_regions = Region.visible
    @regioncontact = RegionContact.search_region(params[:search_cg]) if params[:search_cg].present?
    #@contactgroup = @contactgroup.page(params[:page]).per(50)
    #@contactgroup_count = @contactgroup.count

    respond_to do |format|
      format.html
    end
  end

  def destroy
    @rc = RegionContact.find(params[:id])
    @rc.destroy

    respond_to do |format|
      format.html { redirect_to :back }
    end
  end

  def new
    @params = params[:region_id] if params[:region_id].present?
    @reg = Region.find(@params) if @params.present?
    @region_contact = RegionContact.new
    @region_contact.region_id = @params if params.present?

    puts "INSIDE NEW"

    #puts "#{@region_contact.region.name}"

    @allcontacts = Contact.order("name ASC").all
    @allregions = Region.visible
    @allroles = Role.all

    respond_to do |format|
      format.html
    end

  end

  def create
    @region_contact = RegionContact.new(params[:region_contact])

    puts "region contact are #{@region_contact.inspect}"

    puts "here are params"
    puts params[:region_contact]
    puts "-----"

    respond_to do |format|
      if @region_contact.save
        puts "DID SAVE"
        format.html { redirect_to console_region_contacts_path, notice: 'Region Contact was successfully created.'}
      else
        puts "DID NOT SAVE"
        format.html { render action: "new" }
      end
    end
  end

  def update
    @region_contact = RegionContact.find(params[:id])

    respond_to do |format|
      if @region_contact.update_attributes(params[:region_contact])
        format.html { redirect_to console_phone_path(@rc), notice: 'Region Contact was successfully updated.' }
      else
        format.html { render action: "edit" }
      end
    end
  end

  def show
    @region_contact = RegionContact.find(params[:id])
    respond_to do |format|
      format.html
    end
  end

end

这是我的 _form.html.erb

<%= form_for([:console, @region_contact]) do |f| %>
  <% if @region_contact.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@region_contact.errors.count, "error") %> prohibited this Region Contact from being saved:</h2>

      <ul>
      <% @region_contact.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>


  <% if params[:region_id] %>

  <div class="field">
    <%#= f.label :region_id %><br />
    <%#= f.text_field :region_id, :value => @reg.id, :readonly => true%> <%= @reg.name rescue nil %> <br><br>
    <%=  hidden_field_tag :region_id, params[:region_id] %>
  </div>

  <% else %>

   <div class="field">
     <%= f.label :region_id %><br />
     <%= select_tag :region_id, options_from_collection_for_select(@allregions, "id", "name", params[:region_id]), :prompt => "Search By region", include_blank: false, :disabled => false %>
   </div>

  <% end %>


  <div class="field">
    <%= f.label :contact_id%><br />
    <%= select_tag :contact_id, options_from_collection_for_select(@allcontacts, "id", "name", params[:contact_id]), :prompt => "Search By Contact", include_blank: false %>
  </div>

    <div class="field">
      <%= f.label :priority%><br />
      <%= number_field_tag :priority, nil, in: 1..10 %>
    </div>

    <div class="field">
      <%= f.label :role_id %><br />
      <%= select_tag :role_id, options_from_collection_for_select(@allroles, "id", "title", params[:role_id]), :prompt => "Search By Role", include_blank: false %>
    </div>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>
4

1 回答 1

0

我必须单独手动获取参数。
这是我在创建操作下添加的代码

 @region_contact = RegionContact.new
    @region_contact.region_id = params[:region_id]
    @region_contact.contact_id = params[:contact_id]
    @region_contact.priority = params[:priority]
    @region_contact.role_id = params[:role_id]
于 2013-11-05T22:18:30.313 回答