[英]attr_accessor returns nil?
I want a temporary attribute which I can use in the controller. 我想要一个可以在控制器中使用的临时属性。
I thought attr_accessor
is the best way to do this. 我认为
attr_accessor
是执行此操作的最佳方法。
But when I submit the users_binded
-input, the following error occurs: 但是,当我提交
users_binded
-input时,会发生以下错误:
Template is missing: Missing template schedule/invite_now, application/invite_now with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in: * ../views
I want to find the user with the users_binded
-input in the database, as you can see in the controller. 我想在数据库中找到具有
users_binded
-input的用户,就像在控制器中看到的那样。
I don't know how to use the temporary-attribute in the controller. 我不知道如何在控制器中使用临时属性。
Should it be Schedule.users_binded
or :users_binded
or something else? 应该是
Schedule.users_binded
或:users_binded
还是其他名称?
schedule-model: 时间表模型:
class Schedule < ActiveRecord::Base
attr_accessor :users_binded
end
schedule-view: 日程安排视图:
<%= form_for @schedule_invite, :as => :schedule, :url => schedule_invite_path, :method => :post, :html => {:class => 'navbar-form', :role => 'login'} do |schedule_form_builder| %>
<p>
<strong>..Benutzer:</strong>
<%= schedule_form_builder.text_field :users_binded, :class => 'form-control', :placeholder => 'Benutzer'%>
</p>
<p>
<strong>Title:</strong><br>
<%= @schedule_invite.title %>
</p>
<p>
<strong>Ort:</strong><br>
<%= @schedule_invite.location %>
</p>
<p>
<strong>Datum und Uhrzeit:</strong><br>
<%= @schedule_invite.date_time.strftime("%A, %d.%m.%Y") %>
<%= @schedule_invite.date_time.strftime("%H:%M:%S") %>
</p>
<p class="pull-right">
<%= link_to "Zurück", root_path, :class => 'btn btn-danger' %>
<%= schedule_form_builder.submit 'Termin teilen?', :class => 'btn btn-success' %>
</p>
<% end %>
schedule-controller: 时间表控制器:
class ScheduleController < ApplicationController
def invite_now
begin #try
#user_name = @schedule_invite.users_binded.to_s
user_binded = User.find_by_name(:user_binded)
rescue #catch
if user_binded.nil?
flash[:notice] = 'Der Benutzer konnte nicht gefunden werden.'
@schedule_invite = current_user.schedules.find(params[:id])
render :action => "invite"
else
@schedule_invite = user_binded.schedules.find(params[:id])
user_binded.schedules << @schedule_invite
flash[:notice] = 'Der Termin wird nun mit dem Benutzer ' + user_binded.name + 'geteilt.'
redirect_to :root
end
end
end
end
routes: 路线:
post "schedule/invite/:id" => "schedule#invite_now"
def invite_now begin #try #user_name = @schedule_invite.users_binded.to_s user_binded = User.find_by_name(params[:user_binded]) rescue #catch if user_binded.nil? flash[:notice] = 'Der Benutzer konnte nicht gefunden werden.' @schedule_invite = current_user.schedules.find(params[:id]) render :action => "invite" end end if user_binded.nil? flash[:notice] = 'Der Benutzer konnte nicht gefunden werden.' @schedule_invite = current_user.schedules.find(params[:id]) render :action => "invite" else @schedule_invite = user_binded.schedules.find(params[:id]) user_binded.schedules << @schedule_invite flash[:notice] = 'Der Termin wird nun mit dem Benutzer ' + user_binded.name + 'geteilt.' redirect_to :root end end
So that I don't need a invite_now.html.erb
. 这样,我就不需要
invite_now.html.erb
。
The problem that occurs is the following (maybe there is a attr_accessor-problem): 发生的问题如下(可能存在attr_accessor问题):
The SQL-Statement SQL语句
user_binded = User.find_by_name(params[:user_binded])
returns/loads 回报/负载
User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."name" IS NULL LIMIT 1
, User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."name" IS NULL LIMIT 1
,
so that the attr_accessor isn't correct ? 这样attr_accessor是不正确的?
As Doon said: "attr_accsessor would be used on an instance." 正如Doon所说:“ attr_accsessor将用于实例。”
Is the solution the following ? 解决方案如下吗?
@schedule_invite = current_user.schedules.find(params[:id]) @schedule_invite.user_binded = User.find_by_name(params[:users_binded])
It only makes sense if the attributes type can be everything (ex. Type of User). 只有属性类型可以是所有内容(例如用户类型),才有意义。
Any Ideas ? 有任何想法吗 ?
params[:users_binded]
with the following:
params[:users_binded]
:
flash[:notice] = params[:users_binded]
can't convert nil into String
, so that the param users_binded wont be set correctly.
can't convert nil into String
,因此无法正确设置参数users_binded。
private def schedule_params params.require(:schedule).permit(:id, :titel, :location, :date_time) end
I think here is the problem.. 我认为这是问题所在。
Issue is not related to attr_accessor
at all. 问题根本与
attr_accessor
。 That's just fine. 很好 Problem is with template rendering.
问题在于模板渲染。
So, you need to add template view invite_now.html.erb
in your views/schedule/
directory 因此,您需要在
views/schedule/
目录中添加模板视图invite_now.html.erb
Than in your controller: 比您的控制器中:
you need to use params[:schedule][:users_binded]
instead of :user_binded
. 您需要使用
params[:schedule][:users_binded]
而不是:user_binded
。 So it will be like 所以会像
user_binded = User.find_by_name(params[:schedule][:users_binded])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.