I have 2 models, blogs and posts
resources :blogs do
resources :posts
end
So, also i have an association. One blog can have a lot of posts. So I put the link to new post in the index of blog:
= link_to 'New Post', new_blog_post_path(#blog)
And then it redirects you to new post, which renders a form like this:
= simple_form_for(#post) do |f|
= f.error_notification
.form-inputs
= f.input :title
= f.input :content
.form-actions
= f.button :submit
And I'm getting an error:
undefined method `posts_path'
I think simple form requires the blog_id but I could't find it. I tried to put #blog.id to simple form, but anyway i got an error (id for nil class)
How can I solve my problem?
I've never used simple_form_for, but it looks to me like your resource has association that is assigned. So you'll need to include it in the form definition:
= simple_form_for([#blog, #post]) do |f|
= f.error_notification
.form-inputs
= f.input :title
= f.input :content
.form-actions
= f.button :submit
And obviously you'll need to have #blog available in your controller action.
Link to the form_for docs:
http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for
EDIT:
Not sure where posts_path is coming from in your code, but obviously that route doesn't exist as all post paths are dependent on blogs.
Is there anywhere where you reference a post_path in your code?
Related
I am learning Rails and making a reddit clone to help me understand how routing works.
Each post has subreddit. I have the following in my routes file:
resources :subreddits do
resources :posts
end
This way I can create new posts with the following url:
/subreddits/:subreddit_id/posts/new(.:format)
Example:
http://localhost:3000/subreddits/1/posts/new
However I still need to specify in the new posts form what subreddit I want to post to belong to. Wheras what I want is a hidden field that sets the subreddit id to the correct one. The correct one being the one given in the URL.
This is what I currently have:
=simple_form_for #post, html: {multipart: true} do |f|
-if #post.errors.any?
#errors
%h2
=pluralize(#post.errors.count, "error")
prevented this pin from saving
%ul
-#post.errors.full_messages.each do |msg|
%li= msg
.form-group
=f.input :title, input_html: {class: 'form-control'}
.form-group
=f.input :content, input_html: {class: 'form-control'}
=f.hidden_field :subreddit_id, :value => #post.subreddit_id
=f.button :submit, class: "btn btn-primary"
I get the following error:
No route matches {:action=>"show", :controller=>"posts", :id=>33, :subreddit_id=>nil} missing required keys: [:subreddit_id]
I think this is because I am trying to access the subreddit id of a post that hasn't been created yet. How do I solve this? Am I going about it in the wrong direction?
In the new action of the posts_controller, you'll set the #subreddit instance variable
def new
#subreddit = Subreddit.find(params[:subreddit_id])
#post = Post.new
end
In the form, you'll need to change the first line
= simple_form_for [#subreddit, #post], html: {multipart: true} do |f|
:subreddit_id will now be in the url
Try this and check if it works
= f.input :title, :as => :hidden, :input_html => { :value => "some value" }
I am running into an error with a form I am trying to create:
ActionView::Template::Error (undefined method `contact_forms_path' for #<#<Class
The thing is I never created a contact_forms route, so I do not know why I am getting an undefined method for the contact_forms_path.
My route for the contact form is:
get "/contact_form/new", to: "contact_form#new"
My view for this form is new.html.erb within my contact_form directory
<%= simple_form_for #contact_form do |f| %>
<%= f.input :first_name %>
<%= f.input :last_name %>
<%= f.input :email, placeholder: 'example#email.com' %>
<%= f.input :address %>
<%= f.input :city %>
<%= f.input :state %>
<%= f.input :zip_code %>
<%= f.input :phone %>
<%= f.button :submit %>
<% end %>
My model is contact_form.rb and my controller is contact_form_controller.rb.
I could use a little direction. Any help is appreciated. I can pass along more info if needed.
Thanks!
Please try this:
add this to your routes:
post "/contact_form", to: "contact_form#create"
open up the terminal and run:
rake routes|grep contact_form
You should get something like this as a response:
contact_form_new GET /contact_form/new(.:format) contact_form#new
contact_form POST /contact_form(.:format) contact_form#create
This gives you the path for the route. Now you can specify that path in the simple_form_for:
<%= simple_form_for #contact_form, url: contact_form_path, method: :post do |f| %>
form_for assumes a default url for new Foo records as foos_path as explained here...
http://apidock.com/rails/ActionView/Helpers/FormHelper/form_for
You need to specify the path that the contact_form will post to...
post "/contact_forms", to: "contact_forms#create", as: 'contact_forms"
That will be the route that receives the params when you submit the form.
Note that as a shorthand you could just specify in your routes.rb
resources :contact_forms, only: [:new, :create]
(This assumes that you will use the more conventional contact_forms#new ... convention-over-configuration is to use plural for controller names)
I've got the following simple_form:
= simple_form_for instance do |f|
= f.input :update_resolution, collection: 1..10
= f.button :submit
It throws the error:
undefined method `update_resolution' for #<Instance:0x007f0c07329640>
In instances_controller.rb I have:
def update_resolution
render nothing: true, status: 200, content_type: 'text/html'
end
And I'm not 100% sure what's best to put in routes.rb.
Goal: I'm trying make an auto-submitting dropdown to allow the user to run update_resolution with certain params.
Questions:
Why does it throw this error & how can I fix it?
What is the preferred routes.rb strategy?
This can be achieved with a model less simple_form. Look at the following code:
<%= simple_form_for :user, url: users_path do |f| %>
<%= f.input :name, as: :string %>
...
<% end %>
url: users_path can be changed to the action you want it to post to. In your case update_resolution_instance_path The action will take the params being passed to it and do whatever it needs to do.
I am using something similar to be able to take data from fields and then send an email to the user entered email address.
I have a page where I may add n number of questions. And my questions partial contains a form as follows:
= form_for :question, :url => questions_path do |f|
= f.text_field :title
What I get in params is "question"=>{"title"=>"Some Name"}. But, if I click Add New Question button the same partial is rendered below. My problem is I still get the first questions params even if I have 2 forms now. Is there a way to get params like
"question"=>{["title"=>"Some Name"], ["title"=>"Some Other Name"]}
try this
= form_for :question, :url => questions_path do |f|
= f.text_field :title, name: "question[title][]"
for more info refer http://guides.rubyonrails.org/action_controller_overview.html#hash-and-array-parameters
I'm getting some funkiness that is absolutely confounding me with Rails 3. I can't seem to get the routing to generate the proper path using the (mostly) standard _form style of the scaffold.
First off, I'm doing everything within an "admin" namespace. I'm finding that the form partial throws a routing error if I use admin_team_path(#team) to generate the path when creating a new Team, but then submitting the form when editing, it throws an error unless I use admin_teams_path.
admin_team_path(#team) where #team = Team.new throws this error:
No route matches {:controller=>"admin/teams", :action=>"show", :id=>#}
Meanwhile...
admin_teams_path(#team) where #team = throws this error:
The action 'edit' could not be found for TeamsController
In the latter case, it seems to be directing to the URL: http://localhost:3000/teams/1/edit - it's not recognizing the namespace properly.
Here's my full _form.html:
<%= semantic_form_for(#team, :url => admin_teams_path(#team)) do |f| %>
<%= f.semantic_errors %>
<%= f.inputs do %>
<%= f.input :user_id %>
<%= f.input :league_id %>
<%= f.input :name %>
<% end %>
<%= f.buttons do %>
<%= f.commit_button :button_html =>{:class => "primary"} %>
<% end %>
<% end %>
What gives? What's the right way to create this form partial so it works for both new and edit actions?
Namespaces seem to be such a mess to work with.
Presuming you have defined your routes in a RESOURCEful manner, like so:
namespace :admin do
resources :teams
end
Then, in your _form partial you can let rails take care of the action like so:
<%= semantic_form_for(["admin", #team]) do |f| %>
.... #rest of the code
<% end %>