I've been watching and reproducing these railscasts on my app: 196-nested-model-form-part-1 and 197-nested-model-form-part-2. I do not yet have a pro account so i can't watch the revised episode.
我一直在观察并复制我的app上的这些railscast: 196-nested-model- part-1和197-nested-model-form-part-2。我还没有一个专业账号,所以我不能看修改后的那一集。
I'm developing under rails4 (edge) and link_to_function
has been deprecated in favor of unobstrusive JS (which is great).
我是在rails4 (edge)下开发的,link_to_function已经被弃用,代之以不引人注目的JS(非常棒)。
I'll keep the example of the above railscasts (i.e. survey/question). What i'd like to do is to use the question's partial through unobstrusive javascript and i just don't know how and where i should do this.
我将以上述铁路运输为例(即调查/问题)。我想要做的是通过不引人注目的javascript使用问题的部分,我不知道该怎么做,在哪里做。
I was thinking of two ways to do so :
我想到了两种方法:
- First way would be to add in my
app/assets/javascripts
a filesurveys.js
with the functionadd_question
but i don't see how i could use my partial from here. - 第一种方法是在我的应用/资产/javascript中添加一个文件调查。带有add_question函数的js但是我不知道如何使用这里的偏导。
- Other way would be to create a new action in my
SurveyController
that would respond using the question partial but i'm bothered by the fact of having an action specific to questions in my survey controller. - 另一种方法是在我的SurveyController中创建一个新的操作,它将使用问题部分进行响应,但我对我的调查控制器中有一个特定于问题的操作感到困惑。
I can't help to think there must be a better way to do this.
我不禁认为一定有更好的办法来做这件事。
2 个解决方案
#1
9
I don't have a pro account on Railscasts either, but sometimes it is a good idea to have a look at Ryan's Github account. Oftentimes he develops gems to stuff he covered in his episodes. There you will find this awesome nested_form gem, which does exactly what you want.
我也没有铁路广播的专业账户,但是有时候看看Ryan的Github账户是个不错的主意。通常,他会在他所经历的事情中发现宝石。在那里,你会发现这个很棒的nested_form gem,它完全满足你的需求。
Of course you can reinvent the wheel, but I think it is much easier and faster to use Ryan's gem. The documentation explains perfectly how to use it. Have fun!
当然你可以重新发明*,但是我认为使用Ryan的宝石会更容易更快。文档完美地解释了如何使用它。玩得开心!
#2
18
Did you know about remote links and forms? You can use a remote link here to accomplish what you want.
你知道远程链接和表单吗?您可以在这里使用远程链接来完成您想要的。
In your view:
在你的观点:
link_to 'Add question', add_question_path(@survey), remote: true
In your controller
在你的控制器
def add_question
@survey = Survey.find(params[:id])
respond_to do |format|
format.js #add_question.js.erb
end
end
The last step is to create a file app/views/surveys/add_question.js.erb
最后一步是创建一个文件应用程序/视图/调查/add_question.js.erb
$('#my_survey').append('<%=j render partial: 'my_question_partial' %>')
Don't forget to create a route for your ask_question_path
不要忘记为ask_question_path创建路径
For more info about remote links and forms see: http://tech.thereq.com/post/17243732577/rails-3-using-link-to-remote-true-with-jquery-ujs
有关远程链接和表单的更多信息,请参见:http://tech.thereq.com/post/17243732577/rails-3-using-link to remote-true-with-jquery-ujs
#1
9
I don't have a pro account on Railscasts either, but sometimes it is a good idea to have a look at Ryan's Github account. Oftentimes he develops gems to stuff he covered in his episodes. There you will find this awesome nested_form gem, which does exactly what you want.
我也没有铁路广播的专业账户,但是有时候看看Ryan的Github账户是个不错的主意。通常,他会在他所经历的事情中发现宝石。在那里,你会发现这个很棒的nested_form gem,它完全满足你的需求。
Of course you can reinvent the wheel, but I think it is much easier and faster to use Ryan's gem. The documentation explains perfectly how to use it. Have fun!
当然你可以重新发明*,但是我认为使用Ryan的宝石会更容易更快。文档完美地解释了如何使用它。玩得开心!
#2
18
Did you know about remote links and forms? You can use a remote link here to accomplish what you want.
你知道远程链接和表单吗?您可以在这里使用远程链接来完成您想要的。
In your view:
在你的观点:
link_to 'Add question', add_question_path(@survey), remote: true
In your controller
在你的控制器
def add_question
@survey = Survey.find(params[:id])
respond_to do |format|
format.js #add_question.js.erb
end
end
The last step is to create a file app/views/surveys/add_question.js.erb
最后一步是创建一个文件应用程序/视图/调查/add_question.js.erb
$('#my_survey').append('<%=j render partial: 'my_question_partial' %>')
Don't forget to create a route for your ask_question_path
不要忘记为ask_question_path创建路径
For more info about remote links and forms see: http://tech.thereq.com/post/17243732577/rails-3-using-link-to-remote-true-with-jquery-ujs
有关远程链接和表单的更多信息,请参见:http://tech.thereq.com/post/17243732577/rails-3-using-link to remote-true-with-jquery-ujs