Topic: Suggestions on how to make this code better?

The code in the controller seems redundant; moreover, http://www.website.com/profile/edit_profile is accessible when i don't want it to be, i want the users to use the nav for editing profile sections. How can I strip the controller code so it doesn't seem redundant and accessible when it shouldn't be but yet keep the same views?

Here's the code:

        def edit_profile
        @user = User.find(session[:user_id])
        @user.profile ||= Profile.new
        @profile = @user.profile
        if param_posted?(:profile)
            if @user.profile.update_attributes(params[:profile])
                flash[:notice]    = "Profile changes saved."
                redirect_to :controller => "user", :action => "index"
            end
        end
    end
   
    def edit_basic
        @title = "Editing Basic Information"
        edit_profile
    end
   
    def edit_education
        @title = "Editing Education"
        edit_profile
    end
   
    def edit_misc
        @title = "Editing Misc. Information"
        edit_profile
    end

      <div id="content_header">
        <h2><%= @title %></h2>
            <ul id="subnav">
                <li><%= nav "Basic Information", "profile", "edit_basic" %></li>
                <li><%= nav "Education", "profile", "edit_education" %></li>
                <li><%= nav "Misc Information", "profile", "edit_misc" %></li>
            </ul>
    </div>

Re: Suggestions on how to make this code better?

What you could do is have all the nav items pass a get param:

.../edit_profile?section=basic

and have something like this:

section_hash = {:basic => ["Editing Basic Information", "edit_basic"], :edit_education => ["Editing Education", "edit_education"] }

and set which view to render:

if params[:section] == "basic"
render :action => section_hash[:basic][1]
...

I'd also recommend having an update_profile method instead of doing the updating in edit_profile.

As far as accessibly, why does it matter if they use the link directly instead of the nav? As long as they have permission I don't see a problem there.

Re: Suggestions on how to make this code better?

you could simply make the edit_profile action private

  def edit_basic
    @title = "Editing Basic Information"
    edit_profile
  end

  def edit_education
    @title = "Editing Education"
    edit_profile
  end

  def edit_misc
    @title = "Editing Misc. Information"
    edit_profile
  end

private
  def edit_profile
    #blabla
  end


and normally it's not a rails convention to handle the form display and the data processing after submit of the form in the same action. it's nromally split into 2 actions, like "new" and "create, and "edit" and "update"

- You would only have you 3 edit_whatever actions to display the form, and the update action to do the profile data update when the form was submitted.
- the @user setting belongs into a before_filter, preferrably in the application controller