【发布时间】:2018-06-27 06:52:57
【问题描述】:
我使用 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
[app/controllers/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'
【问题讨论】:
-
您在调查控制器中有更新操作吗?
-
@Gabbar 我没有在我的serveys 控制器上定义更新操作。但是在获取编辑操作视图时出现了我的问题 (192.168.99.100:3000/campaigns/1/surveys/1/edit)
-
您是否也在使用此表单进行新操作?问题只是说明 form_form 自动定义表单 url 应该是更新操作,所以它给出错误正是
undefined methodsurvey_path'`? -
使用
form_for @survey而不是form_with(model: survey, local: true)。它实际上是在表单中获取 class,而不是获取要编辑的调查对象。 -
@Emu 如果它应该寻找用于创建操作的surveys_path。
标签: ruby-on-rails ruby ruby-on-rails-5.2