【问题标题】:rails form_for with Client Side Validation error on loadrails form_for 加载时客户端验证错误
【发布时间】:2012-12-17 06:02:41
【问题描述】:

我正在尝试在 Rails 中使用客户端验证 Gem。我的表单一直有效,直到将 `:validate => true" 添加到我的 form_for 中。然后我收到一个错误消息,告诉我我编写它的方式是错误的。我尝试像示例一样重新格式化它,它只是抛出错误。

错误

Using form_for(:name, @resource) is not supported with ClientSideValidations. Please use form_for(@resource, :as => :name) instead.

hashtag_controller

def home    
        @leaderboard = Hashtag.leaderboard_history
        @trends_display = Trend.trends_display
        @hash_create = ""
    end
    def create 
        @hash_create = Hashtag.create_hashtag(params[:hashtag])
        Hashlog.create_hashlog(params[:hashtag])
        @random_hashtag_pull = Hashtag.random_hashtags_pull
        @leaderboard = Hashtag.leaderboard_history_current
        respond_to do |format|
            format.html { redirect_to root_path }
            format.js 
        end
    end

hashtag.rb

class Hashtag < ActiveRecord::Base

  attr_accessible :text, :profile_image_url, :from_user, :created_at, :tweet_id, :hashtag, :from_user_name, :view_count, :wins

  hashtag_regex = /^[A-Za-z\d=#...-]+$/i

  validates :hashtag, :format => { :with => hashtag_regex }, :length => { :within => 2..20 }                    


 class << self

def create_hashtag(hashtag)                #creates new tweetvstweet for inputed hashtag with # for guests
    dash = "#"
    @hashtag_scrubbed = [dash, hashtag].join.downcase
    (User.current_user ? User.current_user.twitter : Twitter).search("%#{@hashtag_scrubbed}", :lang => "en", :count => 100, :result_type => "mixed").results.map do |tweet|
        unless exists?(tweet_id: tweet.id)
      create!(
        tweet_id: tweet.id,
        text: tweet.text,
        profile_image_url: tweet.user.profile_image_url,
        from_user: tweet.from_user,
        from_user_name: tweet.user.name, 
        created_at: tweet.created_at,
        hashtag: @hashtag_scrubbed,
        view_count: "0",
        wins: "0"
          ) 
        end  
    end
end



  def random_hashtags_pull                         #pulls 4 random hashtags for a vote first display
    Hashtag.where{ |hashtag| hashtag.hashtag =~ @hashtag_scrubbed}.order{"RANDOM()"}.limit(4).each(&:update_view_count)
  end

  def cast_vote_hashtag(hashtag)                    #pulls 4 random hashtags for a vote continued votes
    Hashtag.where{ |hashtag| hashtag.hashtag =~ @hashtag_scrubbed}.order{"RANDOM()"}.limit(4).each(&:update_view_count)
  end

  def leaderboard_history_current                  #displaying 5 highscore hashtags on the right
    Hashtag.where{ |hashtag| hashtag.hashtag =~ @hashtag_scrubbed}.order{"wins DESC"}.limit(5)  
  end

  def cast_vote(cast_vote)                           #counts number of wins
    Hashtag.increment_counter(:wins, cast_vote)
  end

  def leaderboard_history                            #right side leaderboard history
    Hashtag.order('wins DESC').limit(5)
  end
end

  def update_view_count                                  #updates the number of views
    Hashtag.increment_counter(:view_count, self.id)
    self.view_count += 1 
  end

home.html.erb

<div class="span6 votes-middle">
    <%= render 'shared/twitter_search' %>
    </div>

**search form**
<%= form_for(@hash_create, :url => hashtags_path, :validate => true, remote: true)  do |f| %>

                <div class="input-prepend input-append">
                <span class="add-on swag">#</span>
                <%= f.text_field :hashtag,class: "span4 swag_text_field", id:"appendedPrependedInput",  :validate => true, data: {autocomplete_source: autocomplete_index_path} %>

                <%= f.submit "VS!", class: "btn add-on-right swag_button" %>
                <% end %>

【问题讨论】:

    标签: jquery ruby-on-rails ruby-on-rails-3 jquery-ui postgresql


    【解决方案1】:

    我认为你应该为表单创建一个新对象

    在新操作中添加@hash_create = HashTag.new 并将您的表单更改为

    form_for(@hash_create, :url => hashtags_path, :validate => true, remote: true)
    

    【讨论】:

    • 验证现在有效,但它传递参数为 ` 参数:{"utf8"=>"✓", "authenticity_token"=>"1ZxXBeeMogGx4uApJM+1LvmdBv2JdTxdKzndYRYOAAE=", "hashtag"=>{" hashtag"=>"test"}, "commit"=>"VS!"}so I can no longer use params[:hashtag]` 所以这个功能不起作用。
    • 只需要使用 params[:hashtag][:hashtag]
    • 如果您使用form_for,它应该是必需的模型对象。请参考this 我们只能访问form_for 中text_field 的值,使用模型名称后跟text_field 名称,如params[:model_name][:field_name]。我了解您只想通过 params[:hashtag] 访问文本字段值,对吗?为此,您可以使用form_tag。 client_side_validations 也支持简单的形式,否则您可以将 text_field 更改为 text_field_tag 就像&lt;% text_field_tag "hashtag", params[:hashtag] %&gt;
    猜你喜欢
    • 2012-08-25
    • 2014-09-06
    • 1970-01-01
    • 2020-07-15
    • 1970-01-01
    • 1970-01-01
    • 2011-11-08
    • 1970-01-01
    • 2016-07-18
    相关资源
    最近更新 更多