如何将模型#new中的变量传递给重定向后的模型#show

时间:2014-10-19 03:01:18

标签: ruby-on-rails-4

我有一个photo_link变量,我想在show.html中显示

所以我在视图中单击“创建”,它将我带到效果#create

  def create
    @effect = Effect.new(effect_params)
    @effect.save
    @pid = params[:effect][:pid]
    @photo_link = apply_effect1(@pid)
    respond_with(@effect)
  end

当我查看日志时,我认为它是重定向到效果#show,然后显示到show.html

Started POST "/effects" for 127.0.0.1 at 2014-10-19 13:56:21 +1100
Processing by EffectsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"zFqMGJkUQeSs+KqUI1ewWl1t6beEc9LZaRRlI8TKrCY=", "effect"=>{"effect1"=>"0", "effect2"=>"0", "effect3"=>"0", "pid"=>"37"}, "commit"=>"Create Effect"}
Unpermitted parameters: pid
   (0.1ms)  begin transaction
  SQL (0.4ms)  INSERT INTO "effects" ("created_at", "effect1", "effect2", "effect3", "updated_at") VALUES (?, ?, ?, ?, ?)  [["created_at", "2014-10-19 02:56:21.056844"], ["effect1", "f"], ["effect2", "f"], ["effect3", "f"], ["updated_at", "2014-10-19 02:56:21.056844"]]
   (1.0ms)  commit transaction
"37"
  Post Load (0.1ms)  SELECT  "posts".* FROM "posts"  WHERE "posts"."id" = ? LIMIT 1  [["id", 37]]

Redirected to http://localhost:3000/effects/60
Completed 302 Found in 704ms (ActiveRecord: 1.6ms)


Started GET "/effects/60" for 127.0.0.1 at 2014-10-19 13:56:21 +1100
Processing by EffectsController#show as HTML
  Parameters: {"id"=>"60"}
  Effect Load (0.2ms)  SELECT  "effects".* FROM "effects"  WHERE "effects"."id" = ? LIMIT 1  [["id", 60]]
  Rendered effects/show.html.erb within layouts/loginpage (0.6ms)
  User Load (0.2ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 1  ORDER BY "users"."id" ASC LIMIT 1
Completed 200 OK in 391ms (Views: 388.9ms | ActiveRecord: 0.4ms)

这是我的效果#show

  def show
    p "HIHI IM HERE"
    p params
    respond_with(@effect)
  end

无论如何我可以将@photo_link传递给show.html吗?

1 个答案:

答案 0 :(得分:0)

默认情况下,

respond_with会将您重定向到show动作。你想要做的是自定义location的{​​{1}}参数,这样你就可以被重定向到正确的位置(在这种情况下,仍然是show动作,但有额外的参数)。见http://apidock.com/rails/ActionController/MimeResponds/respond_with

我认为它类似于:

respond_with