I want a temporary attribute which I can use in the controller.
I thought attr_accessor
is the best way to do this.
But when I submit the users_binded
-input, the following error occurs:
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.
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-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
.
The problem that occurs is the following (maybe there is a attr_accessor-problem):
The SQL-Statement
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
,
so that the attr_accessor isn't correct ?
As Doon said: "attr_accsessor would be used on an instance."
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:flash[:notice] = params[:users_binded]
can't convert nil into String
, so that the param users_binded wont be set correctly.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. 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
Than in your controller:
you need to use params[:schedule][:users_binded]
instead of :user_binded
. So it will be like
user_binded = User.find_by_name(params[:schedule][:users_binded])