我使用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'
答案 0 :(得分:0)
我已解决此问题,方法是使用具有良好url的url:参数调用form_with helper。
<%= form_with(model: survey, url: campaign_survey_path(survey), local: true) do |form| %>