0

我对 RoR 比较陌生,并且一直在研究课程站点共享后端。

目前我有两个模型:课程和修订。这个想法是教师将在课程/新页面上创建课程(带有标题、描述等)。该表单将重定向到另一个表单,该表单在课程/课程 ID/修订/新页面上具有其他字段(内容、评论)。最终,教师将能够通过将修订内容复制到他们自己的个人资料中来“保存和修改”课程,但该模型尚不存在。

在创建新课程和修订方面,我仍然专注于两个所谓的基本问题。

1) 将课程保存给用户,并修改给用户和课程。有一次,我将修订保存给用户,但课程并没有保存。

2) 从课程表重定向到修订表,将修订表重定向到修订页面。我尝试更改控制器中的重定向和 form_for 块之一,但无济于事。新的课程表仍然希望转到 /lesson 而不是 /lesson/1/revision/new,我尝试使用 new_lesson_revision_path 将其重定向到。修订表格也只是回到 /revisions,这将是一个管理索引,但我宁愿去 /lesson/1/revision/1 等。

提前感谢您就这两个问题中的一个或两个问题提供任何建议!

下面是我的代码供参考。来自 routes.rb 的摘录:

resources :lessons
resources :revisions #For index pages for admin
resources :lessons do
  resources :revisions
end

楷模

class Lesson < ActiveRecord::Base
  attr_accessible :stable, :summary, :title, :time_created
  has_many :revisions, :class_name => "Revision"
  has_many :users, :through => :revisions
end

class Revision < ActiveRecord::Base
  attr_accessible :comment, :lesson_id, :user_id, :description, :content, :time_updated
  belongs_to :lesson, :class_name => "Lesson", :foreign_key => "lesson_id"
  belongs_to :user, :class_name => "User", :foreign_key => "user_id"

  accepts_nested_attributes_for :lesson
end

课程 new.html.erb

<% if user_signed_in? %>
  <h2>New lesson</h2>
  <%= form_for @lesson do |f| %>
  <%= render "form" %>
  <% end %>
  <%= link_to 'Back', lessons_path %>
<% else %>
<h3> Please <%= link_to 'sign in', new_user_session_path %> or <%= link_to 'create an account', new_user_registration_path %> to make and save lessons.</h3>
<% end %>

课程_form.html.erb

<%= form_for @lesson, :url => new_lesson_revision_path(@lesson_id, @revision) do |f| %>

  <div class="field">
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :summary %><br />
    <%= f.text_area :summary %>
  </div>
  <div class="field">
    <%= f.label :stable %><br />
    <%= f.check_box :stable %>
  </div>

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

修订 new.html.erb

<% if user_signed_in? %>
  <h2>New revision</h2>
  <%= form_for @revision do |f| %>
    <%= render "form" %>
  <% end %>
  <%= link_to 'Back', lessons_path %>
<% else %>
  <h3> Please <%= link_to 'sign in', new_user_session_path %> or <%= link_to 'create an account', new_user_registration_path %> to make and save revisions.</h3>
<% end %>

修订 _form.html.erb

<%= form_for(@revision) do |f| %>

  <div class="field">
    <%= f.label :description %><br />
    <%= f.text_field :description %>
  </div>
  <div class="field">
    <%= f.label :content %><br />
    <%= f.text_area :content %>
  </div>
  <div class="field">
    <%= f.label :comment %><br />
    <%= f.text_area :comment %>
  </div>

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

课程控制器:新建/创建

  def new
    @lesson = Lesson.new
  end

  def create
    @user = current_user
    @lesson = Lesson.new(params[:lesson])
    #@user.lessons << @lesson
    params[:lesson][:user_id] = current_user.id
    params[:lesson][:lesson_id] = @lesson.id
    respond_to do |format|
      if @lesson.save
        format.html { redirect_to new_lesson_revision_path(params[:lesson_id]), notice: 'Your lesson was successfully created.' }
        format.json { render json: @lesson, status: :created, location: new_lesson_revision_path}
      else
        format.html { render action: "new" }
        format.json { render json: @lesson.errors, status: :unprocessable_entity }
      end
    end
  end

修订控制器:新建/创建

  def new
    @revision = Revision.new
  end
  def create
    @lesson = Lesson.find(params[:lesson_id])
    @revision = @lesson.revisions.new(params[:revision])
    @revision.user_id = current_user.id
    @revision.lesson_id = @lesson.id
    @revision.time_updated = DateTime.now
    respond_to do |format|
      if @revision.save
       format.html { redirect_to current_user.profile }
       format.json { }
      else
      render :new
      end
    end
  end

最后,这是我的 rake 路线的大部分结果:

          home_index GET    /home/index(.:format)                            home#index
             profile POST   /profile(.:format)                               profiles#create
         new_profile GET    /profile/new(.:format)                           profiles#new
        edit_profile GET    /profile/edit(.:format)                          profiles#edit
                     GET    /profile(.:format)                               profiles#show
                     PUT    /profile(.:format)                               profiles#update
                     DELETE /profile(.:format)                               profiles#destroy
             lessons GET    /lessons(.:format)                               lessons#index
                     POST   /lessons(.:format)                               lessons#create
          new_lesson GET    /lessons/new(.:format)                           lessons#new
         edit_lesson GET    /lessons/:id/edit(.:format)                      lessons#edit
              lesson GET    /lessons/:id(.:format)                           lessons#show
                     PUT    /lessons/:id(.:format)                           lessons#update
                     DELETE /lessons/:id(.:format)                           lessons#destroy
    lesson_revisions GET    /lessons/:lesson_id/revisions(.:format)          revisions#index
                     POST   /lessons/:lesson_id/revisions(.:format)          revisions#create
 new_lesson_revision GET    /lessons/:lesson_id/revisions/new(.:format)      revisions#new
edit_lesson_revision GET    /lessons/:lesson_id/revisions/:id/edit(.:format) revisions#edit
     lesson_revision GET    /lessons/:lesson_id/revisions/:id(.:format)      revisions#show
                     PUT    /lessons/:lesson_id/revisions/:id(.:format)      revisions#update
                     DELETE /lessons/:lesson_id/revisions/:id(.:format)      revisions#destroy
                     GET    /lessons(.:format)                               lessons#index
                     POST   /lessons(.:format)                               lessons#create
                     GET    /lessons/new(.:format)                           lessons#new
                     GET    /lessons/:id/edit(.:format)                      lessons#edit
                     GET    /lessons/:id(.:format)                           lessons#show
                     PUT    /lessons/:id(.:format)                           lessons#update
                     DELETE /lessons/:id(.:format)                           lessons#destroy
                root        /                                                home#index
                            /profile(.:format)                               profiles#show
4

1 回答 1

1

Your question : 1) Saving the lessons to a user, and revisions to a user and lesson. At one point I had revisions saved to users, but lessons were not saving.

看你的lessons_form

<%= form_for @lesson, :url => new_lesson_revision_path(@lesson_id, @revision) do |f| %>

您正在使用new_lesson_revision_path操作表单,这不是保存 a @lesson,请将您的更改lessons_form为:

<%= form_for @lesson do |f| %>

看起来 lessons_controller.rb像:

  def new
    @lesson = Lesson.new
  end

  def create
    @user = current_user
    @lesson = Lesson.new(params[:lesson]) 
    ....
    @lesson.save
    # see, redirect_to new lesson with params[lesson_id]
    redirect_to new_lesson_revision_path(params[:lesson_id])
 end

并且revisions_controller.rb看起来像:

  def new
    @revision = Revision.new
    # get @lesson with params(:lesson_id)
    @lesson = Lesson.find(params[:lesson_id])
  end
  def create
   @revision = @revision.new(params[:revision])
   @revision.user_id = current_user.id
   @revision.time_updated = DateTime.now
     respond_to do |format|
       if @revision.save
         format.html { redirect_to current_user.profile }
         format.json { }
       else
        render :new
        end
      end
  end

在您的 上revisions_form,您可以lesson_id添加hidden_field

<%= form_for(@revision) do |f| %>
  <%= f.hidden_field :lesson_id, :value => @lesson.id %>
  ..
  ..
<% end %>

no 2

为什么不应该使用嵌套表单或多步表单?

  • 如果您使用嵌套表格,您可以将课程表格和修订表格制作为一种表格。

    有关嵌套形式,请参阅截屏视频第 1部分和 第 2 部分

  • 如果您使用的是多步表单,则可以解决您的问题no 2

    请参阅截屏向导表单

附加的

error: undefined method 'revisions' for nil:NilClass - from the line @revision = @lesson.revisions.new(params[:revision])

因为@revision在你的情况下是独立的,你可以改变这个

@revision = @lesson.revisions.new(params[:revision])

@revision = @revision.new(params[:revision])

于 2013-05-23T06:44:35.630 回答