我正在使用Ruby on Rails创建一个网站。我遇到了一个问题,我需要在我的网页上显示我的数据作为文本,例如<h2><%=@permits.permitstart%></h2>
,但我一直收到此错误
NoMethodError in Permits#show
Showing C:/Users/GaryVLC/RubymineProjects/Parking_and_Safety_3/app/views/permits/show.html.erb where line #7 raised:
undefined method `permitstart' for #<Permit::ActiveRecord_Relation:0xb69e290>
我不确定我是否在控制器中输入了正确的内容。你们可以看一下吗?任何帮助表示赞赏!
这是我希望我的数据呈现的网页(show.html.erb)
<% provide(:title, 'New Permit') %>
<h1>Permit Application</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<h2><%=@permits.permitstart%></h2></br>
</div>
</div>
这是按钮克隆到(show.html.erb)
的网页<h2>My Permits</h2>
<table>
<tr>
<th>Permit Start Date</th>
<th>Permit End Data</th>
<th>Action</th>
<th> </th>
</tr>
<% @permits.each do |permit| %>
<tr>
<td><%= permit.permitstart %></td>
<td><%= permit.permitend %></td>
<td><%= link_to 'Detail', viewpermit_path(permit) %></td>
<td><%= link_to 'Edit', edit_permit_path(permit) %></td>
</tr>
<% end %>
</table>
这是我的许可控制器
class PermitsController < ApplicationController
before_action :set_permit, only: [:destroy]
def index
@permits = Permit.where(:user_id => current_user.id)
end
def new
@permits = Permit.new
end
def create
@permits = current_user.permits.build(permit_params)
if @permits.save
redirect_to invoice_path
else
render 'new'
end
end
def destroy
Permit.destroy_all(user_id: current_user)
respond_to do |format|
format.html { redirect_to root_path, notice: 'Permit was successfully canceled.' }
format.json { head :no_content }
end
end
def confirm
@fields = %i[vehicle_type, carplate, studentid, name, department, permitstart, permitend]
@permit = current_user.permits.build(permit_params)
render :new and return unless @permit.valid?
end
def show
@permits = Permit.where(:user_id => current_user.id)
end
def update
@permits = Permit.where(user_id: current_user).take
respond_to do |format|
if @permits.update(permit_params)
format.html { redirect_to root_path}
flash[:success] = "Permit successfully updated"
format.json { render :show, status: :ok, location: @user }
else
format.html { render :edit }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
def edit
@permits = Permit.find(params[:id])
#@permits = Permit.find_or_initialize_by(user_id: params[:id])
end
def detail
end
private
# Use callbacks to share common setup or constraints between actions.
def set_permit
@permits = Permit.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def permit_params
params.require(:permit).permit(:vehicle_type, :name, :studentid, :department, :carplate, :duration, :permitstart, :permitend)
end
end
这是我的route.db
Rails.application.routes.draw do
resources :users
resources :permits do
collection do
post :confirm
end
end
resources :visitor_permits
root 'static_pages#home'
get 'viewpermit' =>'permits#show'
get 'invoice' => 'permits#invoice'
get 'payment' =>'transaction#new'
get 'show_visitor_permit' =>'visitor_permits#show'
get 'show_permit' =>'permits#index'
get 'visitorpermit' => 'visitor_permits#new'
post 'createpermit' => 'permits#create'
get 'homepage/index'
post 'permits' => 'permits#create'
get 'permitapplication' => 'permits#new'
get 'adminlogin' => 'admin_controller#index'
get 'patrollogin' => 'patrol_officer_controller#index'
get 'createcitation' => 'citations#new'
get 'contact'=> 'static_pages#contact'
get 'about' => 'static_pages#about'
get 'signup' => 'users#new'
get 'help' => 'static_pages#help'
post 'users' =>
'users#create'
get 'login' => 'sessions#new' #Page for a new session
post 'login' => 'sessions#create' #Create a new session
delete 'logout'=>'sessions#destroy' #Delete a session
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
答案 0 :(得分:1)
您正在集合对象上调用实例方法(该方法不存在)。
您需要遍历@permits
对象并在其包含的每个模型对象上调用permitstart
:
<% provide(:title, 'New Permit') %>
<h1>Permit Application</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<% @permits.each do |p| %>
<h2><%= p.permitstart %></h2></br>
<% end %>
</div>
</div>
-
或者,如果您在show
操作中看到此错误,则需要使用以下内容:
def show
@permits = current_user.permits.find(params[:id])
end
答案 1 :(得分:0)
因为这个方法
def show
@permits = Permit.where(:user_id => current_user.id)
end
为您提供关系(ActiveRecord_Relation),而不是单个Permit对象。你可以拿第一个,但我不知道它是不是你想要的。无论如何,这就是问题所在。
@permits = Permit.where(:user_id => current_user.id).first