今天在做R.R.log的时候发现个问题,在修改密码的时候如果没有通过校验,没有显示校验错误的信息。原来的代码如下:
if @user.update_attributes(:password => params[:user][:password])
flash[:notice] = '密码修改完成'
redirect_to :action => 'index'
else
redirect_to :action => 'change_pass', :id => @user
end
后来随手改了下第5行,把redirect_to改为render,居然就OK了。网上找了下才发现redirect_to和render还是有很多区别的,我以前居然一点都没有注意,汗..
redirect_to实现的是action方法的跳转,向浏览器发起一个新的请求,具体使用方法如下:
redirect_to :action => 'edit', :id => 7
redirect_to "http://wiisola.iteye.com/"
redirect_to "/images/1.jpg"
redirect_to :back
其中第4行是回到上一次访问的页面。
render可以翻译成"渲染",也就是说,render仅仅渲染了一个新的模板,而没有执行相应的action。render的用法如下:
render(:text => string)
render(:inline => string, [:type => "rhtml"|"rxml"])
render(:action => action_name)
render(:file => path, [:use_full_path => true|false])
render(:template => name)
render(:partial => name)
render(:nothing=>true)
render()
第1行:直接渲染出文本
第2行:把传入的string渲染成模板(rhtml或者rxml)
第3行:直接调用某个action的模板,相当于forward到一个view
第4行:使用某个模板文件render, 当use_full_path参数为true时可以传入相对路径
第5行:使用模板名render,e.x.: render(:template => "blog/short_list")
第6行:以局部模板渲染
第7行:什么也不输出,包括layout
第8行:默认的的render, 相当于render(:action => self)
补上一个手动render的例子:
def search
@results =Search.find(params[:query])
case @results
when 0 then render :action=> "no_results"
when 1 then render :action=> "show"
when 2..10 then render :action=> "show_many"
end
end
但是我自己的问题仍然没有解决,为什么用render渲染一个模板能够显示错误信息,但用redirect_to重新请求就没有呢?也许看源码能够解决吧,可惜看不懂,汗..总之以后记住render和redirect_to的用法就是了。
分享到:
相关推荐
Ruby代码 代码如下:if @user.update_attributes(:password => params[:user][:password]) flash[:notice...网上找了下才发现redirect_to和render还是有很多区别的,我以前居然一点都没有注意,汗.. redirect_to实现的是
允许您使用render :new, :alert => 'You messed up' ,就像您可以使用redirect_to thang, :notice => 'You made thang successfully' 。 与:notice , :alert和:flash => {:message => 'Sup foo'} ,就像redirect_...
找不到指定的模块。... 您可能感兴趣的文章:Ruby rails 页面跳转(render和redirect_to)Rails link_to 详解rails常用数据库查询操作、方法浅析学习Ruby你需要了解的相关知识(rvm, gem, bundle, rake,
1.将.rb文件和.rhtml文件都保存为utf-8格式; 2.在/app/Controller/application.rb中增加... 您可能感兴趣的文章:Ruby rails 页面跳转(render和redirect_to)学习Ruby你需要了解的相关知识(rvm, gem, bundle, rake, rail
首先,我们需要了解 Django 中两种主要的页面跳转方式:`redirect()` 和 `render_to_response()`。`redirect()` 函数用于重定向到一个新的 URL,而 `render_to_response()` 用于渲染模板并返回给客户端。 在给定的...
from django.shortcuts import render, redirect from .models import Product def product_list(request): products = Product.objects.all() return render(request, 'shopcar/product_list.html', {'products'...
redirect_to(path or path_helper) change(Class, :count).by(num) ###设置 初始化我们的酸奶应用 $ rails new yogurt_app -T 放入 rspec-rails gem gem 'rspec-rails' 捆 bundle install 初始化 rspec $ ...
format.ext_json { render :update => { :page => redirect_to(posts_path) } } else format.ext_json { render :json => @post.errors.to_json, :status => :unprocessable_entity } end end end ``` #### ...
典型的 Rails 控制器create操作如下所示: # POST /products # POST /products.json def create @product = Product.new(params[:product]) respond_to do |format| if @product.save format.html { redirect_to @...
from django.shortcuts import render, redirect from .forms import MyForm def upload_view(request): if request.method == 'POST': form = MyForm(request.POST, request.FILES) if form.is_valid(): ...
from flask import Flask, render_template, request, redirect, url_for app = Flask(__name__) @app.route('/') def home(): return render_template('home.html') if __name__ == '__main__': app.run...
Rails 2.0中,控制器继承自ActionController基类,提供了许多方便的方法,如`redirect_to`和`render`。 3. **模型(Model)** 模型代表数据库中的数据,通过ActiveRecord库实现,允许开发者使用Ruby代码操作数据库。...
from flask import Flask, render_template, redirect, url_for, request from flask_login import LoginManager, UserMixin, login_user, logout_user, login_required, current_user app = Flask(__name__) login...
return redirect('success_url') # Redirect to a success URL else: # If the request is GET, just show the form return render(request, 'books/publisher_add.html') ``` #### 四、Django Form 类 虽然...
file = SimpleFileField(upload_to='documents/') ``` 创建视图以处理文件上传: ```python from django.shortcuts import render from .models import Document from django_simple_file_handler.views import ...
format.html { redirect_to root_path, notice: 'File uploaded successfully.' } format.js end else render :new end end private def upload_params params.require(:upload).permit(:file) end end...
redirect_to @user, notice: 'User was successfully created.' else render :new end end private def user_params params.require(:user).permit(:name, :email, :avatar) end ``` **Paperclip的高级...
file = models.FileField(upload_to='attachments/') ``` **四、数据库迁移** 创建了模型后,我们需要将它们同步到数据库。执行以下命令: ```bash python manage.py makemigrations python manage.py migrate ``...
from django.shortcuts import render, redirect, get_object_or_404 from .models import FileModel from .forms import UploadFileForm def upload_file(request): if request.method == 'POST': form = ...
from django.shortcuts import render, redirect from .models import FileUpload def upload_file(request): if request.method == 'POST' and request.FILES['file']: file_obj = request.FILES['file'] new_...