2012-03-14 1 views
0

railsnoobquestion : 사용자가 레일에 객체를 저장할 수있는 기능을 개발하려고 시도 중입니다. 그런 다음 다른 객체를 만들기 위해 양식으로 다시 전달됩니다.버튼 : 하나의 객체를 만든 다음 리디렉션하여 다른 객체를 만듭니다.

두 가지 옵션을 생각해 볼 수 있습니다. - 완전히 새로운 경로를 만듭니다 - 레스토랑 게시물 개체에 데이터를 추가하고 컨트롤러에서 해당 데이터를 확인 하시겠습니까?

비슷한 기능을 가진 사람이 있습니까?

thx

답변

0

이 솔루션은 서로 숨겨진 양식 필드를 만드는 것이었다 버튼 : 내 경우, 범주에서 각각의 컨트롤러 내부

jQuery(document).ready(function() { 
    $("#submit_another").click(function() { 
    $('#create_another').attr('value','1'); 
    console.log($('#create_another').attr('value')); 
    $(".formtastic").submit(); 
    return true; 
    }); 
}); 

:

%input#create_another{:name => "create_another", :type => "hidden", :value => 0 } 
%a.btn.btn-info#submit_another 

그런 다음 JavaScript를 사용하여 양식을 제출 컨트롤러 :

if params[:create_another].nil? 
    @create_another = false 
else 
    @create_another = (params[:create_another] == "1") 
end 
respond_to do |format| 
    if @category.save 
    if @create_another 
     format.html { redirect_to new_restaurant_category_path(@restaurant), notice: I18n.t(:entry_successfully_created, :name => I18n.t(:category_singular)) } 
0

다음은 posts 리소스에 대한 표준 작성 동작입니다.

def create 
    @post = Post.new params[:post] 

    if @post.save 
    redirect_to @post, notice: 'Post was successfully created.' 
    else 
    render :new 
    end 
end 

당신이해야 할 모든

@post에서 new_post_path로 리디렉션을 변경할 수 있습니다.

redirect_to new_post_path, notice: 'Post was successfully created.'

은 당신이 찾고있는 무엇인가요? 제출

+0

감사합니다. 나는 당신과 비슷한 나의 해결책을 게시 할 것입니다. – Hendrik