2013-09-29 45 views
0

我正在嘗試構建一個可以運行論壇的rails應用程序。然而,那裏唯一的工具是古老的Rails 2.3版本。我正在尋找的不僅是尋找這樣的教程,而且也是一種建立一些知識的方法。我只有一個人人使用的論壇,但是也有多個Maintopic帖子可以與子帖子一起使用。在Rails 3.2.13構建一個論壇

所以一些問題已經得到解決,但現在我正在尋求更好的理解命令。

文件編輯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 

是從耙路線返回我的計算機上的耙文件

[email protected]:~/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 

從配置的路由表,它表明屬於不同的表正在運行。但是,重點正在放在副標題和主題資源上。

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 

的Maintopics的索引視圖應正確地顯示在用戶第一次訪問這個頁面

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

回答

0

我認爲這個問題是因爲你的副標題是嵌套在maintopic所以它需要maintopic ID,你看到你的耙路線導致以下行

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

(new_maintopic_subtopic GET/maintopics /:maintopic_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 

在您的項目請不要改變按您的要求。 和無論你需要什麼幫助,請問我。

+0

好的,我會試試看。 :)有些東西告訴我,相同的語法問題可以應用於edit_maintopic_subtopic_path。我遇到了同樣的問題,試圖編輯文件。你能通過控制器的一些命令來引導我嗎?我想了解更多關於他們以及他們爲什麼被使用的順序。 – Eric

+0

我已經創建了一個示例項目,它工作正常,你可以看看它。 –

+0

檢查我編輯的答案希望這會幫助你。 –