views:

427

answers:

4

Could anyone help with this problem:

Upon "create", the user is redirected to the url: model/model_id (eg post/1), instead I am redirected to models/url_encoding_object (eg posts/.%23) and there is an "406 Not Acceptable" message in the console.

Typically, upon create, the console's message is "Processing PostsController#create (for 000.0.0.0 at 2009-11-23 12:32:52) [POST]", but with this error, the message is "Processing PostsController#create to # (for 000.0.0.0 at 2009-11-23 12:32:52) [POST]"

I've seen austinfromboston's response and tried his "old fashioned but effective" solution to that similar problem, but it doesn't work for me.

Any help would be greatly appreciated

Controller Code:

# POST /groups
# POST /groups.xml
def create
  @group = Group.new(params[:group])
  @group.category = params[:category]
  @group.user = current_user

  #here we add the current user to the membership collection of the group
  @membership = @group.memberships.build(params[:membership])
  @membership.group = @group
  @membership.user = current_user
  @membership.initiator = false
  @membership.membership_status_id = 2

  #and here we set the current_user as the owner of the group
  @group_permission = @group.group_permissions.build(params[:group_permission])
  @group_permission.membership = @membership
  @group_permission.group_role = GroupRole.find_by_name('Owner')

  unless params[:metro_area_id].blank?
    @group.metro_area  = MetroArea.find(params[:metro_area_id])
    @group.state       = (@group.metro_area && @group.metro_area.state) ? 
      @group.metro_area.state : nil
    @group.country     = @group.metro_area.country if (@group.metro_area &&  
      @group.metro_area.country)
  else
    @group.metro_area = @group.state = @group.country = nil
  end
  @group.tag_list = params[:tag_list] || ''

#   unless @user.is_in_group?(@group)
#     @user.memberships << @group
#   end
 respond_to do |format|
    if @group.save
      flash[:notice] = :group_was_successfully_created.l
      format.html { redirect_to(group_path(@group.id)) }
     else
      format.html {
        @metro_areas, @states = setup_metro_area_choices_for(@group)
        if params[:metro_area_id]
          @metro_area_id = params[:metro_area_id].to_i
          @state_id = params[:state_id].to_i
          @country_id = params[:country_id].to_i
        end
        render :action => "new"
      }
    end
  end
end
+1  A: 

Looks like either your routes are off somewhere or your model_id parameter is not what you are expecting. Might want to check to see what that parameter is being set to.

It's also really hard to give any suggestions without seeing controller code. Can you post the method making this call?

Lukas
A: 

# POST /groups # POST /groups.xml def create @group = Group.new(params[:group]) @group.category = params[:category] @group.user = current_user

#here we add the current user to the membership collection of the group
@membership = @group.memberships.build(params[:membership])
@membership.group = @group
@membership.user = current_user
@membership.initiator = false
@membership.membership_status_id = 2

#and here we set the current_user as the owner of the group
@group_permission = @group.group_permissions.build(params[:group_permission])
@group_permission.membership = @membership
@group_permission.group_role = GroupRole.find_by_name('Owner')

unless params[:metro_area_id].blank?
  @group.metro_area  = MetroArea.find(params[:metro_area_id])
  @group.state       = (@group.metro_area && @group.metro_area.state) ? @group.metro_area.state : nil
  @group.country     = @group.metro_area.country if (@group.metro_area && @group.metro_area.country)
else
  @group.metro_area = @group.state = @group.country = nil
end
@group.tag_list = params[:tag_list] || ''

unless @user.is_in_group?(@group)

@user.memberships << @group

end

respond_to do |format|
  if @group.save
    flash[:notice] = :group_was_successfully_created.l
    format.html { redirect_to(groups_path(@group.id)) }
   else
    format.html {
      @metro_areas, @states = setup_metro_area_choices_for(@group)
      if params[:metro_area_id]
        @metro_area_id = params[:metro_area_id].to_i
        @state_id = params[:state_id].to_i
        @country_id = params[:country_id].to_i
      end
      render :action => "new"
    }
  end
end

end

nml
You should edit your original question to include this code.
John Topley
I've moved the code to the quest for you. You should delete this answer.
EmFi
+1  A: 

There's a lot of superfluous code, in your controller. It still works, but you're doing a lot of things the hard way.

Your problem is this line:

format.html { redirect_to(groups_path(@group.id)) }

Which redirects to the collective groups url adding the parameter @group.id.

What it should be is

format.html { redirect_to(group_path(@group.id)) }
EmFi
Thanks - originally had the concise version, as well as the second suggestion, but both don't work, so I tried the suggestion from austinfromboston in the question http://stackoverflow.com/questions/963090/redirects-with-datamapper-railsbtw - sorry, I'm new to this site and can't find the 'delete' link to the answer with code
nml
You didn't mention datamapper or linked to the other question, so I assumed the concise version would work. I've corrected this answer for datamapper objects.
EmFi
No - datamapper isn't part of the picture, but the question I referenced demo's a similar problem (with similar url redirects). Thanks for the second suggestion to change groups_path to group_path, but this doesn't solve the problem. Appreciate your time!
nml
The question you linked to solves a Datamapper specific problem, your problem has similar symptoms but isn't the same. If changing groups_path to group_path didn't solve the issue, let us know if it changed anything and what. It could help us find your problem.
EmFi
Sorry for the confusion; I was just referencing the Datamapper problem to detail the symptoms. Changing the redirect from groups_path to group_path didn't change anything at all - same redirect problem.
nml
A: 

What is this .1 doing at the end of the line??

flash[:notice] = :group_was_successfully_created.l

I tried to run similar code in my environment and it choked on that.

It should also reference:

group_path(id)

not

groups_path(id)
MattMcKnight
The .l at the end of the error message is for Haml. Had redirect_to(@group)to start, but have been playing with different paths...
nml