private
# Use callbacks to share common setup or constraints between actions.
def set_cart
begin
@cart = Cart.find(params[:id])
rescue ActiveRecord::RecordNotFound
logger.error "Attempt to access invalid cart #{params[:id]}"
redirect_to controller: "store", action: "index", notice:"fdsaf"#notice: 'Invalid cart' #没有提示日的
else
respond_to do |format|
format.html #show.html.erb
format.json { render json: @cart}
end
end
end
redirect_to 后 notice 没有显示到页面,而是显示到地址栏中。
localhost:3000/store/index?notice=fdsaf
class StoreController < ApplicationController
def index
@products = Product.all
end
end
class CartsController < ApplicationController
before_action :set_cart, only: [:show, :edit, :update, :destroy]
# GET /carts
# GET /carts.json
def index
@carts = Cart.all
end
# GET /carts/1
# GET /carts/1.json
def show
end
# GET /carts/new
def new
@cart = Cart.new
end
# GET /carts/1/edit
def edit
end
# POST /carts
# POST /carts.json
def create
@cart = Cart.new(cart_params)
respond_to do |format|
if @cart.save
format.html { redirect_to @cart, notice: 'Cart was successfully created.' }
format.json { render :show, status: :created, location: @cart }
else
format.html { render :new }
format.json { render json: @cart.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /carts/1
# PATCH/PUT /carts/1.json
def update
respond_to do |format|
if @cart.update(cart_params)
format.html { redirect_to @cart, notice: 'Cart was successfully updated.' }
format.json { render :show, status: :ok, location: @cart }
else
format.html { render :edit }
format.json { render json: @cart.errors, status: :unprocessable_entity }
end
end
end
# DELETE /carts/1
# DELETE /carts/1.json
def destroy
@cart.destroy
respond_to do |format|
format.html { redirect_to carts_url, notice: 'Cart was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_cart
begin
@cart = Cart.find(params[:id])
rescue ActiveRecord::RecordNotFound
logger.error "Attempt to access invalid cart #{params[:id]}"
redirect_to ({controller: 'store', action: 'index'}, notice:"fdsaf")#notice: 'Invalid cart' #没有提示日的
else
respond_to do |format|
format.html #show.html.erb
format.json { render json: @cart}
end
end
end
# Never trust parameters from the scary internet, only allow the white list through.
def cart_params
params[:cart]
end
end
<% if notice %>
<p id= "notice"><%= notice %> </p>
<% end %>
<% @products.each do |product| %>
<div class="portfolio food" data-cat="logo">
<div class="portfolio-wrapper">
<%= image_tag(product.image_url) %>
<div class="label">
<div class="label-text">
<a class="text-title"><%= product.title %></a>
<%= button_to '加入购物车',
line_items_path(:product_id => product),
:class =>"cart" %>
<span class="text-category"><%= product.price %></span>
</div>
<div class="label-bg"></div>
</div>
</div>
</div>
<% end %>
这是全部代码
#这是原来的
redirect_to controller: "store", action: "index", notice:"fdsaf"
请问这样写对么?
redirect_to ({controller: 'store', action: 'index'}, notice:"fdsaf")
#5 楼 @kouunn 我昨晚也刚写到这里,你看一下我的 cart controller https://github.com/springwq/depot-rails/blob/develop/app/controllers/carts_controller.rb#L79
redirect_to store_url, notice: 'Invalid cart'
我按照你的改了,受教了,代码比我的好多了。 然后报错 undefined local variable or method `store_url' for #CartsController:0x007fd4786b1a08 这个 store_url 他找不到,是不是 routes.rb 需要加东西?
redirect_to ({controller: 'store', action: 'index'}, notice:"fdsaf")
这样不行么? http://api.rubyonrails.org/classes/ActionController/Redirecting.html
#10 楼 @zhang_soledad /vagrant/yameidie/app/controllers/carts_controller.rb:77: syntax error, unexpected ',', expecting ')' redirect_to ({action: 'index'},notice: "fdsaf") ^ /vagrant/yameidie/app/controllers/carts_controller.rb:77: syntax error, unexpected ')', expecting keyword_end