在Rails中构建一个论坛3.2.13

时间:2013-09-29 22:50:57

标签: ruby-on-rails-3.2 forum

我正在尝试构建一个具有功能论坛的rails应用程序。然而,唯一的工具是古代版本的Rails 2.3。我不仅希望寻找这样的教程,而且还寻找建立一些知识的方法。我只有一个论坛,每个人都使用,但多个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中的路由表,显示正在运行的不同表。然而,重点放在Subtopics和Maintopics资源上。

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 %>

Maintopics控制器

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 %>

现在正在使用的主题的显示视图

<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) %>

1 个答案:

答案 0 :(得分:0)

我认为问题出在以下一行

<%= link_to "New Subtopic", new_maintopic_subtopic_path %>

因为您的子主题嵌套在maintopic中所以它需要maintopic id,如您在rake路由结果中看到的那样(new_maintopic_subtopic GET /maintopics/:ma​​intopic_id/subtopics/new(.:format))。 所以尝试使用以下内容创建新的子主题。

<%= link_to "New Subtopic", new_maintopic_subtopic_path(@maintopic) %>

请查看以下代码。

maintopics控制器

class MaintopicsController < ApplicationController
  before_action :set_maintopic, only: [:show, :edit, :update, :destroy]

  # GET /maintopics
  # GET /maintopics.json
  def index
    @maintopics = Maintopic.all
  end

  # GET /maintopics/1
  # GET /maintopics/1.json
  def show
    @subtopics = @maintopic.subtopics.all
  end

  # GET /maintopics/new
  def new
    @maintopic = Maintopic.new
  end

  # GET /maintopics/1/edit
  def edit
  end

  # POST /maintopics
  # POST /maintopics.json
  def create
    @maintopic = Maintopic.new(maintopic_params)

    respond_to do |format|
      if @maintopic.save
        format.html { redirect_to @maintopic, notice: 'Maintopic was successfully created.' }
        format.json { render action: 'show', status: :created, location: @maintopic }
      else
        format.html { render action: 'new' }
        format.json { render json: @maintopic.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /maintopics/1
  # PATCH/PUT /maintopics/1.json
  def update
    respond_to do |format|
      if @maintopic.update(maintopic_params)
        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.destroy
    respond_to do |format|
      format.html { redirect_to maintopics_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_maintopic
      @maintopic = Maintopic.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def maintopic_params
      params.require(:maintopic).permit(:text)
    end
end

子主题控制器

class SubtopicsController < ApplicationController

  def new
    @maintopic = Maintopic.find(params[:maintopic_id])
  end

  def create
    @maintopic = Maintopic.find(params[:maintopic_id])
    @subtopic = @maintopic.subtopics.create(params[:subtopic].permit(:heading, :body))
    redirect_to maintopics_path(@maintopic)
  end

  def edit
    #raise params.inspect
    @maintopic = Maintopic.find(params[:maintopic_id])
    @subtopic = Subtopic.find(params[:id])
  end

  def update
    @subtopic = Subtopic.find(params[:id])

    respond_to do |format|
      if @subtopic.update(subtopic_params)
        format.html { redirect_to maintopics_path, notice: 'Maintopic was successfully updated.' }
      else
        format.html { render action: 'edit' }
      end
    end
  end

  private
    # Never trust parameters from the scary internet, only allow the white list through.
    def subtopic_params
      params.require(:subtopic).permit(:heading, :body)
    end
end

主要主题展示页

<p id="notice"><%= notice %></p>

<p>
  <strong>Text:</strong>
  <%= @maintopic.text %>
</p>

<h2>Subtopic</h2>

<% @subtopics.each do |subtopic| %>
  <p>
  <strong>Heading:</strong>
  <%= subtopic.heading %>
</p>
<p>
  <strong>Body:</strong>
  <%= subtopic.body %>
</p>
<%= link_to 'Edit Subtopic', edit_maintopic_subtopic_path(@maintopic, subtopic) %>
<% end %>
<br>
<%= link_to 'Add Subtopic', new_maintopic_subtopic_path(@maintopic) %> |
<%= link_to 'Edit', edit_maintopic_path(@maintopic) %> |
<%= link_to 'Back', maintopics_path %>
maintopic模型中的

class Maintopic < ActiveRecord::Base
  has_many :subtopics, dependent: :destroy
end
子主题模型中的

class Subtopic < ActiveRecord::Base
  belongs_to :maintopic
end

请根据您的要求对项目进行更改。 无论你需要什么帮助,请问我。