如何用rails 4的form_for实现无路由的POST请求?

2026-04-11 16:322阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计663个文字,预计阅读时间需要3分钟。

如何用rails 4的form_for实现无路由的POST请求?

通过Lynda开始学习Ruby on Rails,极具挑战性,并竭尽所能地练习。目前正在进行练习,但基于Rails 3的培训已停止,一些用途不被接受。情况如下:我正在达到subject / new的创建形式,但填写“

通过Lynda开始学习 Ruby on Rails – 非常兴奋,并尽我所能尽力练习.我正在进行练习,但是训练是基于Rails 3 – 到现在为止,一些用途不被接受.

如何用rails 4的form_for实现无路由的POST请求?

情况如下:

我正在达到subject / new的创建形式
填写表格
得到以下错误作为回报

No route matches [POST] “/subjects/create”

Rails.root: /Users/alpozenalp/Sites/simple_cms

我花了最近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

希望有所帮助!

本文共计663个文字,预计阅读时间需要3分钟。

如何用rails 4的form_for实现无路由的POST请求?

通过Lynda开始学习Ruby on Rails,极具挑战性,并竭尽所能地练习。目前正在进行练习,但基于Rails 3的培训已停止,一些用途不被接受。情况如下:我正在达到subject / new的创建形式,但填写“

通过Lynda开始学习 Ruby on Rails – 非常兴奋,并尽我所能尽力练习.我正在进行练习,但是训练是基于Rails 3 – 到现在为止,一些用途不被接受.

如何用rails 4的form_for实现无路由的POST请求?

情况如下:

我正在达到subject / new的创建形式
填写表格
得到以下错误作为回报

No route matches [POST] “/subjects/create”

Rails.root: /Users/alpozenalp/Sites/simple_cms

我花了最近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

希望有所帮助!