簡體   English   中英

Rails路由出現問題(表單未發布以創建操作)

[英]Issue with Rails routes (form not POSTing to create action)

我的new.html.erb表單未發布,也未在數據庫中創建對象。 相反,當我單擊“提交”按鈕時,URL更改為meds/new?utf8=✓&authenticity_token=<random characters> 看起來它正在嘗試執行GET,而我認為應該嘗試POST創建對象。 看起來應用程序甚至沒有試圖在Controller中調用我的create動作。 這是我在控制台中得到的:

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

我很確定問題出在我的路線上。 我的create動作甚至沒有前綴。 這是運行耙路徑的結果:

                 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="&#x2713;" /><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>

生成的HTML中有兩個結束符</form> -標簽。 這不是有效的HTML。 如果有環繞的形式,則必須將內部的形式取出

問題可能出在您的創建方法上。 我只是在應用程序中嘗試過您的代碼,就像您嘗試了SELECT 嘗試使用我在下面編寫的代碼。

@med = Med.new(med_params)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM