【问题标题】:I'm learning RoR through lynda.com and i need help solving this issue我正在通过 lynda.com 学习 RoR,我需要帮助来解决这个问题
【发布时间】:2016-03-19 08:45:48
【问题描述】:

这是我遇到的错误

主题中的 NoMethodError#edit

显示 /home/ktutor/Documents/simple_code/app/views/subjects/edit.html.erb 其中第 7 行提出: nil:NilClass 的未定义方法“id” 提取的源代码(第 7 行附近):

     <h2>Update Subject</h2>

  <%= form_for(:subject, :url => {:action => 'update', :id => @subjects.id}) do |f| %>

   <table summary="Subject form fields">
       <tr>

Rails.root:/home/ktutor/Documents/simple_code 应用程序跟踪 |框架跟踪 |全跟踪 app/views/subjects/edit.html.erb:7:in `_app_views_subjects_edit_html_erb__3163959245167106416_44757760'

Request

Parameters:

{"id"=>"6"}

这是我的主题控制器

    class SubjectsController < ApplicationController

  def index
    list
    render('list')
  end   

  def list 
    @subjects = Subject.order("subjects.position ASC")
  end

  def show
     @subject = Subject.find(params[:id])
  end

  def new
     @subject = Subject.new(:Name => "")
  end

  def create
  #new_position = params[:subject].delete(:position)
  # Instantiate a new object using form parameters
  @subject = Subject.new(subject_params)
  # Save the object
 if @subject.save
  @subject.move_to_position(new_position)
  # If save succeeds, redirect to the list action
  flash[:notice] = "Subject Created."
  redirect_to(:action => 'list')
  else
  # If save fails, redisplay the form so user can fix problems
  @subject_count = Subject.count +1
  render('new')
  end  
end 

  private

  def subject_params 
  allow = [:name, :position, :visible, :created_at, :updated_at]
  params.require(:subject).permit(allow)
  end

  def edit 
   @subject = Subject.find(params[:id])
  end

  def update
  # find a new object using form parameters
  @subject = Subject.find.(params[:id])
  # Save the object
  if @subject.update_attributes(params[:subject])
  # If save update, redirect to the list action
  redirect_to(:action => 'show', :id => @subject.id)
  else
  # If save fails, redisplay the form so user can fix problems
  render('edit')
  end  
end 

  def edit 
   @subject = Subject.find.(params[:id])
  end    
end 

这是我的 edit.html.erb 文件

<%= link_to("<< Back to List", {:action => 'list'}, :class => 'back-link') %>

 <div class="subject edit">
   <h2>Update Subject</h2>

<%= form_for(:subject, :url => {:action => 'update', :id => @subject.id}) do |f| %>

   <table summary="Subject form fields">
       <tr>
         <th>Name</th>
         <td><%= f.text_field(:Name) %></td>
        </tr>
       <tr>
         <th>Position</th>
         <td><%= f.text_field(:position) %></td>
       </tr>
       <tr>
          <th>Visible</th>
          <td><%= f.text_field(:visible) %></td>
       </tr>
      </table>

  <div class="form-buttons">
    <%= submit_tag("Update Subject") %>
   </div>

  <% end %>  
</div>           

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    “编辑”方法被定义为私有。将其移至私有修饰符上方。

    同时从 url 字典中删除 id 并将其作为隐藏值添加到表单正文中。

    <%= form_for(:subject, :url => {:action => 'update'}) do |f| %>
    <%= f.hidden_field :id %>
    <table summary="Subject form fields">
       <tr>
         <th>Name</th>
         <td><%= f.text_field(:Name) %></td>
        </tr>
       <tr>
         <th>Position</th>
         <td><%= f.text_field(:position) %></td>
       </tr>
       <tr>
          <th>Visible</th>
          <td><%= f.text_field(:visible) %></td>
       </tr>
      </table>
    
    <div class="form-buttons">
      <%= submit_tag("Update Subject") %>
     </div>
    
    <% end %>
    

    【讨论】:

    • NoMethodError in Subjects#edit 显示 /home/ktutor/Documents/simple_code/app/views/subjects/edit.html.erb 其中第 7 行出现:未定义方法 `id' for nil:NilClass Extracted来源(第 7 行附近):5 6 7 8 9 10

      更新主题

      {:action => 'update', :id => @subjects .id}) 做 |f| %> Rails.root: /home/ktutor/Documents/simple_code
    • 更新主题

      {:action => 'update', :id => @subjects.id}) 做 | f| %>
    • 您应该将 id 添加为表单中的隐藏字段,并从 :url=>{} 字典中删除 :id=>@subjects.id。
    • /home/ktutor/Documents/simple_code/app/views/subjects/edit.html.erb:7:语法错误,意外')',期待'}' ...,:url = > {:action => '更新') 做 |f| @output_buffer.safe_a ... ... ^ /home/ktutor/Documents/simple_code/app/views/subjects/edit.html.erb:29:语法错误,意外的keyword_ensure,期待输入结束
    • 更新主题

      {:action => 'update') 做 |f| %>
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签