0
@posts
の後のすべては、アプリケーション全体にわたって(Facebookの左側のサイドバーのように)持続する必要があります。どのコントローラで、アプリケーション全体に保持される要素を配置する必要がありますか?
def index
@title = "Posts"
default_order = "content_changed_at DESC"
params[:order_by] ||= default_order
@posts = current_user.subscribed_posts.paginate(:page => params[:page],
:per_page => 5,
:order => params[:order_by])
@subscribed_tags = current_user.subscribed_tags
@recent_posts = current_user.posts.limit(5).order("created_at DESC")
@tags= Tag.limit(20).order("ID asc")
@user = current_user
end
ビュー/レイアウト/ _sidebar.html.erb:私は彼らがアプリケーション全体で持続する場合、私は、コントローラのコードを配置する必要があり
<div class="user-profile">
<% avatar = image_tag(current_user.avatar.url(:thumb), :class => "authenticated-avatar") %>
<%= link_to avatar, "https://stackoverflow.com/users/#{current_user.id}" %>
<%= link_to "#{current_user.username}", "https://stackoverflow.com/users/#{current_user.id}", :class => "authenticated-username" %>
</div>
<%= form_for(@user, :remote => true) do |f| %>
<div class="field">
<%= f.label :subscribed_tag_names %><br />
<%= f.autocomplete_field :subscribed_tag_names, autocomplete_tag_name_posts_path, :"data-delimiter" => ' ', :class => "autocomplete_field" %>
</div>
<div class="actions">
<%= f.submit :class => "user_subscribed_tag_names_submit" %>
</div>
<% end %>
<div class="user-subscribed_tags">
<% @subscribed_tags.each do |subscribed_tag| %>
<%= link_to "#{subscribed_tag.name}(#{subscribed_tag.posts.count})", unsubscribe_tags_path(:unsubscribed_tag_name => subscribed_tag.name) %>
<% end %>
</div>
<div class="user-recent-posts">
<h4>Recent Posts</h4>
<ul>
<% @recent_posts.each do |recent_post| %>
<li><%= link_to recent_post.title, recent_post %></li>
<% end %>
</ul>
</div>
<div class="top-tags">
<% @tags.each do |tag| %>
<span class="tag-name"><%= tag.name %></span>
<span class="tag-count"><%= tag.posts.count %></span>
<% end %>
</div>
<% end %>
? (可能であれば、いくつかのサンプルコードを見たいと思います)。
「protect_from_forgery」の前後に配置する必要があります(ところで、それを削除すべきか、それとも便利ですか)。 – alexchenco
'protect_from_forgeryの前か後に' before_filter'を置いても問題はないと思います。しかし、私はいつも 'protect_from_forgery'を(ApplicationControllerクラスを定義した後で)一番上に表示しているようです。 いいえ、削除しないでください。 CSRF攻撃を防ぐことは絶対に必要です。その機能が開発されたとき、すべてのRailsアプリケーションでデフォルトで有効になっていることが非常に重要であることが判明しました。 afaik、 'protect_from_forgery'は、' authenticity_token'としてフォームに格納された 'id'をセッション変数に格納された' id'と比較する 'before_filter'を追加します。 – prasvin
したがって、生成されたフォームにはすべて非表示のidフィールドがあります。この 'id'フィールドが格納された' id'と一致しない場合、フォームの提出は受け入れられません。これにより、他のサイトの悪意のあるフォームがRailsアプリケーションに送信されることを防ぎます。たとえば、別のアプリが別の場所で実行されていたとします(localhost:3001など)。そこから(信頼性トークンなしの)投稿を現在のアプリに送信した場合、機能しません。詳細を理解するためには、それをもっと読むべきです。 – prasvin