Request:Class(rails) 的未定义局部变量或方法“int”
undefined local variable or method `int' for Request:Class(rails)
我遇到一个问题,当我打开表单 "service_create" 时,它会抛出一条错误消息 "undefined local variable or method"。我不知道我做错了什么请告诉我问题是什么(我是 rails 的新手)。谢谢
service_request
<%= link_to("Back" , {:action => '#'}) %>
<%= form_for(:request , :url => {:action => 'service_request_create'}) do |f| %>
<div class="field">
<%= f.label :corresponding_user_from %><br>
<%= f.text_field :corresponding_user_from %>
</div>
<div class="field">
<%= f.label :product_service_location %><br>
<%= f.text_field :product_service_location %>
</div>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :description %><br>
<%= f.text_field :description %>
</div>
<!--<%= f.select :category, @categories %>
-->
<div class="field">
<% f.collection_select(:category_id , Category.all , :_id , :title) %>
</div>
<div class="field">
</div>
<div class="field">
<%= f.radio_button :negotiable, "Yes" %>
<%= f.label :negotiable, "Yes", :value => "Yes" %>
<%= f.radio_button :negotiable, "No" %>
<%= f.label :negotiable, "No", :value => "No" %>
</div>
<div class="field">
<%= f.label :Upper_price_range %><br>
<%= f.text_field :Upper_price_range %>
</div>
<div class="field">
<%= f.label :lower_price_range %><br>
<%= f.text_field :lower_price_range %>
</div>
<div class="field">
<%= f.label :tags %><br>
<%= f.text_field :tags %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
request_controller.rb
class RequestsController < ApplicationController
before_action :set_request, only: [:show, :edit, :update, :destroy]
# GET /requests
# GET /requests.json
def index
end
def active
user = session[:user]
@requests = Array.new
@category = Array.new
@requests = Request.getRequestByUser(user)
@requests.each.with_index do |request, index|
if request != nil
@category[index] = Array.new
request[:category_id].each do |cate|
@category[index] << Category.getCategory(cate)
end
end
end
end
# GET /requests/1
# GET /requests/1.json
def show
@user = User.getUser(@request[:user_id])
@category = Array.new
@request[:category_id].each do |cate|
@category << Category.getCategory(cate)
end
end
# GET /requests/1/edit
def edit
@category = Array.new
@request[:category_id].each do |cate|
@category << Category.getCategory(cate)
end
end
# POST /requests
def post_request
tags = params[:tags] ;
categories = Array.new ;
if tags != nil
tags.each do |tag|
category = Category.createCategoryIfNotExist(tag)
if(category != nil)
categories << category[:_id]
end
end
end
request_data = request_params
user_id = session[:user]
request_data[:category_id] = categories
request_data[:user_id] = user_id
@request_ = Request.createRequest(request_data)
if @request_
flash[:notice] = "Request Post successfully."
redirect_to :action => "active"
end
end
#POST /requests
def service_request
end
def item_request
end
def service_request_create
request_data = request_params_create
user_id = session[:user]
request_data[:is_service] = "on"
request_data[:user_id] = user_id
#requests_data[:category]
@request_ = Request.createRequest(request_data)
if @request_
flash[:notice] = "Request Post successfully."
redirect_to :action => "active"
end
end
def item_service_create
#@req = Request.new(request_params)
#@req.typee="Item"
# if @req.save
# redirect_to(:action => 'rpage')
# else
# render('new')
# end
end
# PATCH/PUT /requests/1
# PATCH/PUT /requests/1.json
def update
tags = params[:tags] ;
categories = Array.new ;
if tags != nil
tags.each do |tag|
category = Category.createCategoryIfNotExist(tag)
if(category != nil)
categories << category[:_id]
end
end
end
Rails.logger.info("RequestsParams: #{request_params.inspect}")
request_data = request_params
if request_data[:is_service] != "on"
request_data[:is_service] = "off"
end
user_id = session[:user]
request_data[:category_id] = categories
request_data[:user_id] = user_id
if Request.updateRequest(@request,request_data)
flash[:notice] = "Request has been Edited successfully."
redirect_to :action => "active"
end
end
def delete_request ()
if Request.delete_request(params[:id])
flash[:notice] = "Request has been Deleted successfully."
render :json => "great"
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_request
@request = Request.getRequest(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def request_params
params.require(:request).permit(:title, :description ,:type , :is_service )
end
def request_params_create
params.require(:request).permit(:title, :description, :corresponding_user_from, :category_id ,:product_service_location , :negotiable ,:Upper_price_range , :lower_price_range, :tags)
end
end
因为我需要 10 到 post 图像的声誉,所以我正在 post 正在 link 我的错误截图
错误图片
请求模型中category_id的类型必须是整数,而不是整数。
我遇到一个问题,当我打开表单 "service_create" 时,它会抛出一条错误消息 "undefined local variable or method"。我不知道我做错了什么请告诉我问题是什么(我是 rails 的新手)。谢谢
service_request
<%= link_to("Back" , {:action => '#'}) %>
<%= form_for(:request , :url => {:action => 'service_request_create'}) do |f| %>
<div class="field">
<%= f.label :corresponding_user_from %><br>
<%= f.text_field :corresponding_user_from %>
</div>
<div class="field">
<%= f.label :product_service_location %><br>
<%= f.text_field :product_service_location %>
</div>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :description %><br>
<%= f.text_field :description %>
</div>
<!--<%= f.select :category, @categories %>
-->
<div class="field">
<% f.collection_select(:category_id , Category.all , :_id , :title) %>
</div>
<div class="field">
</div>
<div class="field">
<%= f.radio_button :negotiable, "Yes" %>
<%= f.label :negotiable, "Yes", :value => "Yes" %>
<%= f.radio_button :negotiable, "No" %>
<%= f.label :negotiable, "No", :value => "No" %>
</div>
<div class="field">
<%= f.label :Upper_price_range %><br>
<%= f.text_field :Upper_price_range %>
</div>
<div class="field">
<%= f.label :lower_price_range %><br>
<%= f.text_field :lower_price_range %>
</div>
<div class="field">
<%= f.label :tags %><br>
<%= f.text_field :tags %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
request_controller.rb
class RequestsController < ApplicationController
before_action :set_request, only: [:show, :edit, :update, :destroy]
# GET /requests
# GET /requests.json
def index
end
def active
user = session[:user]
@requests = Array.new
@category = Array.new
@requests = Request.getRequestByUser(user)
@requests.each.with_index do |request, index|
if request != nil
@category[index] = Array.new
request[:category_id].each do |cate|
@category[index] << Category.getCategory(cate)
end
end
end
end
# GET /requests/1
# GET /requests/1.json
def show
@user = User.getUser(@request[:user_id])
@category = Array.new
@request[:category_id].each do |cate|
@category << Category.getCategory(cate)
end
end
# GET /requests/1/edit
def edit
@category = Array.new
@request[:category_id].each do |cate|
@category << Category.getCategory(cate)
end
end
# POST /requests
def post_request
tags = params[:tags] ;
categories = Array.new ;
if tags != nil
tags.each do |tag|
category = Category.createCategoryIfNotExist(tag)
if(category != nil)
categories << category[:_id]
end
end
end
request_data = request_params
user_id = session[:user]
request_data[:category_id] = categories
request_data[:user_id] = user_id
@request_ = Request.createRequest(request_data)
if @request_
flash[:notice] = "Request Post successfully."
redirect_to :action => "active"
end
end
#POST /requests
def service_request
end
def item_request
end
def service_request_create
request_data = request_params_create
user_id = session[:user]
request_data[:is_service] = "on"
request_data[:user_id] = user_id
#requests_data[:category]
@request_ = Request.createRequest(request_data)
if @request_
flash[:notice] = "Request Post successfully."
redirect_to :action => "active"
end
end
def item_service_create
#@req = Request.new(request_params)
#@req.typee="Item"
# if @req.save
# redirect_to(:action => 'rpage')
# else
# render('new')
# end
end
# PATCH/PUT /requests/1
# PATCH/PUT /requests/1.json
def update
tags = params[:tags] ;
categories = Array.new ;
if tags != nil
tags.each do |tag|
category = Category.createCategoryIfNotExist(tag)
if(category != nil)
categories << category[:_id]
end
end
end
Rails.logger.info("RequestsParams: #{request_params.inspect}")
request_data = request_params
if request_data[:is_service] != "on"
request_data[:is_service] = "off"
end
user_id = session[:user]
request_data[:category_id] = categories
request_data[:user_id] = user_id
if Request.updateRequest(@request,request_data)
flash[:notice] = "Request has been Edited successfully."
redirect_to :action => "active"
end
end
def delete_request ()
if Request.delete_request(params[:id])
flash[:notice] = "Request has been Deleted successfully."
render :json => "great"
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_request
@request = Request.getRequest(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def request_params
params.require(:request).permit(:title, :description ,:type , :is_service )
end
def request_params_create
params.require(:request).permit(:title, :description, :corresponding_user_from, :category_id ,:product_service_location , :negotiable ,:Upper_price_range , :lower_price_range, :tags)
end
end
因为我需要 10 到 post 图像的声誉,所以我正在 post 正在 link 我的错误截图
错误图片
请求模型中category_id的类型必须是整数,而不是整数。