首页 > 解决方案 > Rails 5.2 控制器全局变量在视图上不可访问

问题描述

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

[路线(http://192.168.99.100:3000/campaigns/1/surveys/1/edit)]

edit_campaign_survey_path   GET /campaigns/:campaign_id/surveys/:id/edit(.:format)  surveys#edit

[应用程序/控制器/surveys_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) %>

[app/views/surveys/_form.html.erb]:

<%= 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'

标签: ruby-on-railsrubyruby-on-rails-5.2

解决方案


我已经解决了这个问题,通过使用 url 调用 form_with 助手:使用好的 url 设置的参数。

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

推荐阅读