Я пользуюсь Rails 5.2 с ruby 2.5.1.
На моем контроллере я создаю свою глобальную переменную @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
[приложение / модели / survey.rb]:
class Survey < ApplicationRecord
belongs_to :campaign
belongs_to :survey_state
has_many :questions
has_many :survey_responses
end
[приложение / просмотров / обзоры / 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.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'