开发者

update form redirecting to root_url on submit - Rails 3

开发者 https://www.devze.com 2023-02-02 18:54 出处:网络
Updated w/ dev log. See below I\'m using vanity urls in my routing and I\"m assuming it has something to do with it...

Updated w/ dev log. See below

I'm using vanity urls in my routing and I"m assuming it has something to do with it... In short my issue is I'm unable to have the user update their information. I can get the edit form to show up but on submit the update method doesn't run and I neither get a "fail" or "success".

The relevant routes look like this:

  resources :users do
    resources :friends
  end

 match '/:username/edit' => 'users#edit', :as => "edit_user"
 match '/:username' => 'users#show', :as => "user"

My form looks like this now, but I've tried a couple different things.

<%= form_for @user, :url => user_path(@user.username) do |form| %>  
<%= render 'shared/error_messages', :object => form.object %>
 <div class="form">  
    <p> <%= form.label :description, "Message to friends" %>  <br />  
     <%= form.text_area :description %> </p> 

     <%= form.submit "Update" %>  
 </div>  
 <% end %>

The edit and update controllers look like this:

 def edit 
    @user = User.find_by_username(params[:username]) 
  end

def update 
    @user = User.find(params[:id]) 
    if @user.update_attributes(params[:user]) 
       redirect_to user_url(current_user.username), :flash => { :success => "wham" }
    else
      redirect_to user_url(current_user.username), :error => { :error => "shiz" }
    end
end

Currently, when the update form is submitting, you get directed to the users url but nothing happens. I'd appreciate any help. Thanks!

Update ---- I have changed my routes as described below using to_param. This works just fine but the problem still persists where my update form just redirects to the root_url. Under 开发者_运维百科rake routes I get the correct path for update and the edit form is displaying correctly. Here is what I get form rake routes:

PUT    /users/:id(.:format)   {:controller=>"users", :action=>"update"}

And this is the dev log from submitting my form

Started POST "/1" for 127.0.0.1 at Wed Jan 05 16:09:54 -0500 2011
  Processing by UsersController#show as HTML
  Parameters: {"commit"=>"Update User", "authenticity_token"=>"OM1lIzizuFCYlxC3XmtmG/btqAsyjekHtqsiwlUDn3M=", "utf8"=>"✓", "username"=>"1", "user"=>{"description"=>"Update the message please"}}
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE ("users"."username" = '1') LIMIT 1
Redirected to http://0.0.0.0:3000/
ApplicationController::current_user
ApplicationController::current_user_session
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE ("users"."id" = 1) LIMIT 1
  CACHE (0.0ms)  SELECT "users".* FROM "users" WHERE ("users"."id" = 1) LIMIT 1
ApplicationController::current_user_session
Completed 302 Found in 44ms


Started GET "/" for 127.0.0.1 at Wed Jan 05 16:09:54 -0500 2011
  Processing by PagesController#home as HTML
ApplicationController::current_user
ApplicationController::current_user_session
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE ("users"."id" = 1) LIMIT 1
  CACHE (0.0ms)  SELECT "users".* FROM "users" WHERE ("users"."id" = 1) LIMIT 1
ApplicationController::current_user_session
ApplicationController::current_user
ApplicationController::current_user
ApplicationController::current_user
ApplicationController::current_user
Rendered pages/home.html.erb within layouts/application (22.2ms)
Completed 200 OK in 61ms (Views: 32.4ms | ActiveRecord: 0.2ms)

Why does that redirect happen??


Rather than defining custom routes for this, just define the to_param method on the User class:

def to_param
  username.parameterize
end

Then for your routes, the params[:id] value will be the username's paramertized version. For instance, mine would be ryan-bigg.


Even though your log doesn't indicate this is the case, I'd also check if the attr_accessible has :description added to it in User model. It's happened to me before - the form seems to work but information doesn't get stored.

0

精彩评论

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