开发者

How to check the existence of the User from the Autocomplete in rails

开发者 https://www.devze.com 2023-03-12 07:00 出处:网络
i am using rails 2.3.11. I have a text_field_with_autocomplete thing used inside a form of model blog

i am using rails 2.3.11. I have a text_field_with_autocomplete thing used inside a form of model blog

        <% remote_form_for :blog, :url => {:controller => "blogs", :action =>"add_blog"} do |f|%>
              <%= f.text_field :title, :class => "admtxt1 wd1 lfloat" %>

               <%= text_field_with_auto_complete "user_login", :login, { :size => 15}, { :url => {:controller => 'users', :action => 'autocomplete_for_blogowners'}, :method => :get, :param_name => 'term', :indicator => "spinner"} %>

                <%= submit_tag "Save", :class => "btn-small margintop10" %>
        <%end%>

In the blogs_controller,add_blog action i have

 def add_blog
   unless params[:blog].nil?
       @blog = Blog.new(params[:blog])
        @user = User.find_by_login(params["user_login"][:login])
        @blog.owner = @user
   end

  respond_to do |format|
    if @blog.save
           @saved = true
           format.js { render :action => 'add_blog' }
    else
           @saved = nil
           format.js { render 开发者_高级运维:action => 'add_blog' }
    end
  end
 end

In my add_blog.js.rjs file

     if @saved.nil?
           page.replace_html 'message','<div id="msg" class = "textcolor3">' + @blog.errors.full_messages.join('<br />') + '</div>'
     else
             page.replace_html 'message','<div id="msg" class = "h3txt textcolor3"> Blog created successfully </div>'
     end

My blog tablehas id,title,owner_id

And my model has the validation validates_presence_of :owner_id, :title

The above code works perfectly with the validation of owner shouldnt be blank

But it didnt checks for the wrong user

For eg. if i am typing something where such a user doesn't exists .In that case if i am submitting the form with no results found in the box , Even tat time i am getting Owner shouldn't be blank.

How to check the existence of the User in this case and to add that in the error messages.


Your code is very strange. Why don't you use Rails 3? Why don't you use REST and CRUD ? Sorry but it is better to rewrite code absolutely.

It seems as php code, have you used php?

0

精彩评论

暂无评论...
验证码 换一张
取 消