rails在销毁操作后将用户重定向到配置文件页面

时间:2017-10-18 19:45:29

标签: ruby-on-rails ruby-on-rails-4

我有一份work_histories表格。当用户删除工作历史记录时,我希望将它们重定向到其用户配置文件页面,而不是调用此控制器操作的user_work_histories_url(work_histories #show)。我想重定向到用户个人资料页面,即user_path(@user)。

我尝试将work_histories destroy操作更改为重定向到user_path(@user)而不是user_work_histories_url但是它说明了我刚删除的作业的ID无法找到。

这是我的work_histories控制器

# DELETE /users/:user_id/work_histories/:id
def destroy
@work_history = WorkHistory.find(params[:id])

@work_history.destroy
respond_to do |format|
  format.html { redirect_to user_work_histories_url, 
                notice: "The work history for your #{@work_history.job_title.upcase} job was successfully destroyed." }
  format.json { head :no_content }
end
end

这是show_histories的show.html.erb

<p id="notice"><%= notice %></p>

<p>
  <strong>Job Title:</strong>
  <%= @work_history.job_title %>
</p>

<p>
  <strong>Job Description:</strong>
  <%= @work_history.description %>
</p>

<%= link_to 'Back', user_work_histories_path(@user) %> |
<%= link_to 'Edit', edit_user_work_history_path(@user, @work_history) %> | 
<%= link_to 'Delete', user_work_history_path(@user, @work_history), method: :delete, 
    data: { confirm: 'Are you sure you want to delete this work history?' } %>

1 个答案:

答案 0 :(得分:0)

想想我弄清楚了。我不确定这是否是这种做法的“轨道方式”,或者它只是我的另一个黑客攻击解决方案。

我在work_history控制器中尝试调用user_url(@user),这是我在用户控制器中执行的操作,但在我将其更改为user_url(@ work_history.user)时看起来是有效的。工作历史属于用户。它现在有效......

# DELETE /users/:user_id/work_histories/:id
def destroy
@work_history = WorkHistory.find(params[:id])

@work_history.destroy
respond_to do |format|
  format.html { redirect_to user_url(@work_history.user), 
                notice: "The work history for your #{@work_history.job_title.upcase} job was successfully destroyed." }
  format.json { head :no_content }
end