无法访问新表列获取错误未定义方法名称app_name

时间:2015-11-22 19:14:51

标签: ruby-on-rails forms ruby-on-rails-4

我添加了另一个名为Application的表,我在其中引用了Wiki表。我正在尝试将Application表的app_name列添加到new.html.erb表单中,以便用户在提交表单时可以在其中添加应用程序名称。我得到“未定义的方法app_name”。如何在index.html.erb文件中正确访问列app_name?看来wiki.app_name不是在index.html.erb文件中访问app_name的正确方法吗?

错误消息:

Wiki中的NoMethodError#new

new.html.erb文件:

<h1>New Bug Defect</h1>


  <div class="form-group">
    <%= form_for @wiki do |f| %>
    <%= f.label :title %>
    <%= f.text_field :title, class: 'form-control', placeholder: "Enter defect title" %>
  </div>

  <div class="form-group">
  <%= f.label :app_name %>
  <%= f.text_field :app_name, class: 'form-control', placeholder: "Application affected" %>
   </div>

  <div class="form-group">
  <%= f.label :body %>
  <%= f.text_area :body, rows: 33, class: 'form-control', placeholder: "Place your defect entry"%>
  </div>


   <div class="form-group">
   <% if current_user.can_make_private_wiki? %>
    <%= f.label :private, class: "checkbox" %>
    <%= f.check_box :private %>
   <% end %>

   </div> 



  <div class="form-group">
  <%= f.submit  "Save", class: 'btn btn-success' %>
  </div>
  <% end %>

模式

create_table "applications", force: :cascade do |t|
t.string   "app_name"
t.string   "dev_resource"
t.integer  "wikis_id"
t.datetime "created_at",   null: false
t.datetime "updated_at",   null: false
end

add_index "applications", ["wikis_id"], name: "index_applications_on_wikis_id"


  create_table "wikis", force: :cascade do |t|
   t.string   "title"
   t.text     "body"
   t.boolean  "private",    default: false
   t.integer  "user_id"
   t.datetime "created_at",                 null: false
   t.datetime "updated_at",                 null: false
   end

   add_index "wikis", ["user_id"], name: "index_wikis_on_user_id"

显示应用名称字段的Index.html.erb文件

 h1>List of Bug Defects</h1>
<table class="table">
 <tr> 
  <th colspan="0">
  <h2>Defects</h2>
   </th>  
  </tr>
   <tr>
  <th>Title</th>
   <th>Description</th>
   <th>Application</th>
   <th>Link</th>
   <th>Opened by</th>
   </tr>
    <% unless @wikis.nil? %>
    <% @wikis.each do |wiki| %> 
       <tr>
      <td><%= wiki.title %></td>
      <td><%= wiki.body %></td>
      <td><%= wiki.app_name %></td>
      <td><%= link_to wiki.title, wiki_path(wiki)%></td>
      <td><%= wiki.user.name if wiki.user %></td>
      </tr>
         <% end %>
         <% end %>
    </table>

Wiki控制器:

 class WikisController < ApplicationController
 before_action :find_wiki, only: [:show, :edit, :update, :destroy]

  def index
   @wikis = policy_scope(Wiki)
  end

  def show
   authorize @wiki
  end

  def new
   @wiki = Wiki.new
   authorize @wiki
  end

  def create
   @wiki = Wiki.new(wiki_params)
   @wiki.user_id = current_user.id
   authorize @wiki

   if @wiki.save
   flash[:notice] = "Your wiki has been saved."
   redirect_to @wiki
   else
   flash[:error] = "Something went wrong."
   render :new
   end
  end

  def edit
   @collaborators = @wiki.collaborators
   @new_collaborator = Collaborator.new
   #authorize @wiki
  end

  def update
   #@wiki.user_id = current_user
   authorize @wiki
    if @wiki.update_attributes(wiki_params)
     flash[:notice] = "Wiki is updated"
     redirect_to @wiki
    else 
     flash[:error] = "There was an error saving the wiki. Please try again."
     render :edit
    end
    end

    def destroy

    if @wiki.destroy
     flash[:notice] = "Wiki deleted successfully"
     redirect_to wikis_path
    else
     flash[:error] = "Something went wrong"
    render :show
    end
    end

  private

   def find_wiki
    @wiki = Wiki.find(params[:id])
   end

   def wiki_params
   params.require(:wiki).permit(:body, :title, :private)
   end
 end

1 个答案:

答案 0 :(得分:0)

为了让生活更轻松,我将列添加到现有的Wiki表中。它现在正常工作:

rails g  migration AddApplicationToWikis application:string

更新了维基表:

create_table "wikis", force: :cascade do |t|
t.string   "title"
t.text     "body"
t.boolean  "private",     default: false
t.integer  "user_id"
t.datetime "created_at",                  null: false
t.datetime "updated_at",                  null: false
t.string   "application"
end

add_index "wikis", ["user_id"], name: "index_wikis_on_user_id"