我是Rails的新手,正在尝试创建一个接受用户请求的应用程序。然而,当我尝试创建新请求时,这就是正在发生的事情。
请求控制器:
class RequestsController < ApplicationController
before_action :set_request, only: [:show, :edit, :update, :destroy]
# GET /requests
# GET /requests.json
def index
@requests = Request.all
end
# GET /requests/1
# GET /requests/1.json
def show
end
# GET /requests/new
def new
@request = Request.new
end
# GET /requests/1/edit
def edit
end
# POST /requests
# POST /requests.json
def create
@request = Request.new(request_params)
respond_to do |format|
if @request.save # <-------------- The problem is here
format.html { redirect_to @request, notice: 'Request was successfully created.' }
format.json { render :show, status: :created, location: @request }
else
format.html { render :new }
format.json { render json: @request.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /requests/1
# PATCH/PUT /requests/1.json
def update
respond_to do |format|
if @request.update(request_params)
format.html { redirect_to @request, notice: 'Request was successfully updated.' }
format.json { render :show, status: :ok, location: @request }
else
format.html { render :edit }
format.json { render json: @request.errors, status: :unprocessable_entity }
end
end
end
# DELETE /requests/1
# DELETE /requests/1.json
def destroy
@request.destroy
respond_to do |format|
format.html { redirect_to requests_url, notice: 'Request was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_request
@request = Request.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def request_params
params.require(:request).permit(:user_id, :description, :created)
end
end
错误说没有&#34;创建&#34;请求控制器中的方法,但是说这是create方法的方法。我认为问题在于我的观点并未将描述作为参数传递,我不知道该怎么做/为什么它还没有。
_form.html.erb(由edit.html.erb呈现):
%= form_for(@request) do |f| %>
<% if @request.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@request.errors.count, "error") %> prohibited this request from being saved:</h2>
<ul>
<% @request.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<center>
<div class="field">
<%= f.label 'Describe your favor' %><br>
<%= f.text_area :description %>
</div>
<div class="actions">
<%= f.submit %>
</div>
</center>
<% end %>
如果有人可以告诉我如何将描述对象作为参数传递给我的create方法并让方法不崩溃。
以下是错误日志:
NoMethodError (undefined method `Request' for #<Request:0x007f9fbd8992e8>):
app/controllers/requests_controller.rb:32:in `block in create'
app/controllers/requests_controller.rb:31:in `create'
Rendered /usr/local/rvm/gems/ruby-2.2.2/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/_source.erb (6.9ms)
Rendered /usr/local/rvm/gems/ruby-2.2.2/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.8ms)
Rendered /usr/local/rvm/gems/ruby-2.2.2/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.9ms)
Rendered /usr/local/rvm/gems/ruby-2.2.2/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (29.4ms)
答案 0 :(得分:2)
使用Request(或Response)作为模型的名称不是一个好主意,因为你最终会隐藏request object。这是Rails控制器的核心部分。
由于它会在MVC和Web开发中与核心概念发生冲突,因此它也会使你的代码变得非常混乱。
使用thesaurus并为您的模型找到另一个名称。