渲染Ruby on Rails后保持参数 [英] Keep params after render Ruby on Rails
问题描述
我有一个属于User的项目.在我的用户视图中,我有一个添加新项目的链接,其中包含要向其添加项目的用户的参数.
I have a Project that belongs to User. In my user view I have a link to add a new project, with the parameter for the user I want to add the project to.
<%= link_to 'Add new project', :controller => "project", :action => "new", :id => @user %>
Url: /projects/new?id=62
向用户添加项目是可行的.问题是当添加新项目时验证失败时,我将进行渲染.
Adding a project to a user works. The problem is when the validation fails while adding a new project and I do a render.
def create
@project = Project.new(params[:project])
if @project.save
redirect_to :action => "show", :id => @project.id
else
render :action => "new"
end
end
视图:
<%= form_for @project do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.hidden_field :user_id , :value => params[:id] %>
<%= f.submit "Create project" %>
<% end %>
路线
resources :users do
resources :projects
end
如何在渲染后为用户保留参数?还是有更好的方法来做到这一点?一直在看很多类似的问题,但无法解决.
How can I keep the parameter for the user after the render? Or is there some better way to do this? Been looking at a lot of similar questions but can't get it to work.
推荐答案
尝试
render :action => "new", :id => @project.id
如果它不适用于您,请尝试将参数传递给渲染操作的另一种方法.
if its not works for you, then try alternate way to pass the parameter to your render action.
这也可以帮助您-> Rails 3 Render =>参数新功能
This can also help you-> Rails 3 Render => New with Parameter
这篇关于渲染Ruby on Rails后保持参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!