我有一个简单的应用程序,其热线模型是一个mongoid文档。创建和视图工作正常但编辑我似乎无法为我的生活工作。
我希望有人能帮我辨别我做错了什么。
这是我正确运作的创建形式:
<%= form_for Hotline.new, :url => {:action => "create"} do |f| %>
<div class="row top-buffer">
<div class="form-group">
<label class="col-sm-1 rt-label" for="textinput">Address</label>
<div class="col-sm-3">
<%= f.text_field :city, class: 'form-control input-sm', placeholder: "City" %>
</div>
<label class="col-sm-1" for="textinput"></label>
<div class="col-sm-3">
<%= f.text_field :zip, class: 'form-control input-sm', placeholder: "Zip" %>
</div>
</div>
</div>
<br>
<div class="form-group">
<label class="col-sm-1" for="textinput"></label>
<%= f.submit "Save", class: 'btn btn-sm btn-success' %>
</div>
<% end %>
我尝试更改网址以便在我的编辑表单中发布或创建并使用h ...但它一直没有给我路由匹配错误。
No route matches {:action=>"patch", :controller=>"hotlines", :person_id=>"2"}
以下是包含视图和put或patch作为操作的表单:
<% @hotlines.each do |h| %>
<div id="log" class="visable">
<p>View Log</p>
<%= "City: " + h.city %><br>
<%= "Zip: " + h.zip %><br>
</div>
<br>
<div id="edit" class="visable">
<p>Edit Log</p>
<%= form_for h, :url => {:action => "patch"} do |f| %>
<div class="row top-buffer">
<div class="form-group">
<label class="col-sm-1 rt-label" for="textinput">Address</label>
<div class="col-sm-3">
<%= f.text_field :city, class: 'form-control input-sm', placeholder: "City" %>
</div>
<label class="col-sm-1" for="textinput"></label>
<div class="col-sm-3">
<%= f.text_field :zip, class: 'form-control input-sm', placeholder: "Zip" %>
</div>
</div>
</div>
<br>
<div class="form-group">
<label class="col-sm-1" for="textinput"></label>
<%= f.submit "Save", class: 'btn btn-sm btn-success' %>
</div>
<% end %>
</div>
<% end %>
不确定是否需要,但我的代码中还有一些内容。
控制器(我意识到更新不完整但我似乎无法进行渲染编辑)
class HotlinesController < ApplicationController
def index
@person = Person.find(params['person_id'])
#binding.pry
#@person = Person.find(params: id)
#@hotlines = Hotline.where(person_id: @person.id)
@hotlines = Hotline.where(person_id: params['person_id'])
end
def new
@new_hotline = Hotline.new
end
def create
@new_hotline = Hotline.new(hotline_params)
@new_hotline.person_id = params['person_id']
if @new_hotline.save
redirect_to person_hotlines_path(params['person_id']), notice: "Hotline was added successfully."
else
flash[:error] = "Error creating hotline. Please try again."
render :new
end
end
def update
@person = Person.find(params['person_id'])
@update_hotline = Hotline.find(params[:id])
if @update_hotline.update_attributes(person_params)
redirect_to person_hotlines_path(params['person_id']), notice: "Person was updated successfully."
else
flash[:error] = "Error saving person. Please try again."
render :edit
end
end
private
def hotline_params
params.require(:hotline).permit(:person_id, :city, :zip)
end
end
路线
devise_for :users
resources :users, only: [:update, :index, :new, :edit, :create]
resources :dashboard, only: [:index]
resources :people do
resources :hotlines, only: [:index, :create, :update, :new]
end
post 'dashboard/search'
get '/person/:id', to: 'person#show'
get 'welcome/index'
get 'welcome/about'
root to: 'welcome#index'
答案 0 :(得分:0)
您的更新网址问题与没有名称&#34; patch&#34;的行动和路线有关。请将其更新为:
<%= form_for h, :url => {:action => "update", :method => :put } do |f| %>
或使用命名路线。此控制台命令将帮助您使用可用路径:
"rake routes | grep user"
然后编辑表单变为:
<%= form_for h, :url => user_path(h) do |f| %>
欲了解更多信息,请访问: http://guides.rubyonrails.org/routing.html
希望它有所帮助。