开发者

Rails 3 Route error - "No Route Matches"

开发者 https://www.devze.com 2023-04-01 02:25 出处:网络
I\'ve been reading over this resource as well as this post to try to understand Routes more (currently learning programming/Rails by doing) but am wondering how I can fix the error I\'m getting, which

I've been reading over this resource as well as this post to try to understand Routes more (currently learning programming/Rails by doing) but am wondering how I can fix the error I'm getting, which is No route matches {:controller=>"profiles", :action=>"show"}.

I get the error working my way开发者_如何学JAVA through a Rails 3 sign-up process using nested model forms. The sign-up process, as follows:

user = User.new
user.email = ""
user.password = ""
user.profile = Profile.new
user.profile.save
user.save

The sign-up process starts at the homepage with the following form:

<%= form_for :user, :url => signup_path, :html => {:id => 'homepage'} do |f| %>
  <div>
  ...
  </div>
  <%= f.fields_for :profile do |f| %>
  <% end %>
<% end %>

Then the process goes to fill in the profile, then redirect to the new User's profile after this form is completed:

<%= form_for :profile, :html => { :multipart => true } do |f| %>
  <div>
  ...
  </div>
  <%= f.fields_for :user do |f| %>
  <% end %>
<% end %>

I have accepts_nested_attributes_for :user and :profile in their respective models.

My rails server it gives me a bit more detail:

ActionController::RoutingError (No route matches {:controller=>"profile.save",     :action=>"show"}):
  app/controllers/profiles_controller.rb:15:in `create'

So in my ProfilesController in 'create':

def create
  @profile = Profile.new(params[:profile])
  if @profile.save
    redirect_to profile_path, :notice => 'User successfully added.'
  else
    render :action => 'new'
  end
end

Seems clear that the issue is in profile_path, so my Routes.rb:

post "/signup" => "profiles#create", :as => "signup"
match "skip/signup", :to => "info#signupskip"
match "skip/profiles/new", :to => "profiles#newskip"
root :to => "users#create"

Can anyone help shed light on what I'm doing wrong/missing in my Routes.rb file?


The redirect path should contain the specific profile to redirect to:

if @profile.save
   redirect_to profile_path(@profile), :notice => 'User successfully added.'
else
.....

Also the routes should include this line:

get "/profiles/:id" => "profiles#show", as => "profile" 
0

精彩评论

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

关注公众号