#ruby-on-rails #ruby
#ruby-on-rails #ruby
Вопрос:
У меня есть приложение ruby on rails.Я добавил в приложение новый раздел под названием отчеты. У него нет модели, кроме контроллера и множества форм в папке views. Функциональность работает так, как хотелось. Проблема, с которой я сталкиваюсь, заключается в том, что при нажатии кнопки отправки пользователь, вошедший в систему, автоматически выходит из системы.
Мой код :
Контроллер отчетов:
class ReportsController < ApplicationController
def index
@projects=Project.find(:all)
@releases=Release.find(:all)
@cycles=Cycle.find(:all)
report_type=params[:report_type]
if report_type=="1" amp;amp; params[:cycles]
@cycle=Cycle.find(params[:cycles])
@ics=@cycle.ics
puts " report_type===#{report_type}"
end
end
def update_releases
puts "inside releases"
project = Project.find(params[:project_id])
@releases = project.releases
respond_to do |format|
format.js
end
end
def update_cycles
puts "inside update_cycles"
release = Release.find(params[:release_id])
@cycles =release.cycles
respond_to do |format|
format.js
end
end
end
В index.html.haml :
-set_title "Reports"
-content_for :content_title do
= link_to "Test Case Manager", "/"
amp;raquo;
= "Reports"
%table.grid.full
%tr
%td.grid.full_panels{:style => "width: 30%"}
-panel "Reports" do
= render "reports",:report_type=>params[:report_type]
%td.grid.full_panels{:style => "width: 70%"}
-table_panel "Report details" do
= render "report_details",:report_type=>params[:report_type]
= javascript_include_tag "pages/ic"
_reports.html.haml:
%table.grid.full
%tr
%td.grid.full_panels{:style => "width: 10%"}
=link_to 'Test Not Run Per Cycle',reports_path(:report_type=>1)
%tr
%td.grid.full_panels{:style => "width: 10%"}
= link_to 'Test Cycle Result Comparison',reports_path(:report_type=>2)
%tr
%td.grid.full_panels{:style => "width: 10%"}
= link_to 'Test Cycle Summary'
%tr
%td.grid.full_panels{:style => "width: 10%"}
= link_to 'Tester summary per cycle'
%tr
%td.grid.full_panels{:style => "width: 10%"}
= link_to 'Test Cycle Failure Report'
%tr
%td.grid.full_panels{:style => "width: 10%"}
= link_to 'Release Test Results Summary'
= javascript_include_tag "pages/ic"
_report_details.html.haml:
-if report_type == "1"
= render "tests_not_run_per_cycle",:report_type=>report_type
= render "tests_not_run_per_cycle_reports",:report_type=>params[:report_type]
_tests_not_run_per_cycle.html.haml:
-projects=Project.all
-releases = Release.all
-cycles=Release.all
-form_tag reports_path(),:method => :get, :multipart => true do
%table.grid.full_panels
%tr
%td.grid.full_panels{:style => "width: 20%"}
Project:
%td.grid.full_panels{:style => "width: 20%"}
//= select_tag "projects",options_from_collection_for_select(projects,"id","name",params[:projects]),{:onchange => "#{remote_function(:url => {:action => "update_releases"},:with => "'project_id=' value")}"}
= select_tag "projects",options_from_collection_for_select(projects,"id","name",params[:projects]), :class => "update_releases",:include_blank=>true
//= select_tag 'projects',options_from_collection_for_select(projects, "id", "name"),:'data-remote' => 'true', :'data-url' => 'reports/update_releases', :'data-type' => 'json'
=hidden_field_tag "report_type","1"
%td.grid.full_panels{:style => "width: 20%"}
Releases:
%td.grid.full_panels{:style => "width: 20%"}
<div id="releases">
= render :partial => 'releases', :object => @releases
%td.grid.full_panels{:style => "width: 20%"}
Cycles:
%td.grid.full_panels{:style => "width: 20%"}
<div id="cycles">
= render :partial => 'cycles', :object => @cycles
%tr
%td.grid.full_panels{:style => "width: 20%"}
%td.grid.full_panels{:style => "width: 20%"}
%td.grid.full_panels{:style => "width: 20%"}
=submit_tag "Submit"
%td.grid.full_panels{:style => "width: 20%"}
%td.grid.full_panels{:style => "width: 20%"}
%td.grid.full_panels{:style => "width: 20%"}
= javascript_include_tag "pages/ic"
_tests_not_run_per_cycle_reports:
-if report_type=="1" amp;amp; params[:cycles]
-ic_to_platform_config = @cycle.ic_platform_configs
%table
%th Root
%th Suite
%th Case
%th IC
%th Executor
%th Platform
-@ics.each do |ic|
%tr
-ic_model=Ic.find(ic.id)
- ic_rev=@cycle.get_ic_rev_assign(ic_model)
- populate_table ic_rev, ic_to_platform_config[ic_model]
= javascript_include_tag "pages/ic"
При нажатии кнопки отправки пользователь, вошедший в систему, автоматически выходит из системы.Пожалуйста, помогите мне здесь.
Спасибо, Рамья.
Ответ №1:
Некоторое время назад у меня была похожая проблема — основная проблема заключается в том, что если вы выполняете POST-запрос к приложению Rails, и в вашем post-запросе нет токена подлинности в качестве одного из параметров, Rails удаляет свой сеанс, что обычно приводит к выходу пользователя из системы.
Взгляните на исходный код HTML вашей сгенерированной формы — содержит ли он скрытое поле ввода, называемое токеном подлинности?
Есть несколько решений, одним из которых было бы пропустить эту проверку на сервере, но лучшим решением было бы добавить маркер подлинности в форму. Попробуйте добавить это в head
раздел вашего файла layout HAML:
= csrf_meta_tag
Я думаю, этого достаточно, чтобы решить вашу проблему
Комментарии:
1. Привет, большое спасибо. Это устранило проблему. Огромное спасибо.