2016-10-02 17 views
-4

我讀軌軌指導 - http://guides.rubyonrails.org/ 我無法理解的語法:redirect_to @article裏面的方法:的Ruby on傳遞對象命名路由

def create 
    @article = Article.new(article_params) 
    if @article.save 
    redirect_to @article 
    else 
    render 'new' 
    end 
end 

而且我不能夠理解語法 - url: article_path(@article) 形式

<%= form_for :article, url: article_path(@article), method: :patch do 
+2

這裏有一個問題嗎? –

回答

0

這裏我們創建使用創建方法的條款的認定中的內部。該塊首先使用從表單發送的article_params創建一篇文章,如果新創建的文章保存到數據庫,則我們被重定向到文章本身,否則我們被重定向到顯示錯誤的表單。

0

當您創建的RoR平臺的任何物品,然後理想教程This指南,當您使用Rails的形式,那麼就不需要提方法,因爲軌道的形式內置了一些方法,像getpostput/patch & destroy他自動明白什麼樣的您的要求只需按照thisthis

def create 
@article = Article.new(article_params) 

@article.save 
redirect_to @article 
end 

private 
def article_params 
    params.require(:article).permit(:title, :text) #-> passing article parameters 
end