您好,我无法通过表单将数据填充到在Rails应用程序中创建的表中。
我可以使用索引操作查看数据,但是在控制台中触发创建操作时,数据库中是否没有持久存储任何内容?
我在resources
文件中使用config/routes.rb
耙道:
user_leads GET /user_leads(.:format) user_leads#index
POST /user_leads(.:format) user_leads#create
new_user_lead GET /user_leads/new(.:format) user_leads#new
edit_user_lead GET /user_leads/:id/edit(.:format) user_leads#edit
user_lead GET /user_leads/:id(.:format) user_leads#show
PATCH /user_leads/:id(.:format) user_leads#update
PUT /user_leads/:id(.:format) user_leads#update
DELETE /user_leads/:id(.:format) user_leads#destroy
app / controllers / user_leads_controller.rb:
class UserLeadsController < ApplicationController
def index
@user_lead = UserLead.all.order("created_at DESC")
end
def new
@user_lead = UserLead.new
end
def create
@user_lead = UserLead.new(user_lead_params)
end
private
def user_lead_params
params.require(:user_lead).permit(:name, :businessname, :phone, :email, :amount)
end
end
app / views / user_leads / new.html.erb
<%= form_for :user_lead, url: user_leads_path do |u| %>
<p>
<%= u.label :name %>
<%= u.text_field :name %>
</p>
<p>
<%= u.label :businessname %>
<%= u.text_field :businessname %>
</p>
<p>
<%= u.label :phone %>
<%= u.text_field :phone %>
</p>
<p>
<%= u.label :email %>
<%= u.text_field :email %>
</p>
<p>
<%= u.submit %>
</p>
<% end %>
config / routes.rb:
resources :user_leads
以及发出发布请求时控制台中的输出:
Started POST "/user_leads" for 127.0.0.1 at 2018-09-29 11:28:48 +0100
Processing by UserLeadsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"caJM7oMC7t3ATCnE0AyrnnKMOUi9vn7MPSj7jUadqCQZDQ9DPbUvn7pw5VMsf9+LFyAIxan9qTcBTfuwI2ygjQ==", "user_lead"=>{"name"=>"ffbgd", "businessname"=>"ret", "phone"=>"re", "email"=>"btgrfq"}, "commit"=>"Save User lead"}
No template found for UserLeadsController#create, rendering head :no_content
Completed 204 No Content in 39ms (ActiveRecord: 0.0ms)
答案 0 :(得分:0)
您需要使用#create
而不是#new
:
def create
@user_lead = UserLead.create(user_lead_params)
end
答案 1 :(得分:0)
将控制器中的创建方法更改为:
def create
@user_lead = UserLead.new(user_lead_params)
respond_to do |format|
if @user_lead.save
format.html { redirect_to user_leads_path, notice: 'UserLead was successfully created.' }
format.json { render :show, status: :created, location: @user_lead }
else
format.html { render :new }
format.json { render json: @user_lead.errors, status: :unprocessable_entity }
end
end
end
答案 2 :(得分:0)
应该是:
def create
@user_lead = UserLead.create(user_lead_params)
end