2016-07-01 7 views
1

私は、ユーザーがledermann-rails-settingsのフォームでデフォルトの設定から設定を更新することを可能にしようとしています。私はわからないんだけど、私は、フォームがthis answerに基づいて構築されましたが、私は設定を更新するためにフォームを送信しようとすると、私はネストされたリソースに関連していると思わルーティングエラーが出るが、私はRoRのに新たなんです。これに関するその他の質問は、Rails 3または以前のバージョンのgemを使用しているようです。私はレール4.2.1を使用しています。ルーティングエラー

routes.rbを:

resources :users do 
    resources :settings 
end 

すくい経路:

 user_settings GET /users/:user_id/settings(.:format)   settings#index 
        POST /users/:user_id/settings(.:format)   settings#create 
    new_user_setting GET /users/:user_id/settings/new(.:format)  settings#new 
    edit_user_setting GET /users/:user_id/settings/:id/edit(.:format) settings#edit 
     user_setting GET /users/:user_id/settings/:id(.:format)  settings#show 
        PATCH /users/:user_id/settings/:id(.:format)  settings#update 
        PUT /users/:user_id/settings/:id(.:format)  settings#update 
        DELETE /users/:user_id/settings/:id(.:format)  settings#destroy 
       users GET /users(.:format)       users#index 
        POST /users(.:format)       users#create 
      new_user GET /users/new(.:format)      users#new 
      edit_user GET /users/:id/edit(.:format)     users#edit 
       user GET /users/:id(.:format)      users#show 
        PATCH /users/:id(.:format)      users#update 
        PUT /users/:id(.:format)      users#update 
        DELETE /users/:id(.:format)      users#destroy 

形態:

<%= form_for(:settings) do |form| %> 
<h3>Dashboard settings</h3> 
    <%= form.fields_for :dashboard, current_user.settings(:dashboard) do |f| %> 
     <%= f.label :theme_light, 'Light (Default)' %> 
     <%= f.radio_button :theme, "themes/flatly" %> 
     <%= f.label :theme_dark, 'Dark' %> 
     <%= f.radio_button :theme, "themes/darkly" %> 
    <% end %> 
    <%= form.submit "Save" %> 
<% end %> 

SettingsController:

class SettingsController < ApplicationController 
    def update 
    if params[:settings] 
     params[:settings].each do |key, value| 
     current_user.settings(key.to_sym).update_attributes! value 
     end 
     flash[:success] = "Settings updated!" 
     redirect_to root_path 
    else 
     render 'edit' 
    end 
    end 
end 

User.rb:

Started POST "https://stackoverflow.com/users/1/settings/1/edit" for 72.231.138.82 at 2016-07-01 15:12:36 +0000 ActionController::RoutingError (No route matches [POST] "https://stackoverflow.com/users/1/settings/1/edit")

私は、ネストされたリソースのフォームが最初にすることを意味するために、私はRailsのガイドを理解すると思う:今あるとして、次のルーティングエラーが発生します

has_settings do |s| 
    s.key :dashboard, :defaults => { :theme => 'themes/flatly' } 
end 

は、フォームを送信フォームのラインは

<%= form_for([@user, @settings]) do |form| %> 

のようなものでなければなりませんが、変更することは、エラーになります

First argument in form cannot contain nil or be empty

さらに、ledermann-rails-settingsは(少なくとも私が現在のバージョンの宝石について知る限りでは)すべての設定を呼び出す方法を持っているようには見えません。私は@settingsも定義します。

ノー運と形で異なるパスを指定しようとしただけでなく、リソースしようとしてきました:設定とリソース:routes.rbをの設定を。私は、コントローラまたはルートレベルのいずれかで何かが欠けているような気がしますが、私は宝石のドキュメントと問題がフォーム上の多くを持っていないと知っているのに十分な経験を持っていません。

答えて

1

ケースでは、ここで私はこの作業を得た方法ですが、他の誰かを助けます。

<%= form_for(:settings, url: user_setting_path, html: { method: :put }) do |form| %> 

SettingsController

def update 
    if setting_params 
    setting_params.each do |key, value| 
     @current_user.settings(key.to_sym).update_attributes! value 
    end 
    flash[:success] = "Settings updated!" 
    redirect_to request.referrer 
    else 
    render 'edit' 
    end 
end 

def setting_params 
    params.require(:settings).permit(dashboard: :theme) 
end 
+0

私をたくさん助けました。ありがとう! –