1

それでは、次のようなコードがあるとしましょう。

class PostsController < InheritedResources::Base 
  # Add before_filter here and devise should handle the redirection if the user is not signed in.
  before_filter :authenticate_user!, only: [:vote_up]

  def vote_up
  begin
  current_user.vote_for(@post = Post.find(params[:id]))
  redirect_to [@post]
  flash[:success] = "You have voted successfully"
rescue ActiveRecord::RecordInvalid
  redirect_to [@post]
  flash[:error] =  "You have already voted"
  end
 end

end

「投票に成功しました」または「すでに投票しました」というメッセージは表示されません。

私の見解では:

enter code here

<p id="notice"><%= notice %></p>
    <%= @post.embed .html_safe %>
    <header>
        <h7><%= @post.name %></h7>
    </header>
<h8><%= @post.title %></h8>
<article>
<%= @post.content .html_safe %>

<p>
<%= link_to 'Back', posts_path %> |
<%= link_to('Vote for this song!', vote_up_post_path(@post), :method => :post) %></p>

<p><id="notice"><%= notice %></p>

サイコロはありません。私はまだどこにもフラッシュメッセージを受け取っていません。私が間違っていることは何か分かりますか?

4

4 に答える 4

2

あなたが使用した

flash[:success] = "You have voted successfully"

あなたのコントローラーで、あなたは呼び出しました

<%= notice %>

あなたの見解で。コントローラーまたはビューで変更する必要があります。

あなたは付け加えられます

format.html { redirect_to @post, notice: 「投票に成功しました。」}

コントローラーで、必要なメッセージを取得します。

于 2013-07-18T11:38:40.500 に答える
0

それを行う最良の方法

<% if flash[:notice] %>
    <div class="notice"><%= flash[:notice] %></div>
<% end %>
于 2014-12-11T11:15:20.653 に答える
0

あなたのコードは次のようなものです:<p><id="notice"><%= notice %></p>

あなたが必要<p id="notice"><%= notice %></p>

于 2017-07-08T10:18:57.737 に答える