nml
nml

Reputation: 11

rails redirect after create problem

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

Upvotes: 1

Views: 3221

Answers (4)

MattMcKnight
MattMcKnight

Reputation: 8290

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)

Upvotes: 0

EmFi
EmFi

Reputation: 23450

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)) }

Upvotes: 1

nml
nml

Reputation: 11

# 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

Upvotes: 0

Lukas
Lukas

Reputation: 3245

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?

Upvotes: 1

Related Questions