我可以从" new"中获取视图中的对象。方法但无法从控制器的另一个方法获取它。为什么呢?
所以,我在routes.rb中有这个:
Rails.application.routes.draw do
get 'horarios/new'=> 'horarios#new'
get 'horarios/newprofessor'=> 'horarios#newprofessor'
end
这在horarios_controller.rb中:
class HorariosController < ApplicationController
def new
@horario = Horario.new
@teste = "it prints!"
end
def newprofessor
@teste = "it prints!"
end
end
并将其放在两个视图中(new.html.erb和newprofessor.html.erb):
<% raise @teste.inspect %>
&#34; new&#34; view给了我这个对象,但是&#34; newprofessor&#34;观点说它没有。这是为什么?谢谢!
- 编辑 (添加所有代码) horarios_controller.rb
class HorariosController < ApplicationController
def new
@horario = Horario.new
@teste = "it prints!"
end
def addhorarioprofessor
puts params[:Sex10]
if params[:Sex10] == "1"
puts "foi"
@horario = Horario.where(:hora_inicio => "10:00", :dia_semana => "Sexta-feira").first
@horario.update_attributes(:professor_id => 6)
@horario.save
end
redirect_to '/'
end
def create
@horario = Horario.new(horario_params)
if @horario.save
redirect_to '/'
else
render 'new'
end
end
private
def horario_params
params.require(:horario).permit(:dia_semana, :hora_inicio, :hora_fim, :professor_id)
end
def index
@horarios = Horario.all
end
def newprofessor
@horarios_preenchidos = Hash.new
@horarios = Horario.new
@teste = "it prints!"
for horario in @horarios
if horario.professor_id != nil
@tah_preenchido = true
else
@tah_preenchido = false
end
@horarios_preenchidos[horario.dia_semana+"_"+horario.hora_inicio] = @tah_preenchido
end
end
end
的routes.rb
Rails.application.routes.draw do
get 'professors' => 'professors#index'
get 'professors/new' => 'professors#new'
get 'horarios/new'=> 'horarios#new'
get 'horarios/newprofessor'=> 'horarios#newprofessor'
post 'professors' => 'professors#create'
post 'horarios'=> 'horarios#create'
post 'horarios/newprofessor' => 'horarios#addhorarioprofessor'
end
new.html.erb
<h1>Horarios#new</h1>
<p>Find me in app/views/horarios/new.html.erb</p>
<% raise @teste.inspect %>
<%= form_for(@horario) do |f| %>
<div class="field">
<%= f.label :dia_semana %><br>
<%= f.text_area :dia_semana %><br>
<%= f.label :hora_inicio %>
<%= f.text_area :hora_inicio %>
<%= f.label :hora_fim %>
<%= f.text_area :hora_fim %>
</div>
<div class="actions">
<%= f.submit "Create" %>
</div>
<% end %>
newprofessor.html.erb:
<h1>Horarios#new</h1>
<p>Find me in app/views/horarios/new.html.erb</p>
<%= debug(params) if Rails.env.development? %>
<% raise @teste.inspect %>
<%= form_tag({ :method => :post }) do %>
<%= text_field :Horario, :attribute_name %><br>
<table>
<th>
<tr>Segunda</tr>
<tr>Terça</tr>
<tr>Quarta</tr>
<tr>Quinta</tr>
<tr>Sexta</tr>
</th>
<tr>
<td><%= check_box_tag 'segunda-feira_05:00', '1', false, disabled: true %> 05:00</td>
<td><%= check_box_tag :Ter5 %> 0x:00</td>
<td><%= check_box_tag :Qua5 %> 0x:00</td>
<td><%= check_box_tag :Qui5 %> 0x:00</td>
<td><%= check_box_tag :Sex5 %> 0x:00</td>
</tr>
<tr>
<td><%= check_box_tag :Seg6 %> 06:00</td>
<td><%= check_box_tag :Ter5 %> 0x:00</td>
<td><%= check_box_tag :Qua5 %> 0x:00</td>
<td><%= check_box_tag :Qui5 %> 0x:00</td>
<td><%= check_box_tag :Sex5 %> 0x:00</td>
</tr>
<tr>
<td><%= check_box_tag :Seg7 %> 07:00</td>
<td><%= check_box_tag :Ter5 %> 0x:00</td>
<td><%= check_box_tag :Qua5 %> 0x:00</td>
<td><%= check_box_tag :Qui5 %> 0x:00</td>
<td><%= check_box_tag :Sex5 %> 0x:00</td>
</tr>
<tr>
<td><%= check_box_tag :Seg8 %> 08:00</td>
<td><%= check_box_tag :Ter5 %> 0x:00</td>
<td><%= check_box_tag :Qua5 %> 0x:00</td>
<td><%= check_box_tag :Qui5 %> 0x:00</td>
<td><%= check_box_tag :Sex5 %> 0x:00</td>
</tr>
<tr>
<td><%= check_box_tag :Seg9 %> 09:00</td>
<td><%= check_box_tag :Ter5 %> 0x:00</td>
<td><%= check_box_tag :Qua5 %> 0x:00</td>
<td><%= check_box_tag :Qui5 %> 0x:00</td>
<td><%= check_box_tag :Sex5 %> 0x:00</td>
</tr>
<tr>
<td><%= check_box_tag :Seg10 %> 10:00</td>
<td><%= check_box_tag :Ter5 %> 0x:00</td>
<td><%= check_box_tag :Qua5 %> 0x:00</td>
<td><%= check_box_tag :Qui5 %> 0x:00</td>
<td><%= check_box_tag :Sex5 %> 0x:00</td>
</tr>
<tr>
<td><%= check_box_tag :Seg11 %> 11:00</td>
<td><%= check_box_tag :Ter5 %> 0x:00</td>
<td><%= check_box_tag :Qua5 %> 0x:00</td>
<td><%= check_box_tag :Qui5 %> 0x:00</td>
<td><%= check_box_tag :Sex5 %> 0x:00</td>
</tr>
<tr>
<td><%= check_box_tag :Seg12 %> 12:00</td>
<td><%= check_box_tag :Ter5 %> 0x:00</td>
<td><%= check_box_tag :Qua5 %> 0x:00</td>
<td><%= check_box_tag :Qui5 %> 0x:00</td>
<td><%= check_box_tag :Sex5 %> 0x:00</td>
</tr>
</table>
<div class="actions">
<%= submit_tag "Save" %>
</div>
<% end %>
答案 0 :(得分:1)
当我把#34; newprofessor&#34;作为控制器中的第一个方法工作的方法(我不知道为什么它工作)。
class HorariosController < ApplicationController
def newprofessor
@horarios_preenchidos = Hash.new
@horarios = Horario.all
@teste = "it prints!"
for horario in @horarios
if horario.professor_id != nil
@tah_preenchido = true
else
@tah_preenchido = false
end
@horarios_preenchidos[horario.dia_semana+"_"+horario.hora_inicio] = @tah_preenchido
end
end
def new
@horario = Horario.new
@teste = "it prints!"
end
def addhorarioprofessor
puts params[:Sex10]
if params[:Sex10] == "1"
puts "foi"
@horario = Horario.where(:hora_inicio => "10:00", :dia_semana => "Sexta-feira").first
@horario.update_attributes(:professor_id => 6)
@horario.save
end
redirect_to '/'
end
def create
@horario = Horario.new(horario_params)
if @horario.save
redirect_to '/'
else
render 'new'
end
end
private
def horario_params
params.require(:horario).permit(:dia_semana, :hora_inicio, :hora_fim, :professor_id)
end
def index
@horarios = Horario.all
end
end