ControlPanelController
又名control_panel_controller.rb是我的主控制器代码,我想在其中处理所有物流。 Picture class aka picture.rb是我使用的模型。
在我的资源中我有
resource :control_panel, only: [:index, :new, :create, :destroy], controller: 'control_panel'
rake路线告诉我
control_panel POST /control_panel(.:format) control_panel#create
new_control_panel GET /control_panel/new(.:format) control_panel#new
DELETE /control_panel(.:format) control_panel#destroy
这是我的ControllerPanelController
class ControlPanelController < ApplicationController
def index
@pictures = Picture.all
end
def new
@picture = Picture.new
end
def create
@picture = Picture.new(picture_params)
if @picture.save
redirect_to control_panel_path, notice: "The picture #{@picture.name} has been uploaded."
else
render "new"
end
end
def destroy
@picture = Picture.find(params[:id])
@picture.destroy
redirect_to control_panel_path, notice: "The picture #{@picture.name} has been deleted."
end
private
def picture_params
params.require(:picture).permit(:name, :attachment)
end
end
在我的app/views/control_panel/new.html.erb
我使用url: control_panel_path(@picture)
所以我可以发布到ControlPanel创建方法,但它不起作用。我得到了
错误1:
没有路线匹配[GET]“/ control_panel”
<% if !@picture.errors.empty? %>
<div class="alert alert-error">
<ul>
<% @picture.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= form_for @picture, url: control_panel_path(@picture), :html => {:multipart => true} do |f| %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.file_field :image %>
</p>
<p>
<%= f.label :remote_image_url, "or image URL" %><br />
<%= f.text_field :remote_image_url %>
</p>
<p><%= f.submit %></p>
<% end %>
错误2: 当我尝试从索引中删除资源时。我得到了
Couldn't find Picture without an ID
Extracted source (around line #22):
def destroy
@picture = Picture.find(params[:id])
@picture.destroy
redirect_to control_panel_path, notice: "The picture #{@picture.name} has been deleted."
end
第22行就是@picture = Picture.find(params [:id])
那么修复我的路线的正确方法是什么..我使用的是资源而不是资源,因为它只有一个控制面板,因为它是一个用户而不是多个用户的应用
修改 app / views / control_panel / index.html.erb
的代码<h1>Control Panel</h1>
<p>Manage all your pictures here</p>
<% if !flash[:notice].blank? %>
<div class="alert alert-info">
<%= flash[:notice] %>
</div>
<% end %>
<br />
<%= link_to "Add Picture", new_control_panel_path, class: "btn btn-primary" %>
<br />
<br />
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Name</th>
<th>Picture</th>
<th> </th>
</tr>
</thead>
<tbody>
<% @pictures.each do |picture| %>
<tr>
<td><%= picture.name %></td>
<td><%= image_tag picture.image_url(:thumb) %></td>
<td><%= button_to "Delete", control_panel_path(picture), method: :delete, class: "btn btn-danger", confirm: "Are you sure that you wish to delete #{pi
</tr>
<% end %>
</tbody>
</table>
**EDIT 2:**
通过修复button_to代码来实现删除工作
<td><%= button_to "Delete", control_panel_path(picture.id), method: :delete, class: "btn btn-danger", confirm: "Are you sure that you wish to delete #
</tr>
答案 0 :(得分:1)
错误1
您收到错误是因为您的应用没有与[GET]&#34; / control_panel&#34;相对应的路线。添加:显示您的control_panel路径并向ControlPanelController显示操作。还要记住,奇异资源没有:index route / action。
http://guides.rubyonrails.org/routing.html#singular-resources
错误2
您收到错误,因为params [:id]为零。 control_panel_path(picture)
未设置params[:id]
,因为您使用的是control_panel_path
而非pictures_path
。尝试将id
作为GET参数传递:control_panel_path(id: picture.id)
。
嵌套资源也是一种很好的方法:http://guides.rubyonrails.org/routing.html#nested-resources
答案 1 :(得分:0)
看来你的路线control_panel#destroy不接受id param 使用&#34; resourece s &#34;而不是&#34;资源&#34;如下。
resources :control_panel, only: [:index, :new, :create, :destroy], controller: 'control_panel'
这会产生这样的路线。
control_panel_index GET /control_panel(.:format)
POST /control_panel(.:format)
new_control_panel GET /control_panel/new(.:format)
control_panel DELETE /control_panel/:id(.:format)