我正在尝试保存数据,但在提交数据时,它只刷新页面,但实际上并未将数据存储在表中。甚至它也没有给出任何错误。我想要做的是保存数据后,控件应该与空文本框在同一页面上。
使用Ruby 2.4.1,Rails 5.1.3,sqLite3
这是feedback_controller.rb的内容
class FeedbackController < ApplicationController
def new
@feedback = Feedback.new
end
def create
@feedback = Feedback.new(feedback_params)
redirect_to root_path
respond_to do |f|
if @feedback.save
f.html {redirect_to @feedback, notice: 'feedback submitted'}
f.json {render :new, status: :created, location: :@feedback}
else
f.html {render :new }
f.json {render json: @feedback.errors, status: :unprocessable_entity}
end
end
end
private
# def set_feedback
# @feedback = Feedback.find(params[:id])
# end
def feedback_params
params.require(:feedback).permit(:name, :email, :message)
end
end
这是new.html.erb文件。
<%= form_with scope: :feedback, url: feedback_new_path, local: true do |form| %>
<% if @feedback.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(feedback.errors.count, "error") %> prohibited this feedback from being saved:</h2>
<ul>
<% @feedback.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :name %>
<%= form.text_field :name, id: :feedback_name %>
</div>
<div class="field">
<%= form.label :email %>
<%= form.text_field :email, id: :feedback_email %>
</div>
<div class="field">
<%= form.label :message %>
<%= form.text_area :message, id: :feedback_message %>
</div>
<div class="actions">
<%= form.submit "Submit Feedback"%>
</div>
<% end %>
<%= link_to 'Home', root_path %>
这是puma服务器在存储数据时在日志上说的内容:
Started POST "/feedback/new" for 127.0.0.1 at 2017-09-14 21:33:41 +0530
Processing by FeedbackController#new as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Dk1sGiKYryIOeU6HyO7zeX6wqXazj9BUfooJgIDH1JjHxuKwA5MT0E6zvuwFYXGA8xEOz+tGAiXWySKv7voKvg==", "feedback"=>{"name"=>"sheshang", "email"=>"sid@gm.co", "message"=>"this is demo message"}, "commit"=>"Submit Feedback"}
Rendering feedback/new.html.erb within layouts/application
Rendered feedback/new.html.erb within layouts/application (3.8ms)
Completed 200 OK in 75ms (Views: 55.2ms | ActiveRecord: 1.3ms)
除了这个问题,我想知道如何在rails app中调试。就像在C或任何其他程序中我们用来放置printf语句来检查控件是否进入内部。我们如何在Rails中做到?
答案 0 :(得分:0)
如您所见,您已发布到new
操作:
Started POST "/feedback/new" for 127.0.0.1 at 2017-09-14 21:33:41 +0530
Processing by FeedbackController#new as HTML
但new
动作不进行保存。
所以,你可能想要:
<%= form_with scope: :feedback, url: feedbacks_path, local: true do |form| %>
按照惯例,如果您这样做(在routes.rb
文件中):
resources :feedbacks
然后你应该得到:
feedbacks GET /feedbacks(.:format) feedbacks#index
POST /feedbacks(.:format) feedbacks#create
new_feedback GET /feedbacks/new(.:format) feedbacks#new
edit_feedback GET /feedbacks/:id/edit(.:format) feedbacks#edit
feedback GET /feedbacks/:id(.:format) feedbacks#show
PATCH /feedbacks/:id(.:format) feedbacks#update
PUT /feedbacks/:id(.:format) feedbacks#update
DELETE /feedbacks/:id(.:format) feedbacks#destroy
正如您所看到的,这不会为POST
操作生成new
动词,这表示您不使用传统路线。如果是这样,那么您可能需要更新路线,以便获得feedbacks_path
。
我建议遵循惯例,因为它通常会让生活更轻松。
就打印到控制台而言,我倾向于这样做:
puts "#{self.class}.#{__method__}, var: #{var}"
我用我要检查的任何变量替换var
。我想加入#{self.class}.#{__method__}
位,因为它让我知道我所在的类和方法。