使用自定义 url 在新页面上呈现错误,例如 http://localhost:3000/education_informations/new?user_id=10

Render error on new page with customize url like http://localhost:3000/education_informations/new?user_id=10

我正在尝试使用新操作 http://localhost:3000/education_informations/new?user_id=10 and also set form validation on it. when form submit failed so its display error but url is changes like http://localhost:3000/education_informations 传递查询参数,所以我想同样自定义 url 并使用 user_id 传递查询参数。请帮助我

控制器代码如下

def create
    @edu_info = EducationInformations.new(educational_params)

        if @edu_info.save
            #flash[:notice] = 'Vote saved.'
            redirect_to @edu_info
        else
            render 'new'
        end     

end

您可以使用带有 url 参数的 redirect_to

在您的 #create 操作中,您将拥有当前的 user_id 参数:

user_id = params[:user_id]

如果您想 redirect_to @edu_info 并保留参数,您可以通过 redirect_to 方法上的选项散列设置它们:

redirect_to @edu_info(user_id: user_id)

你觉得这有意义吗?

我假设教育信息将始终呈现给用户。所以要创建你应该做的路线:

在路由文件中:

resources :users do
  resources :education_informations
end

所以这将做的是创建我在评论中指定的路线。像这样将是路线:

user_education_informations     GET    /users/:user_id/education_informations(.:format)          education_informations#index
                                POST   /users/:user_id/education_informations(.:format)          education_informations#create
 new_user_education_information GET    /users/:user_id/education_informations/new(.:format)      education_informations#new
edit_user_education_information GET    /users/:user_id/education_informations/:id/edit(.:format) education_informations#edit
     user_education_information GET    /users/:user_id/education_informations/:id(.:format)      education_informations#show
                                PATCH  /users/:user_id/education_informations/:id(.:format)      education_informations#update
                                PUT    /users/:user_id/education_informations/:id(.:format)      education_informations#update
                                DELETE /users/:user_id/education_informations/:id(.:format)      education_informations#destroy
                          users GET    /users(.:format)                                          users#index
                                POST   /users(.:format)                                          users#create
                       new_user GET    /users/new(.:format)                                      users#new
                      edit_user GET    /users/:id/edit(.:format)                                 users#edit
                           user GET    /users/:id(.:format)                                      users#show
                                PATCH  /users/:id(.:format)                                      users#update
                                PUT    /users/:id(.:format)                                      users#update
                                DELETE /users/:id(.:format)                                      users#destroy

因此,对于新的教育信息表,您需要提供此路径 new_user_education_information_path(user),其中 user 将是被点击的用户。

您可以在此处获取更多信息:http://guides.rubyonrails.org/routing.html#nested-resources

希望这对您有所帮助。

您可以像下面那样将 参数 传递给 form_for 以将 user_id 保留在 URL 即使在 表单提交失败后

<%= form_for @edu_info, :url => { :action => :create, :user_id => @user.id } %>

<%= form_for @edu_info, :url => { :action => :create, :user_id => current_user.id } %>

如果@user没有初始化。

Source