创建问题后导轨重定向

时间:2009-11-23 21:15:30

标签: ruby-on-rails redirect

任何人都可以帮助解决这个问题:

在“创建”时,用户被重定向到url:model / model_id(例如post / 1),而我被重定向到models / url_encoding_object(例如posts /.% 23)并且有一个“406 Not Acceptable” “控制台中的消息。

通常,在创建时,控制台的消息是“Processing PostsController #create(for 000.0.0.0 at 2009-11-23 12:32:52)[POST]”,但是出现此错误,消息为“Processing PostsController” #create to#(000.0.0.0 at 2009-11-23 12:32:52)[POST]“

我见过austinfromboston的回应并尝试了他的“老式但有效”的解决方案来解决这个类似的问题,但它对我不起作用。

非常感谢任何帮助

控制器代码:

# 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

4 个答案:

答案 0 :(得分:1)

看起来您的路线在某处或您的model_id参数不是您所期望的。可能想检查一下该参数的设置。

如果不看控制器代码,也很难提出任何建议。你可以发布这个电话的方法吗?

答案 1 :(得分:1)

你的控制器里有很多多余的代码。它仍然有效,但你很难做很多事情。

你的问题就在这一行:

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

哪个重定向到集体组网址添加参数@ group.id。

应该是什么

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

答案 2 :(得分:0)

#POST / groups   #POST /groups.xml   def创建     @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] || ''

除非@ user.is_in_group?(@ group)

@ user.memberships&lt;&lt; @group

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

答案 3 :(得分:0)

这是什么.1在行尾做什么?

flash[:notice] = :group_was_successfully_created.l

我试图在我的环境中运行类似的代码并且因此而烦恼。

它还应该引用:

group_path(id)

不是

groups_path(id)