开发者

Rails 3 similar form on different page

I currently have these fields (:name :email :password :password_confirmation :image :desktopinfo) on one form. I would like to have :image and :desktopinfo on another page as well.

The current code for the first form is this:

<%= form_for(@user, :html => { :multipart => true }) do |f| %>
    <%= render 'shared/error_messages', :object => f.object %>
    <%= f.label :name %>
    <%= f.text_field :name %>

    <%= f.label :email %>
    <%= f.text_field :email %>

    <%= f.label :password %>
    <%= f.password_field :password %>

    <%= f.label :password_confirmation, "Confirmation" %>
    <%= f.password_field :password_confirmation %>

    <%= f.label :image %>
     <%= f.file_field :image %>

    <%= f.label :desktopinfo, "Desktop Info" %>
    <%= f.text_area :desktopinfo %>
    <%= f.submit "Update" %>
<% end %> 

When adding the following code to the separate page, it goes to the edit page (with the code above) and errors saying the password needs to be entered.

<%= form_for(@user, :html => { :multipart => true }) do |u| %>
    <%= render 'shared/error_messages', :object => u.object %>
    <%= u.label :image %>
    <%= u.file_field :image %>

    <%= u.label :desktopinfo, "Desktop Info" %>
    <%= u.text_area :desktopinfo %>
    <%= u.submit "Update" %>
<% end %>

This is a pain as I want the info (:image and :desktopinfo) to change without needing a password to be entered. As you can see I changed the f.label to u.label on the second form. Does this make any difference?

How would I go about doing this?

Thanks! Dean

UPDATE

The current code in the Users controller is:

def update
   开发者_StackOverflow if @user.update_attributes(params[:user])
        redirect_to @user, :flash => { :success => "Profile updated." }
    else
        @title = "Edit user"
        render 'edit'
    end
end

Where would I put @user.update_attributes!(:image => params[:image], :desktopinfo => params[:desktopinfo])

Also, I am getting undefined local variable or methodupdate_user_path'`.


It would seem you have some kind of authentication in place. Look for it in your controller or ApplicationController. Depending on which auth package you are using, you can disable it for certain actions. That's apparently what you want to do.


The problem here is because you are associating the form instance with your model, and the model validations are failing when you do not provide the password.

Take a look at the form_for documentation here:
http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html

You can use the form_tag here. An example is shown below:

<% form_tag(update_user_path, :method=>'post') do %>
 <p>
   Desktop Info:
   <%= text_field_tag "desktopinfo" %>
 </p>
 <p>
   Image:
   <%= file_field_tag "image" %>
 </p>
 <p>
   <%= submit_tag 'Submit' %>
 </p>
<% end %>

And in the controller update action, the form data will be available in params hash. Now instead of save you would have to call update_attributes for the fields you want to change: @user.update_attributes!(:image => params[:image], :desktopinfo => params[:desktopinfo])

Take care of validations.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜