new,create,show,index etc
但在这里我无法理解新行动的转变以创造行动
例如.当我点击新帖子它查找新动作时,现在它呈现_form,但是在提交时如何将数据输入到该特定表中,控制器的创建动作在哪里调用?如何?
我的posts_controller是
def new@post = [email protected]_ID = [email protected]_to do |format| format.HTML # new.HTML.erb format.Json { render Json: @post }endend# GET /posts/1/editdef edit@post = Post.find(params[:ID])authorize! :manage,@postend# POST /posts# POST /posts.Jsondef create@post = Post.new(params[:post])respond_to do |format| if @post.save format.HTML { redirect_to @post,notice: 'Post was successfully created.' } format.Json { render Json: @post,status: :created,location: @post } else format.HTML { render action: "new" } format.Json { render Json: @post.errors,status: :unprocessable_entity } endendend解决方法 默认情况下脚手架形式( read here)
When the user clicks the Create Post button on this form,the browser
will send information back to the create action of the controller
(Rails kNows to call the create action because the form is sent with
an http POST request; that’s one of the conventions that were
mentioned earlIEr):
def create @post = Post.new(params[:post]) respond_to do |format| if @post.save format.HTML { redirect_to(@post,:notice => 'Post was successfully created.') } format.Json { render :Json => @post,:status => :created,:location => @post } else format.HTML { render :action => "new" } format.Json { render :Json => @post.errors,:status => :unprocessable_entity } end endend
如果您想在新表格脚手架上自定义 *** 作,则应添加:url => {:action =>表单上的“YourActionname”}.
示例:
#formform_for @post,:url => {:action => "YourActionname"}#controllerdef YourActionname @post = Post.new(params[:post]) respond_to do |format| if @post.save format.HTML { redirect_to(@post,:status => :unprocessable_entity } end endend#routematch '/posts/YourActionname`,'controllers#YourActionname',:via => :post总结
以上是内存溢出为你收集整理的ruby-on-rails – 在rails中创建动作全部内容,希望文章能够帮你解决ruby-on-rails – 在rails中创建动作所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)