路由是使用ID有action并且action在提交表单后有ID

发布于 2024-09-01 10:18:21 字数 1141 浏览 4 评论 0原文

有一个模型用户 has_one user_profile 和 User_Profile属于用户

我在用户控制器中

  def personal
    @user = User.find_by_id(params[:id])
    @user_profile = @user.user_profile
    @user_profile ||= @user.build_user_profile
  end

  def update_personal
    @user = User.find_by_id(params[:id])
    if @user.user_profile.update_attributes(params[:user_profile])
      flash[:notice] = "OK"
      redirect_to @user
    else
      flash[:notice] = "Fail"
      render :action => 'update_personal'
    end
  end

我有:在我的personal.html.erb视图中我有:

<% semantic_form_for @user_profile, :url => { :action => "update_personal"} do |form| %>
  <%= form.inputs %>
  <%=  form.buttons %>
<%end%>

在轮盘上我有:

map.resources :users, :member => {
         :personal            => :get,
         :update_personal     => :put
        }

现在奇怪的是我可以做:

users/1/personal 

看看但当我提交时,我收到此错误:

Unknown action

No action responded to 1.

它正在尝试查找名为 1 的操作。

任何人都可以指出我正确的方向吗?

I have a model User that has_one user_profile and a User_Profile belongs_to user

in the User controller I have:

  def personal
    @user = User.find_by_id(params[:id])
    @user_profile = @user.user_profile
    @user_profile ||= @user.build_user_profile
  end

  def update_personal
    @user = User.find_by_id(params[:id])
    if @user.user_profile.update_attributes(params[:user_profile])
      flash[:notice] = "OK"
      redirect_to @user
    else
      flash[:notice] = "Fail"
      render :action => 'update_personal'
    end
  end

In my personal.html.erb view I have:

<% semantic_form_for @user_profile, :url => { :action => "update_personal"} do |form| %>
  <%= form.inputs %>
  <%=  form.buttons %>
<%end%>

And on the rountes I have:

map.resources :users, :member => {
         :personal            => :get,
         :update_personal     => :put
        }

Now the strange thing is that I can do:

users/1/personal 

to see the form but when I submit I get this error:

Unknown action

No action responded to 1.

It's trying to find an action with the name 1.

Can anyone point me out on the right direction?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

违心° 2024-09-08 10:18:21

刚刚又遇到了这个问题,终于明白了问题所在,找到了解决办法。

这次我使用 getJason 进行 ajax 调用。
因为调用是一个 get,并且在我的路线中我有一个 update_xxxxxx => :放,
路由被忽略并使用默认的 :controller/:action/:id 。

我只需要把 update_xxxx =>; :get ,问题就解决了。

也许这会对某人有所帮助。

I just got the problem again, and finally understood the problem and found the solution.

This time i was using an ajax call using getJason.
Because the call was a get, and in my routes I had a update_xxxxxx => :put,
the route was ignored and the default :controller/:action/:id was used.

I just had to put the update_xxxx => :get and the problem was solved.

Maybe this will help someone.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文