用户中的NoMethodError#mypage undefined方法`model_name'代表nil:NilClass

时间:2016-02-19 19:12:02

标签: ruby-on-rails ruby ruby-on-rails-4

我的代码中的这一行:

<%= simple_form_for @category, :url => url_for(:action => 'bookfinal', :controller => 'users'), :method => 'post' do |f| %>

给我这个错误:

  

用户#Minpage中的NoMethodError未定义方法`model_name'for   零:NilClass

我不确定为什么?请参阅以下完整代码:

用户/ mypage.html.erb

<div id="widepage">
  <div class="bpage">     
    <div class="bcol">      
      <div class="blue-text-header">Book</div>
      <div class="grey-text-subheader">Ask a question</div>

      <br><br>
      <div class="infoheaders">1. Question
        <%= simple_form_for @category, :url => url_for(:action => 'bookfinal', :controller => 'users'), :method => 'post' do |f| %>
          <%= f.collection_select(:title, Category.all, :title, :title) %>
          <div class="grey-text-subheader">Device: (optional)</div>
          <%= f.check_box :Apple %> <%= f.label "Apple" %> &nbsp; &nbsp; 
          <%= f.check_box :iPhone %> <%= f.label "iPhone" %> &nbsp; &nbsp;  
          <%= f.check_box :iPad %> <%= f.label "iPad" %> &nbsp; &nbsp; 
          <%= f.check_box :Mac %> <%= f.label "Mac" %> &nbsp; &nbsp;  
          <%= f.check_box :Android %> <%= f.label "Android" %><br><br>
          <%= f.check_box :Samsung %> <%= f.label "Samsung" %> &nbsp; &nbsp;  
          <%= f.check_box :Microsoft %> <%= f.label "Microsoft" %>&nbsp; &nbsp; 
          <%= f.check_box :Windows %> <%= f.label "Windows" %> &nbsp; &nbsp; 
          <%= f.check_box :Google %> <%= f.label "Google" %> 
          <%= f.button :submit, 'Submit' %>
        <% end %>
      </div>


      <div class="bcol">        
        <div class="card">          
          <div class="columns">
            <div class="col">
              <%= image_tag @user.avatar.url(:thumb), :class => 'round-image-125' %>            
              <br><br>

              <font color="#ee4d50" font size="3">
                <b>Services:</b>
              </font>
            </div>            

            <div class="coltechie">
              <div class="techie-name">
              <%= @user.firstname %><%= @user.lastname %>              
              </div>
            </div>

            <br>
            <font color="#4d4d4d" font size="3">
              <b><%= @user.city%>, <%= @user.stateprov%></b>
            </font>

            <br><br>
            <font color="#2c78d3" font size="4">
              <b>Bio:</b>
            </font> 

            <br><br>
            <%= @user.bio%>
            <br><br>

            <font color="#2c78d3" font size="4">
              <b>Categories:</b>
            </font>
          </div>          
        </div>        
      </div>    
    </div>    
  </div>
</div>

users_controller.rb

class UsersController < ApplicationController

  def mypage
    @user = User.new
    @category = Category.new
  end

  def bookfinal
    if params[:category][:ipad].present?
      @checked = params[:category][:ipad]
    end  
    if params[:category][:ipad].present?
      @checked = params[:category][:android]
    end
    if params[:category][:ipad].present?
      @checked = params[:category][:samsung]
    end
  end

end

的routes.rb

get "users/bookfinal"

控制台:

Started POST "/users/bookfinal" for 96.49.105.234 at 2016-02-19 20:19:02 +0000
Processing by UsersController#bookfinal as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"pVchU8SD2MuypducuRPLcWbFwMg1yXIJWoKNVbRoDj1n5LlQIGhtQsOxsZ3MoO1uUaMe9rnVqVYqaECURW+OfA==", "category"=>{"title"=>"Digital Marketing", "Apple"=>"1", "iPhone"=>"0", "iPad"=>"0", "Mac"=>"0", "Android"=>"0", "Samsung"=>"0", "Microsoft"=>"0", "Windows"=>"0", "Google"=>"0"}, "commit"=>"Submit"}
  Rendered users/bookfinal.html.erb within layouts/application (0.4ms)
Completed 200 OK in 75ms (Views: 73.8ms | ActiveRecord: 0.0ms)

0 个答案:

没有答案