Rails 关于 redirect_to 的 notice 方法

kouunn · 2015年05月06日 · 最后由 kouunn 回复于 2015年05月06日 · 3537 次阅读
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

ref

redirect_to({ action: 'atom' }, alert: "Something serious happened")
2 楼 已删除

看一下 View 里面写的有问题吗?

1 楼正解 你的写法 参数会被当成一个 hash 传进去 相当于第二个参数没传

#3 楼 @springwq

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 %>






这是全部代码

#1 楼 @etnl 我刚学 ruby

#这是原来的
redirect_to controller: "store", action: "index", notice:"fdsaf"

请问这样写对么?

redirect_to ({controller: 'store', action: 'index'}, notice:"fdsaf")

#4 楼 @zhang_soledad

redirect_to controller: "store", action: "index", notice:"fdsaf"

请问这句应该如何写?

#8 楼 @springwq

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

需要 登录 后方可回复, 如果你还没有账号请 注册新账号