我的new.html.erb表单没有发布,也没有在数据库中创建对象。相反,当我单击“提交”按钮时,URL将更改为meds/new?utf8=✓&authenticity_token=<random characters>
。看起来它正在尝试进行GET,我认为它应该尝试POST来创建对象。它看起来不像应用程序甚至试图在Controller中调用我的创建操作。这是我在控制台中得到的内容:
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
我很确定问题出在我的路线上。我的创建操作甚至没有前缀。以下是运行rake路线的结果:
Prefix Verb URI Pattern Controller#Action
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
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
root GET / meds#index
以下是其他相关代码:
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.input :brand_name, required: true %>
</div>
<div class="form-group">
<%= f.label :generic_name %>
<%= f.input :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.input :reactions, class: 'summernote', id: 'reactions' %>
</div>
<div class="form-group">
<%= f.label :interactions %>
<%= f.input :interactions, class: 'summernote', id: 'interactions' %>
</div>
<div class="form-group">
<%= f.label :implementation %>
<%= f.input :implementation, class: 'summernote', id: 'implementation' %>
</div>
<div class="form-group">
<%= f.label :availability %>
<%= f.input :availability, class: 'summernote', id: 'availability' %>
</div>
<div class="form-group">
<%= f.label :warnings %>
<%= f.input :warnings, class: 'summernote', id: 'warnings' %>
</div>
<%= f.button :submit, 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=%2FUNN9iumnSSPPFI%2BFkNF%2BbE5SeKJMRbcyasMdJIVyWprJ8dQ%2FHSOF21CzeHt6GOmVl6lk9gyOVZemt4OQIi7LA%3D%3D&med%5Bbrand_name%5D=sdfsdf&med%5Bgeneric_name%5D=sdfsdf&med%5Bdescription%5D=sdfsdfsdf&med%5Breactions%5D=&med%5Binteractions%5D=&med%5Bimplementation%5D=&med%5Bavailability%5D=&med%5Bwarnings%5D=&commit=Create+Med" for ::1 at 2015-08-29 15:27:41 -0400
Processing by MedsController#new as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"/UNN9iumnSSPPFI+FkNF+bE5SeKJMRbcyasMdJIVyWprJ8dQ/HSOF21CzeHt6GOmVl6lk9gyOVZemt4OQIi7LA==", "med"=>{"brand_name"=>"sdfsdf", "generic_name"=>"sdfsdf", "description"=>"sdfsdfsdf", "reactions"=>"", "interactions"=>"", "implementation"=>"", "availability"=>"", "warnings"=>""}, "commit"=>"Create Med"}
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Rendered meds/new.html.erb within layouts/application (33.1ms)
Completed 200 OK in 245ms (Views: 244.0ms | ActiveRecord: 0.1ms)
编辑2:页面来源
<form novalidate="novalidate" class="simple_form new_med" id="new_med" action="/meds" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="✓" /><input type="hidden" name="authenticity_token" value="MIxyO+9P9O4ReTyuJdOmAQnywlQAAcg3uSFc6FkUphim6PidOJ3n3fMHo3HeeIBe7pUuJVEC570uEI6Si4nUXg==" />
<div class="form-group">
<label for="med_brand_name">Name of the medicine:</label>
<div class="input string required med_brand_name"><label class="string required" for="med_brand_name"><abbr title="required">*</abbr> Brand name</label><input class="string required" type="text" name="med[brand_name]" id="med_brand_name" /></div>
</div>
<div class="form-group">
<label class="string optional" for="med_generic_name">Generic name</label>
<div class="input string required med_generic_name"><label class="string required" for="med_generic_name"><abbr title="required">*</abbr> Generic name</label><input class="string required" type="text" name="med[generic_name]" id="med_generic_name" /></div>
</div>
<div class="form-group">
<label class="text optional" for="med_description">Description</label>
<div class="input summernote optional med_description"><label class="summernote optional" for="med_description">Description</label><textarea class="summernote optional" data-provider="summernote" name="med[description]" id="med_description">
</textarea></div>
</div>
<div class="form-group">
<label class="text optional" for="med_reactions">Reactions</label>
<div class="input text optional med_reactions"><label class="text optional" for="med_reactions">Reactions</label><textarea class="text optional" name="med[reactions]" id="med_reactions">
</textarea></div>
</div>
<div class="form-group">
<label class="text optional" for="med_interactions">Interactions</label>
<div class="input text optional med_interactions"><label class="text optional" for="med_interactions">Interactions</label><textarea class="text optional" name="med[interactions]" id="med_interactions">
</textarea></div>
</div>
<div class="form-group">
<label class="text optional" for="med_implementation">Implementation</label>
<div class="input text optional med_implementation"><label class="text optional" for="med_implementation">Implementation</label><textarea class="text optional" name="med[implementation]" id="med_implementation">
</textarea></div>
</div>
<div class="form-group">
<label class="text optional" for="med_availability">Availability</label>
<div class="input text optional med_availability"><label class="text optional" for="med_availability">Availability</label><textarea class="text optional" name="med[availability]" id="med_availability">
</textarea></div>
</div>
<div class="form-group">
<label class="text optional" for="med_warnings">Warnings</label>
<div class="input text optional med_warnings"><label class="text optional" for="med_warnings">Warnings</label><textarea class="text optional" name="med[warnings]" id="med_warnings">
</textarea></div>
</div>
<input type="submit" name="commit" value="Create Med" class="btn btn btn-primary" />
</form> </fieldset>
</form>
</div>
答案 0 :(得分:2)
生成的HTML中有两个结束的</form>
- 标记。这不是有效的HTML。如果有一个环绕的形式,你必须把内在的形式
答案 1 :(得分:0)
问题可能是您的创建方法。我刚刚在我的应用程序中尝试了您的代码,并且像您一样尝试SELECT
。试试我在下面写的代码。
@med = Med.new(med_params)