Rails:PostsController中的ArgumentError#创建错误的参数个数(1表示0)

时间:2016-05-21 16:45:59

标签: ruby-on-rails ruby devise

Github Repo 嗨,我是一个菜鸟,需要帮助。为我的训练班制作一本年鉴并有一些打嗝。目前的错误是:

ArgumentError in PostsController#create
wrong number of arguments (1 for 0)

Extracted source (around line #63):
61
62
63
64
65
66


  private
  def post_params
    params.require(:post).permit(:name, :body)
  end
end

screenshot of error message

Wdiers是我的用户。这些学生是学生和教师。目前,我只是想在wdier展示页面上添加帖子。我目前在页面上有一个帖子,用于来自我的种子文件的测试wdier。错误是在我尝试创建新帖子时。我能够进入表单页面并在提交帖子后出现错误。 (我最初发帖称为评论,但将其更改为帖子,并将评论作为对这些帖子的回复)

另外,要注意我正在使用设计,以防万一。

以下是我的网页:

WDIer控制器     class WdiersController< ApplicationController中

  def index
    @wdiers = Wdier.all
  end

  def new
    @wdier = Wdier.new
  end

  def create
    @wdier = Wdier.create!(wdier_params)

    redirect_to wdiers_path(@wdier)
  end

  def show
    @wdier = Wdier.find(params[:id])
  end

  def edit
    @wdier = Wdier.find(params[:id])
  end

  def update
    @wdier = Wdier.find(params[:id])
    @wdier.update(wdier_params)

    redirect_to wdiers_path(@wdier)
  end

  def destroy
    @wdier = Wdier.find(params[:id])
    @wdier.destroy

    redirect_to wdiers_path(@wdier)
  end

  def wdier_params
    params.require(:wdier).permit(:name, :img_url, :squad_name, :squad_id, :quote, :teaching, :memory, :favlang, :wisewords, :tag_list,:github_url, :portfolio_url, :project1_url, :project2_url, :project3_url, :quote, :q1, :q2, :q3, :fb, :linkedin, :email, :role,  :student_id, :instructor_id)
  end
end

发布控制器

class PostsController < ApplicationController

  def index
    @posts = Post.all
  end

  def show
    @wdier = Wdier.find(params[:wdier_id])
    @post = Post.find(params[:id])
  end

  def edit
    @wdier = Wdier.find(params[:wdier_id])
    @post = Post.find(params[:id])
  end

  def new
    @wdier = Wdier.find(params[:wdier_id])
    @post = Post.new
  end

  def create
    @wdier = Wdier.find(params[:wdier_id])
    @post = @wdier.posts.create!(post_params)
    @post = Post.new(params.require(:post).permit(:task))
    @post.save
    if @post.save
      flash[:alert] = "Post created successfully."
      redirect_to post_params([:wdier_id])
    else
      flash[:alert] = "Error creating post."
      redirect_to post_params([:wdier_id])
    end
  end


  def update
    @wdier = Wdier.find(params[:wdier_id])
    @post = Post.find(params[:id])
    if @post.user == current_user
      @post.update(wdier_params)
    else
      flash[:alert] = "Only the author of the post can edit it!"
    end
    redirect_to wdier_params(@wdier)
  end

  def destroy
    @wdier = Wdier.find(params[:wdier_id])
    @post = Post.find(params[:id])
    if @post.user == current_user
      @post.destroy
    else
      flash[:alert] = "Only the author of the post can delete"
    end
    redirect_to wdier_path(@wdier)
  end

  private
  def post_params
    params.require(:post).permit(:name,:body)
  end
end

发布模型

class Post < ActiveRecord::Base
  belongs_to :instructors
  belongs_to :students
  belongs_to :users
  belongs_to :wdiers
  has_many :comments
end

Wdier模型

class Wdier < ActiveRecord::Base
  belongs_to :squad
  belongs_to :instructors
  belongs_to :students
  has_one :codey
  has_many :comments
  has_many :posts
  has_many :negatives
  has_many :photos, through: :negatives
end

Wdier秀视图:

<section id="wdierShow">
<h1 class="page-header"><%= @wdier.name %></h1>
<div class="index">


  <section>
    <div class="button"><%= link_to "edit", edit_wdier_path(@wdier)%></h2></div>
  </br>
  <div class="profileImg">
  <%= image_tag @wdier.img_url %>
</div>
  <div class="topContainer">
    <!-- <h1><%= @wdier.name %></h1> -->
  <ul class="grid">
    <%= @wdier.squad_id %> </br>
    <%= @wdier.codey_id %> </br>
    <%= @wdier.quote %> </br>
    <%= @wdier.teaching %> </br>
    <%= @wdier.memory %> </br>
    <%= @wdier.favlang %> </br>
    <%= @wdier.wisewords %> </br>
  </ul>
  </div>
  <section id="photo">
    <div class="photoGallery">
      <h2>Pics Please</h2>
      <button> <%= link_to "Add new photo", new_photo_path(@wdier) %></button>
      <ul>
        <li class="col-lg-3 col-md-4 col-sm-6 col-xs-12"><% @wdier.photos.each do |photo| %>
          <%= image_tag photo.img_url %></li>
          <% end %>
        </ul>
        </div>
<section>
</section>
<section id="comment">
  <div class="commentGallery">
    <div class="overlay">
      <h2>keep in touch...</h2>


<%= link_to "Add new comment", new_wdier_post_path(@wdier) %>
<% @wdier.posts.each do |post| %>

<p>
  <%= post.body %>
</p>
<% if current_user && current_user == post.user %>
<%= link_to "Edit", edit_wdier_post_path(@wdier) %> |
<%= link_to "Delete", wdier_post_path(@wdier), method: :delete %>
<% end %>
</div>
<% end %>
</div>
</section>
</section>

发布新视图:

<%= form_for [@wdier, @post] do |f| %>
  <%= f.label :name %>
  <%= f.text_field :name %>
  <%= f.label :body %>
  <%= f.text_area :body %>
  <%= f.submit %>
<% end %>

架构:

ActiveRecord::Schema.define(version: 20160521153002) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "comments", force: :cascade do |t|
    t.string  "name"
    t.string  "body"
    t.integer "wdier_id"
  end

  add_index "comments", ["wdier_id"], name: "index_comments_on_wdier_id", using: :btree

  create_table "instructors", force: :cascade do |t|
    t.string  "name"
    t.string  "img_url"
    t.string  "squad_name"
    t.string  "quote"
    t.string  "teaching"
    t.string  "memory"
    t.string  "favlang"
    t.string  "wisewords"
    t.string  "email"
    t.string  "password"
    t.integer "squad_id"
    t.integer "user_id"
    t.integer "wdier_id"
  end

  add_index "instructors", ["squad_id"], name: "index_instructors_on_squad_id", using: :btree
  add_index "instructors", ["user_id"], name: "index_instructors_on_user_id", using: :btree
  add_index "instructors", ["wdier_id"], name: "index_instructors_on_wdier_id", using: :btree

  create_table "negatives", force: :cascade do |t|
    t.integer "photo_id"
    t.integer "instructor_id"
    t.integer "student_id"
    t.integer "user_id"
    t.integer "wdier_id"
  end

  add_index "negatives", ["instructor_id"], name: "index_negatives_on_instructor_id", using: :btree
  add_index "negatives", ["photo_id"], name: "index_negatives_on_photo_id", using: :btree
  add_index "negatives", ["student_id"], name: "index_negatives_on_student_id", using: :btree
  add_index "negatives", ["user_id"], name: "index_negatives_on_user_id", using: :btree
  add_index "negatives", ["wdier_id"], name: "index_negatives_on_wdier_id", using: :btree

  create_table "photos", force: :cascade do |t|
    t.string "img_url"
    t.string "caption"
  end

  create_table "posts", force: :cascade do |t|
    t.string  "name"
    t.string  "body"
    t.integer "wdier_id"
  end

  add_index "posts", ["wdier_id"], name: "index_posts_on_wdier_id", using: :btree

  create_table "students", force: :cascade do |t|
    t.string  "name"
    t.string  "img_url"
    t.string  "github_url"
    t.string  "portfolio_url"
    t.string  "project1_url"
    t.string  "project2_url"
    t.string  "project3_url"
    t.string  "project4_url"
    t.string  "quote"
    t.string  "q1"
    t.string  "q2"
    t.string  "q3"
    t.string  "q4"
    t.string  "fb"
    t.string  "linkedin"
    t.string  "email"
    t.string  "password"
    t.integer "instructor_id"
    t.integer "squad_id"
    t.integer "user_id"
    t.integer "wdier_id"
  end

  add_index "students", ["instructor_id"], name: "index_students_on_instructor_id", using: :btree
  add_index "students", ["squad_id"], name: "index_students_on_squad_id", using: :btree
  add_index "students", ["user_id"], name: "index_students_on_user_id", using: :btree
  add_index "students", ["wdier_id"], name: "index_students_on_wdier_id", using: :btree

  create_table "users", force: :cascade do |t|
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.inet     "current_sign_in_ip"
    t.inet     "last_sign_in_ip"
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
    t.string   "name"
    t.string   "img_url"
    t.string   "github_url"
    t.string   "portfolio_url"
    t.string   "project1_url"
    t.string   "project2_url"
    t.string   "project3_url"
    t.string   "quote"
    t.string   "squad_name"
    t.string   "teaching"
    t.string   "memory"
    t.string   "favlang"
    t.string   "wisewords"
    t.string   "tag_list"
    t.string   "q1"
    t.string   "q2"
    t.string   "q3"
    t.string   "fb"
    t.string   "linkedin"
    t.integer  "squad_id"
    t.integer  "student_id"
    t.integer  "instructor_id"
  end

  add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
  add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree

  create_table "wdiers", force: :cascade do |t|
    t.string  "name"
    t.string  "img_url"
    t.string  "github_url"
    t.string  "portfolio_url"
    t.string  "project1_url"
    t.string  "project2_url"
    t.string  "project3_url"
    t.string  "quote"
    t.string  "squad_name"
    t.integer "teaching"
    t.integer "memory"
    t.integer "favlang"
    t.integer "wisewords"
    t.integer "tag_list"
    t.integer "q1"
    t.integer "q2"
    t.integer "q3"
    t.integer "fb"
    t.integer "linkedin"
    t.integer "email"
    t.integer "role"
    t.integer "password"
    t.integer "squad_id"
    t.integer "codey_id"
    t.integer "student_id"
    t.integer "instructor_id"
  end

  add_index "wdiers", ["codey_id"], name: "index_wdiers_on_codey_id", using: :btree
  add_index "wdiers", ["instructor_id"], name: "index_wdiers_on_instructor_id", using: :btree
  add_index "wdiers", ["squad_id"], name: "index_wdiers_on_squad_id", using: :btree
  add_index "wdiers", ["student_id"], name: "index_wdiers_on_student_id", using: :btree

  add_foreign_key "comments", "wdiers"
end

任何帮助都将非常感谢!!!

我正在为问题添加rake路线。

$ rake routes
                  Prefix Verb   URI Pattern                                Controller#Action
        new_user_session GET    /users/sign_in(.:format)                   devise/sessions#new
            user_session POST   /users/sign_in(.:format)                   devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)                  devise/sessions#destroy
           user_password POST   /users/password(.:format)                  devise/passwords#create
       new_user_password GET    /users/password/new(.:format)              devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format)             devise/passwords#edit
                         PATCH  /users/password(.:format)                  devise/passwords#update
                         PUT    /users/password(.:format)                  devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)                    devise/registrations#cancel
       user_registration POST   /users(.:format)                           devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)                   devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)                      devise/registrations#edit
                         PATCH  /users(.:format)                           devise/registrations#update
                         PUT    /users(.:format)                           devise/registrations#update
                         DELETE /users(.:format)                           devise/registrations#destroy
                    root GET    /                                          yearbook#index
                     tag GET    /tags/:tag(.:format)                       photo#index
             wdier_posts GET    /wdiers/:wdier_id/posts(.:format)          posts#index
                         POST   /wdiers/:wdier_id/posts(.:format)          posts#create
          new_wdier_post GET    /wdiers/:wdier_id/posts/new(.:format)      posts#new
         edit_wdier_post GET    /wdiers/:wdier_id/posts/:id/edit(.:format) posts#edit
              wdier_post GET    /wdiers/:wdier_id/posts/:id(.:format)      posts#show
                         PATCH  /wdiers/:wdier_id/posts/:id(.:format)      posts#update
                         PUT    /wdiers/:wdier_id/posts/:id(.:format)      posts#update
                         DELETE /wdiers/:wdier_id/posts/:id(.:format)      posts#destroy
                  wdiers GET    /wdiers(.:format)                          wdiers#index
                         POST   /wdiers(.:format)                          wdiers#create
               new_wdier GET    /wdiers/new(.:format)                      wdiers#new
              edit_wdier GET    /wdiers/:id/edit(.:format)                 wdiers#edit
                   wdier GET    /wdiers/:id(.:format)                      wdiers#show
                         PATCH  /wdiers/:id(.:format)                      wdiers#update
                         PUT    /wdiers/:id(.:format)                      wdiers#update
                         DELETE /wdiers/:id(.:format)                      wdiers#destroy
                  photos GET    /photos(.:format)                          photos#index
                         POST   /photos(.:format)                          photos#create
               new_photo GET    /photos/new(.:format)                      photos#new
              edit_photo GET    /photos/:id/edit(.:format)                 photos#edit
                   photo GET    /photos/:id(.:format)                      photos#show
                         PATCH  /photos/:id(.:format)                      photos#update
                         PUT    /photos/:id(.:format)                      photos#update
                         DELETE /photos/:id(.:format)                      photos#destroy
                  codeys GET    /codeys(.:format)                          codeys#index
                         POST   /codeys(.:format)                          codeys#create
               new_codey GET    /codeys/new(.:format)                      codeys#new
              edit_codey GET    /codeys/:id/edit(.:format)                 codeys#edit
                   codey GET    /codeys/:id(.:format)                      codeys#show
                         PATCH  /codeys/:id(.:format)                      codeys#update
                         PUT    /codeys/:id(.:format)                      codeys#update
                         DELETE /codeys/:id(.:format)                      codeys#destroy
                  squads GET    /squads(.:format)                          squads#index
                         POST   /squads(.:format)                          squads#create
               new_squad GET    /squads/new(.:format)                      squads#new
              edit_squad GET    /squads/:id/edit(.:format)                 squads#edit
                   squad GET    /squads/:id(.:format)                      squads#show
                         PATCH  /squads/:id(.:format)                      squads#update
                         PUT    /squads/:id(.:format)                      squads#update
                         DELETE /squads/:id(.:format)                      squads#destroy

3 个答案:

答案 0 :(得分:1)

你用一个参数调用post_params,而不是期待一个。

由于post_params返回哈希值,因此您无需使用括号来访问该值:

post_params[:wdier_id]

但是,您不允许wdier_id使用强大的参数,因此会返回nil。我的猜测是你想要这种行为:

redirect_to wdier_path(params[:wdier_id])

答案 1 :(得分:1)

查看您的错误说明。在create方法的posts_controllers.rb的第32行中,您使用带有参数post_params的函数[:wdier_id]。该方法不带任何参数,因此您有错误。

而不是redirect_to post_params([:wdier_id])你应该有类似redirect_to photo_path(@post.id)之类的东西。

答案 2 :(得分:0)

分辨率为redirect_to wdier_path(@wdier)