我正在尝试构建一个具有功能性论坛的 Rails 应用程序。然而,唯一可用的工具是用于称为 2.3 的古老版本的 Rails。我不仅在寻找这样的教程,而且还在寻找一种建立一些知识的方法。我只有一个每个人都使用的论坛,但也允许多个 Maintopic 帖子带有子主题。
所以有些问题已经解决了,但现在我正在寻找对命令的更好理解。
文件编辑于 9 月 30 日 现在我有一个新问题。我收到异常错误。它是一种涉及更新和创建功能的功能。我需要一些关于如何解决它们的想法。
错误
NoMethodError in SubtopicsController#update
undefined method `subtopic' for #<Maintopic:0x007f10b80cf858>
Rails.root: /home/eric/tobenamed.git/Trial
Application Trace | Framework Trace | Full Trace
app/controllers/subtopics_controller.rb:67:in `update'
Request
Parameters:
{"utf8"=>"✓",
"_method"=>"put",
"authenticity_token"=>"lVu6uOycUuY/BJmwvFJi3t7brmYVFWkeZQsnJID3Ve0=",
"subtopic"=>{"main_id"=>"1",
"user_id"=>"3",
"topicname"=>"Jimjon",
"description"=>"Beeeeee12"},
"commit"=>"Update Subtopic",
"maintopic_id"=>"1",
"id"=>"4"}
Show session dump
Show env dump
Response
Headers:
None
从我的计算机上的 Rake Routes 返回的 Rake 文件
eric@ubuntu:~/tobenamed.git/Trial$ rake routes
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
narratives GET /narratives(.:format) narratives#index
POST /narratives(.:format) narratives#create
new_narrative GET /narratives/new(.:format) narratives#new
edit_narrative GET /narratives/:id/edit(.:format) narratives#edit
narrative GET /narratives/:id(.:format) narratives#show
PUT /narratives/:id(.:format) narratives#update
DELETE /narratives/:id(.:format) narratives#destroy
maintopic_subtopics GET /maintopics/:maintopic_id/subtopics(.:format) subtopics#index
POST /maintopics/:maintopic_id/subtopics(.:format) subtopics#create
new_maintopic_subtopic GET /maintopics/:maintopic_id/subtopics/new(.:format) subtopics#new
edit_maintopic_subtopic GET /maintopics/:maintopic_id/subtopics/:id/edit(.:format) subtopics#edit
maintopic_subtopic GET /maintopics/:maintopic_id/subtopics/:id(.:format) subtopics#show
PUT /maintopics/:maintopic_id/subtopics/:id(.:format) subtopics#update
DELETE /maintopics/:maintopic_id/subtopics/:id(.:format) subtopics#destroy
maintopics GET /maintopics(.:format) maintopics#index
POST /maintopics(.:format) maintopics#create
new_maintopic GET /maintopics/new(.:format) maintopics#new
edit_maintopic GET /maintopics/:id/edit(.:format) maintopics#edit
maintopic GET /maintopics/:id(.:format) maintopics#show
PUT /maintopics/:id(.:format) maintopics#update
DELETE /maintopics/:id(.:format) maintopics#destroy
comments GET /comments(.:format) comments#index
POST /comments(.:format) comments#create
new_comment GET /comments/new(.:format) comments#new
edit_comment GET /comments/:id/edit(.:format) comments#edit
comment GET /comments/:id(.:format) comments#show
PUT /comments/:id(.:format) comments#update
DELETE /comments/:id(.:format) comments#destroy
fights GET /fights(.:format) fights#index
POST /fights(.:format) fights#create
new_fight GET /fights/new(.:format) fights#new
edit_fight GET /fights/:id/edit(.:format) fights#edit
fight GET /fights/:id(.:format) fights#show
PUT /fights/:id(.:format) fights#update
DELETE /fights/:id(.:format) fights#destroy
pet_items GET /pet_items(.:format) pet_items#index
POST /pet_items(.:format) pet_items#create
new_pet_item GET /pet_items/new(.:format) pet_items#new
edit_pet_item GET /pet_items/:id/edit(.:format) pet_items#edit
pet_item GET /pet_items/:id(.:format) pet_items#show
PUT /pet_items/:id(.:format) pet_items#update
DELETE /pet_items/:id(.:format) pet_items#destroy
inventories GET /inventories(.:format) inventories#index
POST /inventories(.:format) inventories#create
new_inventory GET /inventories/new(.:format) inventories#new
edit_inventory GET /inventories/:id/edit(.:format) inventories#edit
inventory GET /inventories/:id(.:format) inventories#show
PUT /inventories/:id(.:format) inventories#update
DELETE /inventories/:id(.:format) inventories#destroy
pet_owners GET /pet_owners(.:format) pet_owners#index
POST /pet_owners(.:format) pet_owners#create
new_pet_owner GET /pet_owners/new(.:format) pet_owners#new
edit_pet_owner GET /pet_owners/:id/edit(.:format) pet_owners#edit
pet_owner GET /pet_owners/:id(.:format) pet_owners#show
PUT /pet_owners/:id(.:format) pet_owners#update
DELETE /pet_owners/:id(.:format) pet_owners#destroy
items GET /items(.:format) items#index
POST /items(.:format) items#create
new_item GET /items/new(.:format) items#new
edit_item GET /items/:id/edit(.:format) items#edit
item GET /items/:id(.:format) items#show
PUT /items/:id(.:format) items#update
DELETE /items/:id(.:format) items#destroy
pets GET /pets(.:format) pets#index
POST /pets(.:format) pets#create
new_pet GET /pets/new(.:format) pets#new
edit_pet GET /pets/:id/edit(.:format) pets#edit
pet GET /pets/:id(.:format) pets#show
PUT /pets/:id(.:format) pets#update
DELETE /pets/:id(.:format) pets#destroy
Config 中的 Routes Table 显示正在运行的不同表。然而,重点放在了子主题和主主题资源上。
Trial::Application.routes.draw do
resources :users
resources :narratives
resources :maintopics do
resources :subtopics
end
resources :comments
resources :fights
resources :pet_items
resources :inventories
resources :pet_owners
resources :items
resources :pets
end
子主题控制器
class SubtopicsController < ApplicationController
# GET /subtopics
# GET /subtopics.json
#before_filter :load_forum
def index
@maintopic = Maintopic.find(params[:maintopic_id])
@subtopics = Subtopic.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @subtopics }
end
end
# GET /subtopics/1
# GET /subtopics/1.json
def show
@maintopic = Maintopic.find(params[:maintopic_id])
@subtopic = Subtopic.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @subtopic }
end
end
# GET /subtopics/new
# GET /subtopics/new.json
def new
# @maintopic = Maintopic.find_by_id(params[:main_id])
# @subtopic = @maintopic.subtopics.build
# respond_to do |format|
# format.html # new.html.erb
# format.json { render json: @subtopic }
# end
@maintopic = Maintopic.find(params[:maintopic_id])
@subtopic = @maintopic.subtopics.build
end
# GET /subtopics/1/edit
def edit
@maintopic = Maintopic.find(params[:maintopic_id])
@subtopic = Subtopic.find(params[:id])
#@subtopic = @maintopic.subtopics.build
end
# POST /subtopics
# POST /subtopics.json
def create
# @maintopic = Maintopic.find_by_id(params[:main_id])
@maintopic = Maintopic.find(params[:maintopic_id])
@subtopic = @maintopic.subtopics.new(params[:subtopic])
@subtopic.save
#if @subtopic.save
# redirect_to @maintopic.subtopic
#else
# render "new";
#end
end
# PUT /subtopics/1
# PUT /subtopics/1.json
def update
@subtopic = Subtopic.find(params[:id])
respond_to do |format|
if @subtopic.update_attributes(params[:subtopic])
format.html { redirect_to @subtopic, notice: 'Subtopic was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @subtopic.errors, status: :unprocessable_entity }
end
end
end
# DELETE /subtopics/1
# DELETE /subtopics/1.json
# def destroy
# @subtopic = Subtopic.find(params[:id])
# @subtopic.destroy
# respond_to do |format|
# format.html { redirect_to subtopics_url }
# format.json { head :no_content }
# end
# end
private
def load_forum
if (params[:id])
@subtopic = Subtopic.find(params[:id])
@maintopic = @subtopic.maintopic #rescue redirect_to(maintopics_path)
elsif (params[:forum_id])
@maintopic = Maintopic.find(params[:maintopic_id])
else
#redirect_to(maintopics_path)
#echo "I am here."
end
end
end
子主题的新视图页面
<h1>New subtopic</h1>
<%= render 'form' %>
<%#= link_to 'Back', subtopics_path %>
<%= link_to 'Back', maintopics_path %>
子主题的表单视图页面
<%= form_for([@maintopic,@subtopic]) do |f| %>
<% if @subtopic.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@subtopic.errors.count, "error") %> prohibited this subtopic from being saved:</h2>
<ul>
<% @subtopic.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :main_id %><br />
<%= f.number_field :main_id %>
</div>
<div class="field">
<%= f.label :user_id %><br />
<%= f.number_field :user_id %>
</div>
<div class="field">
<%= f.label :topicname %><br />
<%= f.text_field :topicname %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
主题控制器
class MaintopicsController < ApplicationController
# GET /maintopics
# GET /maintopics.json
def index
@maintopics = Maintopic.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @maintopics }
end
end
# GET /maintopics/1
# GET /maintopics/1.json
def show
@maintopic = Maintopic.find(params[:id])
@subtopic = @maintopic.subtopics.all
# respond_to do |format|
# format.html # show.html.erb
# format.json { render json: @maintopic }
# end
end
# GET /maintopics/new
# GET /maintopics/new.json
def new
@maintopic = Maintopic.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @maintopic }
end
end
# GET /maintopics/1/edit
def edit
@maintopic = Maintopic.find(params[:id])
end
# POST /maintopics
# POST /maintopics.json
def create
@maintopic = Maintopic.new(params[:maintopic])
respond_to do |format|
if @maintopic.save
format.html { redirect_to @maintopic, notice: 'Maintopic was successfully created.' }
format.json { render json: @maintopic, status: :created, location: @maintopic }
else
format.html { render action: "new" }
format.json { render json: @maintopic.errors, status: :unprocessable_entity }
end
end
end
# PUT /maintopics/1
# PUT /maintopics/1.json
def update
@maintopic = Maintopic.find(params[:id])
respond_to do |format|
if @maintopic.update_attributes(params[:maintopic])
format.html { redirect_to @maintopic, notice: 'Maintopic was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @maintopic.errors, status: :unprocessable_entity }
end
end
end
# DELETE /maintopics/1
# DELETE /maintopics/1.json
def destroy
@maintopic = Maintopic.find(params[:id])
@maintopic.destroy
respond_to do |format|
format.html { redirect_to maintopics_url }
format.json { head :no_content }
end
end
end
用户首次访问此页面时应正确显示的主要主题的索引视图
<h1>Listing maintopics</h1>
<table>
<% @maintopics.each do |maintopic| %>
<tr>
<td>Topic: <%= link_to maintopic.topicname, maintopic %></td>
<td><%= link_to 'Edit', edit_maintopic_path(maintopic) %></td>
<td><%= button_to 'Destroy', maintopic, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<tr>
<td><%= maintopic.description %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Maintopic', new_maintopic_path %>
正在运行的 Maintopics 的显示视图
<p id="notice"><%= notice %></p>
<h1>Listing maintopics</h1>
<table>
<% @maintopic.subtopics.each do |subtopic| %>
<tr>
<td>Topic: <%= link_to subtopic.topicname %></td>
<td><%#= link_to 'Edit', edit_maintopic_subtopic_path(subtopic) %></td>
<td><%#= button_to 'Destroy', subtopic, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<tr>
<td><%#= subtopic.description %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to "New Subtopic", new_maintopic_subtopic_path(@maintopic) %>