I have a method called "createpost" in "topics_controller" which I am trying access from the show inside "topics_controller" but I keep getting a route error.
The Form:
<%= form_for #community_post, :url => { :action => "createpost", :controller=> "community_topics" } do |f| %>
<%= render 'error_messages' %>
<%= f.label :text %>
<%= f.text_area :text %>
<%= hidden_field_tag :community_topic_id, #community_topic.id %>
<br>
<%= f.submit "Submit reply" %>
The Controller Action:
def createpost
#community_post = CommunityPost.new(community_post_params)
#community_post.user_id = current_user.id
#community_post.community_topic_id = params[:community_topic_id]
if #community_post.save
redirect_to "/community_topics/#{#community_post.community_topic_id}", notice: 'Community post was successfully created.'
else
render action: 'new'
end
end
What am I doing wrong so I can correct it? Thanks a bunch.
You should have in your routes something like:
resources :comunity_topics do
post 'createpost', action: 'createpost'
end
and the form route should be something like:
createpost_comunity_topics_path
Either rename the method createpost in controller to create and remove the url option for form_for in view. Or define the route, if you really want use the createpost action like this:
resources :community_posts do
collection do
post :createpost
end
end
But I suspect that you are following some old tutorial from the times when Rails had routes that contained the name of controller and action to be executed. This approach was abandoned in favor of RESTful routes.
Read the current docs here:
http://guides.rubyonrails.org/routing.html#resource-routing-the-rails-default
Related
I have a form for creating new comments. This code exists in a page that is under a different controller (let's say it's app/views/posts/show.html.erb).
<%= form_for Comment.new do |f| %>
<%= f.label :content %>
<%= f.text_field :content %><br/>
<%= f.submit %>
<% end %>
The form works if I have Comment.new like above, but I want to use an instance variable like form_for #comment, similar to the first code snippet in this link: https://api.rubyonrails.org/v5.2.3/classes/ActionView/Helpers/FormHelper.html
In order to do so, I thought I need to define a new function like this and assign an empty comment. I tried putting this code in both the posts_controller and comments_controller.
def new
#comment = Comment.new
end
But when I replace Comment.new with #comment, I get this error: ActionView::Template::Error (First argument in form cannot contain nil or be empty):
This leads me to believe that neither of the new methods are being called. What am I doing wrong here?
My routes.rb looks like this:
Rails.application.routes.draw do
root to: 'posts#show'
resources :messages
end
if you are using show page (app/views/posts/show.html.erb) to display form
add this line in the show action of posts controller
# posts_controller
def show
#comment = Comment.new
end
and if you also want to submit your form other than the comment's create action mention the url in form_for tag
<%= form_for #comment, url: posts_path do |f| %>
<%= f.label :content %>
<%= f.text_field :content %><br/>
<%= f.submit %>
<% end %>
I have a form in Rails
<div class="page-header">
<h3>Create Blah</h3>
</div>
<%= simple_form_for #blah do |f| %>
<%= f.input :id %>
<%= f.input :name %>
<%= f.input :pho %>
<%= f.input :fun %>
<%= f.submit :class => 'btn btn-primary' %>
<% end %>
<br>
When I click the submit button, where does the code attempt to go? Does it call the create method for blah_controller.rb? Because currently, I get a routing error
Routing Error
uninitialized constant BlahsController
Here is the BlahController#create method:
def create
authorize! :create, :blahs
#blah = Blah.new(params[:blah])
if #blah.save
redirect_to admin_blah_path(#blah), :notice => 'New blah created!'
else
render :new
end
end
In my rake routes, I have
admin_blahs GET /admin/blahs(.:format) admin/blahs#index
POST /admin/blahs(.:format) admin/blahs#create
new_admin_blah GET /admin/blahs/new(.:format) admin/blahs#new
edit_admin_blah GET /admin/blahs/:id/edit(.:format) admin/blahs#edit
admin_blah GET /admin/blahs/:id(.:format) admin/blahs#show
PUT /admin/blahs/:id(.:format) admin/blahs#update
DELETE /admin/blahs/:id(.:format) admin/blahs#destroy
It looks like your BlahsController is a namespaced controller, living under the Admin module (i.e., its fully-qualified name is Admin::BlahsController). If so, when constructing forms you must also provide the :admin namespace, using something like the following:
<%= simple_form_for [:admin, #blah] do |f| %>
See the Rails Guide to Form Helpers, under the "Dealing with Namespaces" section.
I am trying to set up a form in a Rails view to submit an id back to the show method in the controller. My form uses autocomplete to set the hidden id field:
<%= form_tag students_path, id: 'search_student_name', method: :get do %>
<%= text_field_tag :search_name, '', size: 30 %>
<%= hidden_field_tag :id %>
<%= submit_tag "Search", name: nil %>
<% end %>
I'm using the standard controller 'show' method generated by the scaffold:
# GET /students/1
# GET /students/1.json
def show
#student = student_scope.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #student }
end
end
I'd be grateful for any advice on the combination of form url / post method / additional routes to get this to work. Is this the way you'd normally do this is Rails, or should I set up a new controller method to handle the submitted form?
Thanks in advance
Because it is not exactly a Restful show, you should create a new action, search.
#routes.rb
post 'search' => 'students#search'
#student_controller.rb
def search
#student = Student.find_by_name params[:search_name]
render action: 'show'
end
The form doesn't need to send the :id as far as I can tell.
<%= form_tag search_path, method: :get do %>
<%= input_tag :search_name, type: 'text' %>
<%= submit_tag "Search" %>
<% end %>
I have a template "groups/show.html.erb" I have link that renders a partial by passing a parameter to the controller. The controller then uses the parameter to identify which JS call to make.
<%= link_to 'Add a Video', group_path(create_video: true), remote: true %>
then in the controller
elsif params[:create_video]
#video = Group.find(params[:id]).videos.build
respond_to do |format|
format.js {render action: 'create_video'}
end
This brings up a partial with a form that creates a video using the create method in the videos_controller. If a validation on the form fails and I try to render "groups/show" I get a routing error:
_create_video.html.erb
<%= form_for #video, :url => group_videos_path(#group) do |f| %>
<%= f.text_field :name %>
<%= f.submit %>
<% end %>
No route matches {:action=>"show", :controller=>"groups", :create_video=>true}
To make this work I think you can just explicitly match it in routes.rb but is there a better way to do it? Thanks a bunch
You are forgetting to give which group, you should normally do something like
group_path(#group, create_video: true)
hope this helps.
I decided to start a little project in rails 3 and I am a little bit stuck on a form... Where can I specified the f.submit action should go to a special controller / action ?
The code in the form is:
<%= form_for #user, :url => { :action => "login" } do |f| %>
<div class="field">
<%= f.text_field :email %><br />
<%= f.text_field :password %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
User is defined as #user = User.new in "index" method of "home_controller".
but I have the error:
No route matches {:controller=>"home", :action=>"login"}
as soon as I run http://0.0.0.0:3000
I am very sorry for this newbee question but I cannot find the routing details (I worked a little bit with rails a couple of years ago but...)
Thanks,
Luc
You don't need to specify any action for f.sumbit.
First of all, you need to make sure you put
resources :users
(for example)
in your routes.rb
then if you want to create a user
put
def new
#user = User.new
end
in your users_controller so you have a page to create new user
or you can put #user=User.new anywhere you like, remember to set
the route correctly
then
def create
#user = User.new(params[:id])
if #user.save
sign_in #user
redirect_to #user
else
render 'new'
end
end
is the part that does real work after you hit on submit
the actual part that connect your form with everything else is this line
<% form_for #user do |f| %>
you can change user to other object, and you can also edit form using update action in a controller.
Hope you got the idea
Whenever you use REST objects, the mere:
form_for #article
is enough for the form to find the proper path.
Otherwise, you can use helpers this way:
form_tag(:controller => "people", :action => "search", :method => "get", :class => "nifty_form")
More info here: http://edgeguides.rubyonrails.org/form_helpers.html