rails flash.now来自partial

时间:2013-03-24 01:59:09

标签: ruby-on-rails ruby ruby-on-rails-3

我有一个应该显示flash消息的控制器。闪光信息未显示。 我唯一的问题是没有出现的flash消息。如果有人可以请为此解释这将是非常棒的。我猜我的错误是noobish但我不知道解释发生了什么。

编辑:我为此部分添加了我的日志

编辑:当我点击包含无效信息的管理按钮时,没有任何反应。当我刷新页面时,闪烁消息显示出来。我真的只需刷新当前页面,但它无法正常工作。如果有人有任何想法,请分享。

编辑:我尝试过改变一切 redirect_to @department, :notice => "invalid Password"

我仍然无法显示Flash消息。

编辑:从this网站开始,现在说flash将显示在您正在渲染的视图中。但我渲染的观点是偏袒的。任何人都可以至少让我知道我是否在正确的轨道上。除了我自己以外的一点方向或想法?

这是我的关系的创建方法。

def create
    @department = Department.find(params[:manager_relationship][:department_id])
    if @department.authenticate(params[:manager_relationship][:password])
    current_user.manage!(@department)
    respond_to do |format|
        format.html { redirect_to @department }
        format.js
        end
    else
        redirect_to department_path@department, :alert => "Invalid Password"
    end
end

在我的会话控制器中成功使用了相同的代码。

SessionsController
if user && user.authenticate(params[:session][:password])
        sign_in user
        redirect_back_or user
    else
        flash.now[:error] = 'Invalid email/password combination'
        render 'new'
    end

从部分

调用

部/ show.hmtl.erb

<% provide(:title, @department.department_name) %>
<div class="span8">
    <%= render 'manage_form' if signed_in? %>
</div>

_manage_form.html.erb

<div id="manage_form">
<% if current_user.managing?(@department) %>
<%= render 'unmanage' %>
<% else %>
<%= render 'manage' %>
<% end %>
</div>

_manage.html.erb

<%= form_for(current_user.manager_relationships.build(department_id: @department.id), remote: true) do |f| %>

    <div>
    <%= f.label :password %>
    <%= f.password_field :password %>
    </div>

    <div><%= f.hidden_field :department_id %></div>
    <%= f.submit "Manage department", class: "btn btn-large btn-primary" %>
<% end %>

application.html.erb

<!DOCTYPE html>
<html>
<head>
.
.
.
...
<body>
<%= render 'layouts/header' %>
<div class="container">
  <% flash.each do |key, value| %>
    <div class="alert alert-<%= key %>"><%= value %></div>
  <% end %>
  <%= yield %>
  <%= render 'layouts/footer' %>

</div>

以下是取消管理和管理http请求的日志

Started DELETE "/manager_relationships/22" for 127.0.0.1 at 2013-03-24 11:08:20 -0700
Processing by ManagerRelationshipsController#destroy as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Y3I7qY4LBfChjdYAUlJ1eDh23YkcJRwSRfa6s2wavoI=", "commit"=>"Unmanage", "id"=>"22"}
[1m[36mUser Load (0.0ms)[0m  [1mSELECT "users".* FROM "users" WHERE "users"."remember_token" = '9J1RAJy16Tooz5wMmzQohw' LIMIT 1[0m
     [1m[35mManagerRelationship Load (0.0ms)[0m  SELECT "manager_relationships".* FROM "manager_relationships" WHERE "manager_relationships"."id" = ? LIMIT 1  [["id", "22"]]
[1m[36mDepartment Load (1.0ms)[0m  [1mSELECT "department".* FROM "departments" WHERE "department"."id" = 1 LIMIT 1[0m
[1m[35mManagerRelationship Load (0.0ms)[0m  SELECT "manager_relationships".* FROM "manager_relationships" WHERE "manager_relationships"."user_id" = 1 AND "manager_relationships"."department_id" = 1 LIMIT 1
[1m[36m (0.0ms)[0m  [1mbegin transaction[0m
[1m[35mSQL (0.0ms)[0m  DELETE FROM "manager_relationships" WHERE "manager_relationships"."id" = ?      [["id", 22]]
[1m[36m (26.0ms)[0m  [1mcommit transaction[0m
Rendered departments/_manage.html.erb (2.0ms)
Rendered manager_relationships/destroy.js.erb (5.0ms)
Completed 200 OK in 86ms (Views: 11.0ms | ActiveRecord: 27.0ms)


Started POST "/manager_relationships" for 127.0.0.1 at 2013-03-24 11:08:23 -0700
Processing by ManagerRelationshipsController#create as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Y3I7qY4LBfChjdYAUlJ1eDh23YkcJRwSRfa6s2wavoI=",     "manager_relationship"=>{"password"=>"[FILTERED]", "department_id"=>"1"}, "commit"=>"Manage Bar"}
[1m[35mUser Load (0.0ms)[0m  SELECT "users".* FROM "users" WHERE "users"."remember_token" =     '9J1RAJy16Tooz5wMmzQohw' LIMIT 1
[1m[36mDepartment Load (0.0ms)[0m  [1mSELECT "departments".* FROM "departments" WHERE "departments"."id" = ? LIMIT 1[0m      [["id", "1"]]
Redirected to http://localhost:3000/departments/1
Completed 406 Not Acceptable in 91ms (ActiveRecord: 0.0ms)

1 个答案:

答案 0 :(得分:0)

首先,我在你的视图中看不到你在哪里显示闪光灯。您应该在要显示消息的视图中执行以下操作:

<% if flash[:success] %>
  <p>
   <%= flash[:success] %>
  </p>
<% end %>

<% if flash[:error] %>
  <p>
   <%= flash[:error] %>
  </p>
<% end %>

其次,当您进行重定向时,应使用flash[:success] = 'message'而不是flash.now[:success]。使用flash.now是您没有重定向用户,只是渲染视图。