我单击“提交”按钮时,我的Rails表单(&#34;我的控制器中的新&#34;操作)没有创建对象。我在页面或控制台中没有收到任何错误。似乎它被卡住而没有对&#34;创建&#34;执行POST。我的控制器中的动作。当我单击表单上的“提交”按钮时,URL将更改为meds/new?utf8=✓&authenticity_token=<random characters>
并清除表单。
我觉得我错过了一些非常基本的东西(自从我在Rails中编程以来已经有一段时间了。)
以下是相关代码:
new.html.erb
<% if user_signed_in? && current_user.is_admin == true %>
<%= simple_form_for(@med) do |f| %>
<div class="form-group">
<%= f.label :brand_name, "Name of the medicine:" %>
<%= f.text_field :brand_name, required: true %>
</div>
<div class="form-group">
<%= f.label :generic_name %>
<%= f.text_field :generic_name, required: true %>
</div>
<div class="form-group">
<%= f.label :description %>
<%= f.input :description, as: :summernote, :id => 'description' %>
</div>
<div class="form-group">
<%= f.label :reactions %>
<%= f.text_area :reactions, class: 'summernote', id: 'reactions' %>
</div>
<div class="form-group">
<%= f.label :interactions %>
<%= f.text_area :interactions, class: 'summernote', id: 'interactions' %>
</div>
<div class="form-group">
<%= f.label :implementation %>
<%= f.text_area :implementation, class: 'summernote', id: 'implementation' %>
</div>
<div class="form-group">
<%= f.label :availability %>
<%= f.text_area :availability, class: 'summernote', id: 'availability' %>
</div>
<div class="form-group">
<%= f.label :warnings %>
<%= f.text_area :warnings, class: 'summernote', id: 'warnings' %>
</div>
<%= f.submit "Save changes", class: "btn btn-primary" %>
<% end %>
<% end %>
meds_controller.rb
class MedsController < ApplicationController
def index
end
def new
@med = Med.new
end
def create
@med = Med.create(med_params)
redirect_to root_url
end
def show
@med = Med.find(params[:id])
end
def edit
@med = Med.find(params[:id])
end
def update
@med = Med.find(params[:id])
respond_to do |format|
if @med.update(med_params)
format.html { redirect_to @med, notice: 'Med was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @med.errors, status: :unprocessable_entity }
end
end
end
private
def med_params
params.require(:med).permit(:brand_name, :generic_name, :description, :reactions, :interactions, :implementation,
:availability, :warnings)
end
end
meds.rb模型
class Med < ActiveRecord::Base
end
的routes.rb
Rails.application.routes.draw do
devise_for :users
resources :meds
#get 'meds/index'
root to: "meds#index"
end
我错过了什么?
谢谢!
编辑:单击按钮时在控制台中添加输出:
Started GET "/meds/new?utf8=%E2%9C%93&authenticity_token=XG44W3%2Bj5A3piAJnp0uQUF8L5gruoGDgdiaqZgCSf3bY219l%2BGIRc4Z%2FaypXa%2FacacQGTCFQmFMckS30Jf6ZLg%3D%3D&med%5Bbrand_name%5D=sfsdf&med%5Bgeneric_name%5D=sdfsdf&med%5Bdescription%5D=sdfsdf&med%5Breactions%5D=&med%5Binteractions%5D=&med%5Bimplementation%5D=&med%5Bavailability%5D=&med%5Bwarnings%5D=&commit=Save+changes" for ::1 at 2015-08-28 22:24:00 -0400
Processing by MedsController#new as HTML
编辑2:我不认为正在调用创造。这是我的佣金路线:
Prefix Verb URI Pattern Controller#Action
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
meds GET /meds(.:format) meds#index
POST /meds(.:format) meds#create
new_med GET /meds/new(.:format) meds#new
edit_med GET /meds/:id/edit(.:format) meds#edit
med GET /meds/:id(.:format) meds#show
PATCH /meds/:id(.:format) meds#update
PUT /meds/:id(.:format) meds#update
DELETE /meds/:id(.:format) meds#destroy
root GET / meds#index
答案 0 :(得分:0)
在您的创建操作中尝试此操作:
def create
@med = Med.create(med_params)
if @med.save
flash[:notice] = "Med was successfully created"
redirect_to med_path(@med)
else
flash[:error] = "Med was not successfully created"
render :new
end
end
并将其添加到index.html.erb的第一行
<p id="notice">
<%= notice %>
</p>
答案 1 :(得分:0)
在你的new.html.erb中试试这个
<%= simple_form_for @med, :url => { :action => "create" } do |f| %>
&#13;