rails 杂记 - render and layout
def update @book = Book.find(params[:id]) if @book.update(book_params) redirect_to(@book) else render "edit" end end
def update @book = Book.find(params[:id]) if @book.update(book_params) redirect_to(@book) else render :edit end end
render "products/show"
render template: "products/show"
render file: "/u/apps/warehouse_app/current/app/views/products/show"
render :edit render action: :edit render "edit" render "edit.html.erb" render action: "edit" render action: "edit.html.erb" render "books/edit" render "books/edit.html.erb" render template: "books/edit" render template: "books/edit.html.erb" render "/path/to/rails/app/views/books/edit" render "/path/to/rails/app/views/books/edit.html.erb" render file: "/path/to/rails/app/views/books/edit" render file: "/path/to/rails/app/views/books/edit.html.erb"
render inline: "<% products.each do |p| %><p><%= p.name %></p><% end %>"
render inline: "xml.p {'Horrid coding practice!'}", type: :builder
render plain: "OK"
render html: "<strong>Not Found</strong>".html_safe
render json: @product render xml: @product
render js: "alert('Hello Rails');"
render body: "raw"
:content_type :layout :location :status :formats
render file: filename, content_type: "application/rss"
render layout: "special_layout"
render layout: false
render xml: photo, location: photo_url(photo)
render status: 500 render status: :forbidden
render formats: :xml render formats: [:json, :xml]
class ProductsController < ApplicationController layout "inventory" #... end
class ProductsController < ApplicationController layout "product", except: [:index, :rss] end
# in app/controllers/application_controller class ApplicationController < ActionController::Base end # in app/controllers/admin_controller class AdminController < ApplicationController end # in app/controllers/admin/products_controller class Admin::ProductsController < AdminController def index end end
app/views/admin/products/ app/views/admin/ app/views/application/
def show @book = Book.find(params[:id]) if @book.special? render action: "special_show" end end
redirect_to photos_url
redirect_back(fallback_location: root_path)
这里调用 redirect_to时, Rails 默认使用 HTTP status
redirect_to photos_path, status: 301
get '/stories', to: redirect { |path_params, req| "/articles/#{req.subdomain}" }
def index @books = Book.all end def show @book = Book.find_by(id: params[:id]) if @book.nil? render action: "index" end end
def index @books = Book.all end def show @book = Book.find_by(id: params[:id]) if @book.nil? redirect_to action: :index end end
def index @books = Book.all end def show @book = Book.find_by(id: params[:id]) if @book.nil? @books = Book.all flash.now[:alert] = "Your book was not found" render "index" end end
Asset tags yield and content_for Partials
auto_discovery_link_tag javascript_include_tag stylesheet_link_tag image_tag video_tag audio_tag
<html> <head> <%= yield :head %> </head> <body> <%= yield %> </body> </html>
<% content_for :head do %> <title>A simple page</title> <% end %> <p>Hello, Rails!</p>
<html> <head> <title>A simple page</title> </head> <body> <p>Hello, Rails!</p> </body> </html>
<%= render "menu" %>
<%= render partial: "link_area", layout: "graybar" %>
<h1>New zone</h1> <%= render partial: "form", locals: {zone: @zone} %>
<%= form_for(zone) do |f| %> <p> <b>Zone name</b><br> <%= f.text_field :name %> </p> <p> <%= f.submit %> </p> <% end %>
<%= render partial: "customer", object: @new_customer %>
直接渲染
<%= render @customer %>
<h1>Products</h1> <%= render partial: "product", collection: @products %>
<p>Product Name: <%= product.name %></p>
<h1>Products</h1> <%= render @products %>
<h1>Contacts</h1> <%= render [customer1, employee1, customer2, employee2] %>
<p>Customer: <%= customer.name %></p>
<p>Employee: <%= employee.name %></p>
<h1>Products</h1> <%= render(@products) || "There are no products available." %>
<%= render partial: "product", collection: @products, as: :item %>
<html> <head> <title><%= @page_title or "Page Title" %></title> <%= stylesheet_link_tag "layout" %> <style><%= yield :stylesheets %></style> </head> <body> <div id="top_menu">Top menu items here</div> <div id="menu">Menu items here</div> <div id="content"><%= content_for?(:content) ? yield(:content) : yield %></div> </body> </html>
<% content_for :stylesheets do %> #top_menu {display: none} #right_menu {float: right; background-color: yellow; color: black} <% end %> <% content_for :content do %> <div id="right_menu">Right menu items here</div> <%= content_for?(:news_content) ? yield(:news_content) : yield %> <% end %> <%= render template: "layouts/application" %>