form_tag调用自定义方法?调用先创建

时间:2012-12-08 05:51:58

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

我尝试使用<%= form_tag(hashtags_path, :method => :vote) do %>来调用自定义方法def vote,似乎我已经半途工作了。它首先调用def create,然后调用def vote。我不希望它首先运行创建,因为它此时会触发许多不需要的操作。

hashtags_controller

class HashtagsController < ApplicationController

    def home 

    end

    def vote
        redirect_to thanks_path
    end

    def show


    end

    def index

    end

    def create 
        Hashtag.pull_hashtag(params[:hashtag])
        @random_hashtag_pull = Hashtag.random_hashtags_pull
        respond_to do |format|
        format.html { redirect_to vote_path }
        format.js
    end
     end

end

_vote_tweets.html.erb

<%= form_tag(hashtags_path, :method => :vote) do %>
<div id="hashtags" class="twitter-hashtag-voting-block-v1">
    <% @random_hashtag_pull.each do |hashtag| %>
    <div class="span4 twitter-spans-v1" id="<%= hashtag.id %>">
    <div id="tweet-block-v1" class="hashtag-tweet-database-container">
    <div class="tweet-block-border-v1">
    <div class="tweet-block-spacing-v1">
        <div class="twitter-block-author-v1">
            <a class="twitter-block-user-v1" target="_blank" href="https://twitter.com/<%= hashtag.from_user %>">
            <span class="twitter-author-image-v1"><img alt="" class="twitter-author-image-photo-v1" src="<%= hashtag.profile_image_url %>"></span>
            <span class="twitter-author-name-v1"><%= hashtag.from_user_name %></span>
            <span class="twitter-author-nickname-v1">@<%= hashtag.from_user %></span></a>
            <iframe class="twitter-follow-button-v1" scrolling="no" frameborder="0" src="//platform.twitter.com/widgets/follow_button.html#align=right&button=grey&screen_name=<%= hashtag.from_user %>&show_count=false&show_screen_name=false&lang=en" allowtransparency="true"></iframe>
        </div>
    <div class="twitter-text-container-v1">
        <p class="twitter-text-field-v1"><%= sanitize(auto_link(hashtag.text).html_safe, :suppress_no_follow => true) %></p>
    </div>
    <div class="twitter-footer-v1">
        <a class="twitter-view-details-v1" target="_blank"  href="https://twitter.com/<%= hashtag.from_user %>/statuses/<%= hashtag.tweet_id %>">
        <span class="tweet-date-v1"><%= hashtag.created_at.strftime("%d %b %Y") %></span></a>
        <span class="twitter-vote-button-v1"><%= submit_tag "Vote!", class: "btn btn-mini btn-primary" %></span>
        <script type="text/javascript" src="//platform.twitter.com/widgets.js"></script>
        <ul class="twitter-intent-ul-v1">
        <li class="twitter-intent-li-v1"><a href="https://twitter.com/intent/tweet?in_reply_to=<%= hashtag.tweet_id %>" class="twitter-intent-tweet" title="Reply"></a></li>
        <li class="twitter-intent-li-v1"><a href="https://twitter.com/intent/retweet?tweet_id=<%= hashtag.tweet_id %>"  class="twitter-intent-retweet" title="Retweet"></a></li>
        <li class="twitter-intent-li-v1"><a href="https://twitter.com/intent/favorite?tweet_id=<%= hashtag.tweet_id %>"  class="twitter-intent-favorite" title="Favorite"></a></li>
        </ul>
        </div>
        </div>
        </div>
        </div>
        </div>
    <% end %>
</div>
<% end %>

的routes.rb

root :to => "hashtags#home"
  resources :hashtags do
    get 'vote', :on => :member
  end

  match '/vote', :to => 'hashtags#vote'

  match '/thanks', :to => 'pages#thanks'

1 个答案:

答案 0 :(得分:0)

必须创建自定义路线

match '/cast_vote', :to => 'hashtags#cast_vote'