我对 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