Reputation: 301
Ok I have searched the other Stackoverflow questions available, tried as best I can to decipher this problem, but I am at an impass. Im a PHP developer but I am just learning Ruby on Rails (Rails 4).
What I want to try achieve: - POST a form (using form_for) that is residing in a view index.html.erb file residing under the family of "Blog" (so the controller is also called Blog) - But I want that form to POST to a completely different controller, not the Blog controller, but a controller called Feature (specifically, to the create action in the controller called Feature)
I know I am supposed to use the :url component in form_for but I am certain the syntax I am typing out is wrong OR something else somewhere else is causing the error.
Forgive my newbie cluelessness at this I am just starting out in Rails
The error message I am getting currently:
ArgumentError in Blog#index wrong number of arguments (3 for 1..2) Extracted source (around line #3):
<h1>Blog main page, welcome</h1>
<% form_for :feature, @feature, :url => { :action => "create" } do |f| %>
<p>
<%= f.label :subject %><br>
<%= f.text_field :string %>
The code I have:
Index.html.erb (sitting under the Blog family)
<h1>Blog main page, welcome</h1>
<% form_for :feature, @feature, :url => { :action => "create" } do |f| %>
<p>
<%= f.label :subject %><br>
<%= f.text_field :string %>
</p>
<p>
<%= f.label :author %><br>
<%= f.text_area :string %>
</p>
<p>
<%= f.label :title %><br>
<%= f.text_area :string %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
<br>
<%= link_to "My Blog", new_blog_path %>
The code I have for the Feature controller
class FeatureController < ApplicationController
def index
end
def new
end
def show
end
def create
@feature = Blog.new (feature_params)
render text: params[:feature].inspect
end
private
def feature_params
params.require(:feature).permit(:title, :text, :text2, :text3)
end
end
end
The code I have sitting in my routes config
MyApp::Application.routes.draw do
root 'blog#index'
resources :blog
resources :admin
resources :feature
end
My database schema looks like this:
create_table "blogs", force: true do |t|
t.string "subject"
t.string "title"
t.string "author"
t.text "text"
t.datetime "created_at"
t.datetime "updated_at"
I have no clue what I am doing wrong :( If anyone is able to help me out I would be immensely appreciative
Upvotes: 2
Views: 91
Reputation: 9190
After amending the routes to use plural format, the solution is:
<%= form_for :features, :url => features_path do |f| %>
features_path
being a path helper method for '/features'
Upvotes: 2
Reputation: 33542
You are missing an =
sing in your form_for
tag.I suppose this caused the error.
It should be like this
<%= form_for :feature, @feature, :url => { :action => "create" } do |f| %>
Upvotes: 0