Ruby on rails Rails:PostsController中的ArgumentError创建错误数量的参数(1代表0)
嗨,我是个疯子,需要帮助。为我的训练营课程做一本年鉴,还打嗝。当前错误为:Ruby on rails Rails:PostsController中的ArgumentError创建错误数量的参数(1代表0),ruby-on-rails,ruby,devise,Ruby On Rails,Ruby,Devise,嗨,我是个疯子,需要帮助。为我的训练营课程做一本年鉴,还打嗝。当前错误为: 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) en
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
Wdiers是我的用户。这些教师是学生和教师。目前,我只是想在wdier show页面上添加一篇文章。我目前在页面上有一篇文章,是关于一个来自种子文件的测试wdier。错误是当我试图创建一个新帖子时。我能够进入表单页面,在我提交帖子后出现了错误。我的帖子最初称为评论,但后来改为帖子,并将评论作为对这些帖子的回复
另外,我想指出的是,我正在使用Desive,以防万一
以下是我的页面:
WDIer控制器
类WdiersController<应用程序控制器
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
任何帮助都将不胜感激
我在这个问题上添加了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
您正在使用参数调用post_params,而它不需要参数
由于post_params返回散列,因此不需要括号即可访问该值:
post_params[:wdier_id]
但是,您不允许在强参数中使用wdier_id,因此将返回nil。我猜你想要这种行为:
redirect_to wdier_path(params[:wdier_id])
看看你的错误描述。在create方法中posts_controllers.rb的第32行中,您将函数post_params与参数[:wdier_id]一起使用。该方法不接受任何参数,因此出现了错误
您不应该将_重定向到post_params[:wdier_id],而应该使用类似于将_重定向到photo的内容_path@post.id或者类似的事情。分辨率被重定向到wdier_path@wdier非常感谢您!不幸的是,你建议的重定向不起作用,但你让我朝着正确的方向前进!查看这两条评论和过去的rails项目,我希望我可以返回wdiers概要页面:redirect_to wdier_path@wdier或者使用重定向到wdier\U post返回到post_path@post但这两个也不起作用:@LaFrish你有没有用rake路径验证过路径助手?@PetrGazarov,我有,他补充道。好消息!只是让它与“重定向到wdier”一起工作_path@wdier“去工作。我在控制器上有一个地方,我在做更改时忘了更改。但现在它开始工作了!没有你的帮助我不可能做到!!!非常感谢。你建议的重定向不起作用,但它也让我朝着正确的方向前进。
<%= 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 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
post_params[:wdier_id]
redirect_to wdier_path(params[:wdier_id])