ruby-on-rails – rails 4:使用form_for,无路由,POST
发布时间:2020-12-17 01:30:27 所属栏目:百科 来源:网络整理
导读:通过Lynda开始学习 Ruby on Rails – 非常兴奋,并尽我所能尽力练习.我正在进行练习,但是训练是基于Rails 3 – 到现在为止,一些用途不被接受. 情况如下: 我正在达到subject / new的创建形式 填写表格 得到以下错误作为回报 No route matches [POST] “/subje
通过Lynda开始学习
Ruby on Rails – 非常兴奋,并尽我所能尽力练习.我正在进行练习,但是训练是基于Rails 3 – 到现在为止,一些用途不被接受.
情况如下: 我正在达到subject / new的创建形式
我花了最近2个小时在stackoverflow,铁路指南和所有其他来源周围闲逛 – 尝试了很多变化,但无法超越这个阶段. 对你的帮助表示感谢. 的routes.rb SimpleCms::Application.routes.draw do root :to => "demo#index" get ':controller(/:action(/:id(.:format)))' end subjects_controller.rb class SubjectsController < ApplicationController def index list render('list') end def list @subjects = Subject.order("subjects.position ASC") end def show @subject = Subject.find(params[:id]) end def new @subject = Subject.new end def create # Instantiate a new object using form parameters @subject = Subject.new(params[:subject]) # Save the object if @subject.save # If save succeeds,redirect to the list action redirect_to(:action => 'list') else # If save fails,redisplay the form so user can fix problems render('new') end end end new.html.erb <%= link_to("<< Back to List",{:action => 'list'},:class => 'back-link') %> <div class="subject new"> <h2>Create Subject</h2> <%= form_for(:subject,:url => {:action => 'create'},:method => :post) do |f| %> <table summary="Subject form fields"> <tr> <th>Name</th> <td><%= f.text_field(:name) %></td> </tr> <tr> <th>Position</th> <td><%= f.text_field(:position) %></td> </tr> <tr> <th>Visible</th> <td><%= f.text_field(:visible) %></td> </tr> </table> <div class="form-buttons"> <%= submit_tag("Create Subject") %> </div> <% end %> </div> 解决方法
我已经完成了课程,我的route.rb如下所示:
Cms2::Application.routes.draw do root to: "public#index" get 'admin',:to => 'access#menu' get 'show/:id',:to => 'sections#show' get ':controller(/:action(/:id(.:format)))' post "admin_users/update" post "subjects/update" post "pages/update" post "sections/update" post "subjects/destroy" post "subjects/create" post "pages/destroy" post "pages/create" post "sections/destroy" post "sections/create" post "admin_users/destroy" post "admin_users/create" post "access/attempt_login" get "access/logout" end 我的def创建控制器如下: def create #new_position = params[:subject].delete(:position) # Instantiate a new object using form parameters @subject = Subject.new(params.require(:subject).permit(:name,:position,:visible,:created_at,:updated_at)) # Save the object if @subject.save #@subject.move_to_position(new_position) # If save succeeds,redirect to the list action flash[:notice] = "Subject Created." redirect_to(:action => 'list') else # If save fails,redisplay the form so user can fix problems @subject_count = Subject.count +1 render('new') end end 希望有所帮助! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |