0

EDIT2: [解決済み] コントローラー ファイルの「作成」メソッドは、「編集」メソッドの後まで「終了」しません。ただし、8時間後まで自分の質問に答えることができません.

編集:これは、routes.rb ファイルに問題がある可能性があることに注意してください。これはすぐ下にあります。

ルート.rb

SimpleCms::Application.routes.draw do

  root :to => "subjects#list"

  match ':controller(/:action(/:id))(.:format)'
end

基本的に、これは @instance_variable の問題です。より具体的には @instance_variable[:id => nil] 問題です。どういうわけか、nil (null) :id (主キー) 値をレールに渡していますが、クリックしている編集ボタン (このリストの最後のファイルは list.html.erb ファイルですedit.html.erb へ) は、​​リスト ページの特定の件名に対応する :id 値を渡すことになっています。

したがって、まずブラウザからのエラーメッセージは次のとおりです。

 RuntimeError in Subjects#edit

Showing C:/Users/davo/Desktop/RailsProjects/simple_cms/app/views/subjects/edit.html.erb     where line #6 raised:

Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil,     use object_id
Extracted source (around line #6):

3: <div class="subject edit">
4:   <h2>Update Subject</h2>
5: 
6:   <%=  form_for(:subject, :url => {:action => 'update', :id => @subject.id}) do |f| %>
7: 
8:       <table summary="Subject form fields">
9:         <tr>
Rails.root: C:/Users/davo/Desktop/RailsProjects/simple_cms

Application Trace | Framework Trace | Full Trace
app/views/subjects/edit.html.erb:6:in     `_app_views_subjects_edit_html_erb__829468061_51428148'
Request

Parameters:

{"id"=>"1"}

これがコントローラー ("subjects_controller.rb") で、インスタンス変数の内容を確認できます。edit メソッドと update メソッドは一番下にあります **params[:id] を呼び出すその他のメソッドはすべて機能しています。コントローラーに問題はなく、単なるビューである可能性があります。特に、そのビューでは、 @subject 対 :subject 対 subject.method のもの...その構文に何か問題があるようです。

class SubjectsController < ApplicationController

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

  def index
    list
    render('list')
  end

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

  def new
    @subject = Subject.new(:name => 'default')
  end
  def create
    #instantiate a new object using form params
    @subject = Subject.new(params[:subject])
    #save the subject
    if @subject.save
      redirect_to(:action => 'list')
      #if save succeeds redirect to list action
    else
      #if save fails, redisplay form
      render('new')
    end
  def edit
    @subject = Subject.find(params[:id])
  end
  #passing an @instancevariable inside of a {hash}...is there anything odd about that?
  #I'm just trying to inspire you guys -->
  #
  #I have a hunch that the :syntax/@syntax/#{etc} involving params, :id and
  #:subject could be the root of this issue...just a hunch
  #
  def update
    #Find object using form parameters
    @subject = Subject.find(params[:id])
    #Update the object
    if @subject.update_attributes(params[:subject])
      redirect_to(:action => 'show', :id => @subject.id)
    else
      render('edit')
    end
  end
  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>
      <tr>
        <td><%= f.submit 'Update Subject' %></td>
      </tr>
  <% end %>
</div>

編集: これは list.html.erb ファイルです。これには edit.html.erb ファイルへのリンクが含まれていますが、現在、このファイルは開かれず、エラー メッセージが表示されます。

<html>
    <div class="subject list">
      <h2>Subjects</h2>

      <!--- header row with all the different attributes --->
      <table class="listing" summary="Subject list">
        <tr class="header">
          <th>#</th>
          <th>Subject</th>
         <th>Visible</th>
          <th>Pages</th>
          <th>Actions</th>
        </tr>
        <!-- this is the beginning of a loop, that ends way down there -->
        <% @subjects.each do |subject| %>
        <tr>
          <td><%=  subject.position %></td>
          <td><%=  subject.name %></td>
          <td class="center"><%= subject.visible ? 'Yes' : 'No' %></td>
          <td class="center"><%=  subject.pages.size %></td>
          <td class="actions">
            <%=  link_to("Show", {:action => 'show', :id => subject.id}, :class => 'action show') %>
            <%= link_to("Edit", {:action => 'edit', :id => subject.id}, :class => 'action edit') %>
            <%=  link_to("Delete", '#', :class => 'action delete') %>
          </td>
            <% end %>
        </tr>
        </table>
        <%= button_to "Create New Subject", {:action => 'new'}, {:class => "buttonTo"} %>
    </div>
</html>

編集: - 特定の model.rb ファイルが必要な場合はお知らせください。私のモデル フォルダーには、「subject.rb」、「section.rb」、「section_edit.rb」、「page.rb」、および「admin_user.rb」があり、これらのいずれかを表示する必要があります。私はこれにちょっと困惑しているので、多分それらは役に立つでしょう. それらにはすべて、一連のスキーマ (has_many、belongs_to など) 命令といくつかのカスタム コンソール呼び出しが含まれています。

4

2 に答える 2

0
= form_for(@subject, :as => :subject, .....

レコードが見つからない場合、find は例外を発生させるため、@subject は nil ではありません

于 2012-05-20T01:05:56.540 に答える
0

コントローラーで def/end 構文の一部を実行するときにタイプミスをしました。「def create」だったと思いますが、終了しませんでした。

于 2012-05-21T02:33:09.363 に答える