Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails 5.2控制器全局变量在视图上不可访问_Ruby On Rails_Ruby_Ruby On Rails 5.2 - Fatal编程技术网

Ruby on rails Rails 5.2控制器全局变量在视图上不可访问

Ruby on rails Rails 5.2控制器全局变量在视图上不可访问,ruby-on-rails,ruby,ruby-on-rails-5.2,Ruby On Rails,Ruby,Ruby On Rails 5.2,我使用带有ruby 2.5.1的Rails 5.2。 在控制器上,我创建全局变量@survey。我试图在我的视图中访问它,但我的变量似乎没有在我的视图上下文中定义。 我不明白这种行为,在RoR 4上没有发生。有人能帮我吗 [路线: [app/controllers/surveys\u controller.rb]: class SurveysController < ApplicationController def edit campaign_id = params[:

我使用带有ruby 2.5.1的Rails 5.2。 在控制器上,我创建全局变量@survey。我试图在我的视图中访问它,但我的变量似乎没有在我的视图上下文中定义。 我不明白这种行为,在RoR 4上没有发生。有人能帮我吗

[路线:

[app/controllers/surveys\u controller.rb]:

class SurveysController < ApplicationController

  def edit
      campaign_id = params[:campaign_id]
      survey_id = params[:id]
      @survey = Survey.where("campaign_id = ? AND  id = ?", campaign_id, survey_id).first
  end

  private

    def survey_params
      params.require(:survey).permit(:name, :campaign_id, :survey_state_id, :internal_desc, :presentation, :thank_message, :token_libre_service)
    end
end
[app/models/survey.rb]:

class Survey < ApplicationRecord
  belongs_to :campaign
  belongs_to :survey_state
  has_many :questions
  has_many :survey_responses
end
[app/views/surveys/edit.html.erb]:

<h1>Editing Survey</h1>

<%= render 'form', survey: @survey %>

<%= link_to 'Show', campaign_survey_path(@survey) %> |
<%= link_to 'Back', campaign_path(@survey) %>
<%= form_with(model: survey, local: true) do |form| %>
  <% if survey.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(survey.errors.count, "error") %> prohibited this survey from being saved:</h2>

      <ul>
      <% survey.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
     ...
  </div>

  <div class="actions">
    <%= form.submit %>
  </div>
<% end %>
[app/views/surveys/_form.html.erb]:

<h1>Editing Survey</h1>

<%= render 'form', survey: @survey %>

<%= link_to 'Show', campaign_survey_path(@survey) %> |
<%= link_to 'Back', campaign_path(@survey) %>
<%= form_with(model: survey, local: true) do |form| %>
  <% if survey.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(survey.errors.count, "error") %> prohibited this survey from being saved:</h2>

      <ul>
      <% survey.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
     ...
  </div>

  <div class="actions">
    <%= form.submit %>
  </div>
<% end %>
[服务器日志]:

Started GET "/campaigns/1/surveys/1/edit" for 192.168.99.1 at 2018-06-27 06:12:17 +0000
    Cannot render console from 192.168.99.1! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
       (0.7ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
      ↳ /usr/local/bundle/gems/activerecord-5.2.0/lib/active_record/log_subscriber.rb:98
    Processing by SurveysController#edit as HTML
      Parameters: {"campaign_id"=>"1", "id"=>"1"}
      Survey Load (0.9ms)  SELECT  "surveys".* FROM "surveys" WHERE (campaign_id = '1' AND  id = '1') ORDER BY "surveys"."id" ASC LIMIT $1  [["LIMIT", 1]]
      ↳ app/controllers/surveys_controller.rb:6
      Rendering surveys/edit.html.erb within layouts/application
      Rendered surveys/_form.html.erb (301.8ms)
      Rendered surveys/edit.html.erb within layouts/application (340.5ms)
    Completed 500 Internal Server Error in 532ms (ActiveRecord: 5.6ms)



    ActionView::Template::Error (undefined method `survey_path' for #<#<Class:0x00005625fd23a400>:0x00005625fd22f118>):
        1: <%= form_with(model: survey, local: true) do |form| %>
        2:   <% if survey.errors.any? %>
        3:     <div id="error_explanation">
        4:       <h2><%= pluralize(survey.errors.count, "error") %> prohibited this survey from being saved:</h2>

    app/views/surveys/_form.html.erb:1:in `_app_views_surveys__form_html_erb___2337721490819825260_47360580221160'
    app/views/surveys/edit.html.erb:3:in `_app_views_surveys_edit_html_erb___3451176563971010509_47360580376960'

我已经解决了这个问题,通过使用url:parameter调用带有helper的表单_,该参数由好的url设置

<%= form_with(model: survey, url: campaign_survey_path(survey), local: true) do |form| %>

您在surveys controller中有更新操作吗?@Gabbar我没有在我的serveys controller上定义更新操作。但是,当获取编辑操作的视图时,出现了我的问题。您是否也将此表单用于新操作?问题仅说明表单自动定义表单url,该表单url应为更新操作,因此它给出了错误未定义mMethod survey_path'`?将form_用于@survey,而不是将form_与Model:survey、local:true一起使用。它实际上是在表单中获取一个类,而不是获取一个要编辑的测量对象。@Emu如果是这样的话,它应该查找用于创建操作的survey_路径。