型号:
class User < ApplicationRecord
has_many :posts
end
class Post < ApplicationRecord
belongs_to :user
end
控制器:
class PostsController < ApplicationController
def create
@post = current_user.posts.creata(post_params)
end
def post_params
params.require(:post).permit(:name,:user_id)
end
end
RSpec.describe PostsController do
describe "create post" do
it "should create"
user = create(:user)
expect{
post :create, params: { name: "new post", user_id: user.id }
}.to change(Post,:count).by(1)
end
end
这个错误: ActionController的:: ParameterMissing: param丢失或值为空:post
答案 0 :(得分:0)
在轨道5上,您需要通过整个参数结构。见下文:
Rails 4:
import urllib.request, urllib.parse, urllib.error
import json
import threading
import time
localtime = time.asctime( time.localtime(time.time()))
url = 'api'
uh = urllib.request.urlopen(url)
data = uh.read().decode()
js =json.loads(data)
def last_price():
threading.Timer(1.0, last_price).start()
print(js['last'])
print(localtime)
last_price()
Rails 5:
post :create, post: { name: "new post" }
您的规范也缺少current_user存根:
post :create, params: { post: { name: "new post" } }