I am using rails 4.0.0. I am a newcomer to rails and learning rails watching video tutorials and stuck in a problem so need some help.
我正在使用rails 4.0.0。我是铁路和学习铁路的新手,观看视频教程并陷入困境,因此需要一些帮助。
I have a controller called Subjects and following is the code
我有一个名为Subjects的控制器,以下是代码
Subject Constoller
主题构造函数
class SubjectsController < ApplicationController
before_action :set_subject, only: [:show, :edit, :update]
def index
@subjects = Subject.all
end
def list
@subjects = Subject.all
#@subjects = Subject.order("subjects.position ASC")
end
def show
@subject = Subject.find(params[:id])
end
def new
@subject = Subject.new(:name => "default")
end
def create
@subject = Subject.new(subject_params)
if @subject.save
redirect_to(:action => 'list')
else
render('new')
end
end
def edit
end
def update
if @subject.update(subject_params)
redirect_to @subject
else
render("edit")
end
end
def delete
@subject = Subject.find(params[:id])
end
def destroy
Subject.find(params[:id]).destroy
redirect_to(:action => "list")
end
private
# Use callbacks to share common setup or constraints between actions.
def set_subject
@subject = Subject.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def subject_params
params.require(:subject).permit(:name, :position, :visible)
end
end
I created a view for the List Action
我为List Action创建了一个视图
View for the List Action
查看列表操作
<h1>List</h1>
<div class="subject list">
<h2>Subjects</h2>
<table class="listing" summary="Subject list">
<tr class="header">
<th>Position</th>
<th>Subject</th>
<th>Visible</th>
<th>Pages</th>
<th>Actions</th>
</tr>
<% @subjects.each do |subject| %>
<tr>
<td><%= subject.position %></td>
<td><%= subject.name %></td>
<td class="center"><%= subject.visible ? 'Yes' : 'No' %></td>
<td class="center"><%= subject.pages.size %></td>
<td class="actions">
<%= link_to 'Show', subject, :class => 'action show'%>
<%= link_to "Edit", edit_subject_path(subject), :class => 'action edit' %>
<%= link_to "Delete", {:action => 'delete', :id => subject.id}, :class => 'action delete' %>
</td>
</tr>
<% end %>
</table>
</div>
I also have a view for delete and code is as follows
我也有删除视图,代码如下
View for Delete Action
查看删除操作
<h1>Delete</h1>
<%= link_to("<< Back to List", {:action => 'list'}, :class => 'back-link') %>
<div class="subject delete">
<h2>Delete Subject</h2>
<%= form_for(:subject, :url => {:action => "destroy", :id => @subject.id}) do |f|%>
<p>Are you sure you want to permanently delete this subject?</p>
<p class="reference-name"><%= @subject.name %></p>
<div class="form-buttons">
<%= submit_tag("Delete Subject") %>
</div>
<% end %>
</div>
Here is the configuration of my routes file
这是我的路线文件的配置
SimpleCms::Application.routes.draw do
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
# root 'welcome#index'
root 'demo#index'
# Example of regular route:
# get 'products/:id' => 'catalog#view'
get 'demo/index'
get 'demo/hello'
get 'demo/other_hello'
#get 'subjects/index'
get 'subjects/list'
get 'subjects/delete'
get '/subjects/:id/destroy' => 'subjects#destroy'
resources :subjects
get 'subjects/show'
get 'subjects/new'
get 'subjects/:id' => 'subjects#show'
# Example of named route that can be invoked with purchase_url(id: product.id)
# get 'products/:id/purchase' => 'catalog#purchase', as: :purchase
# Example resource route (maps HTTP verbs to controller actions automatically):
# resources :products
#resources :subjects
# Example resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# end
# end
# Example resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end
# Example resource route with more complex sub-resources:
# resources :products do
# resources :comments
# resources :sales do
# get 'recent', on: :collection
# end
# end
# Example resource route with concerns:
# concern :toggleable do
# post 'toggle'
# end
# resources :posts, concerns: :toggleable
# resources :photos, concerns: :toggleable
# Example resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end
end
So the main concept is as follows when i will call http://localhost:3000/subjects/list
it will show all the subjects and there is also three button that is show, edit and delete and after clicking on the delete link it will go to the delete view and after clicking on the submit button of the form it will be deleting the particular item.
因此主要概念如下我将调用http:// localhost:3000 /臣/列表它将显示所有主题,还有三个按钮,即显示,编辑和删除,点击删除链接后,它将转到删除视图,单击表单的提交按钮后,它将删除特定项目。
But the main problem is that the form_for in delete view is producing the url in the form action is /subjects/8 where 8 is the id of the subject to be deleted but the url should be /subjects/destroy?id=8 to delete the item but when id is passed along with the action in form_for it produces the wrong url but when only the action is passed it produces the url as /subjects/destroy.
但主要的问题是,delete视图中的form_for正在生成表单动作的url / subject / 8其中8是要删除的主题的id但是url应该是/ subjects / destroy?id = 8要删除该项目,但是当id与form_for中的动作一起传递时,它会生成错误的URL,但是当只传递动作时,它会将url生成为/ subjects / destroy。
I cannot figure out what is wrong so please help. Thanks to all in advance.
我无法弄清楚出了什么问题所以请帮忙。感谢所有提前。
2 个解决方案
#1
3
Try a link with delete method instead use a form.
尝试使用删除方法的链接而不是使用表单。
<p>Are you sure you want to permanently delete this subject?</p>
<p class="reference-name"><%= @subject.name %></p>
<%= link_to 'Delete Subject', @subject, method: :delete %>
UPDATE
UPDATE
If you want to use a form for destroy action, try this :
如果要使用表单进行销毁操作,请尝试以下操作:
<% form_for @subject, :html => {:method => :delete} do |form| %>
#2
0
Try this:
尝试这个:
<%= form_for(@subject, :url => {:action => "destroy") do |f|%>
Instead of this:
而不是这个:
<%= form_for(:subject, :url => {:action => "destroy", :id => @subject.id}) do |f|%>
You don't need to mention the id because in form_for it takes id of the variable mentioned by default.
您不需要提及id,因为在form_for中它默认使用提到的变量的id。
#1
3
Try a link with delete method instead use a form.
尝试使用删除方法的链接而不是使用表单。
<p>Are you sure you want to permanently delete this subject?</p>
<p class="reference-name"><%= @subject.name %></p>
<%= link_to 'Delete Subject', @subject, method: :delete %>
UPDATE
UPDATE
If you want to use a form for destroy action, try this :
如果要使用表单进行销毁操作,请尝试以下操作:
<% form_for @subject, :html => {:method => :delete} do |form| %>
#2
0
Try this:
尝试这个:
<%= form_for(@subject, :url => {:action => "destroy") do |f|%>
Instead of this:
而不是这个:
<%= form_for(:subject, :url => {:action => "destroy", :id => @subject.id}) do |f|%>
You don't need to mention the id because in form_for it takes id of the variable mentioned by default.
您不需要提及id,因为在form_for中它默认使用提到的变量的id。