Get Rails to hit controller with an input tag submit - ruby

View:
%input{type: 'submit', action: 'home#create_user'}
Controller:
class HomeController < ApplicationController
def index
render 'home/index'
end
def sign_up
render 'home/sign_up'
end
def create_user
render 'dashboard/dashboard'
end
end
routes.rb
post 'home/create_user' => 'home#create_user', :as => :create_user
Why is this button not hitting the controller?

By itself, an submit tag isn't going to generate the form. I tried your code above and the button doesn't do anything. Unless I'm forgetting INPUT doesn't have an ACTION attribute.
If you had a link to that page, the reason it's not working is because by default that link will be a GET request and you've restricted the route to POST.
So, either wrap it up in a form or use button_to or the :method => :post solutions to make it POST the request and it should work.
Something like this:
= button_to 'click me', create_user_path
= link_to 'click me', create_user_path, method: 'post'

Related

Preview Post before save in active admin

I am looking for some pointers on getting started with this feature; I would like a user to be able to preview what their post would look like if saved
ActiveAdmin.register Post do
permit_params :comments, :title, :category_id, :slug, :published
# Create Blog Post
form do |f|
inputs 'Blog' do
f.semantic_errors
f.input :title
f.input :category_id, as: :select, collection: Category.all
f.input :comments, as: :text, input_html: { rows: 10, cols: 10 }
f.input :published, as: :boolean
end
inputs 'Submit' do
f.actions
end
end
end
So looking at the active admin documentation I can see you can add custom action items
action_item :preview, only: :new, name: 'preview_button' do
# do stuff here
end
I can also add a custom controller action in the form of a collection_action which will also add a route for me
collection_action :preview, method: :post do
#post = Post.new(params[:post])
end
So now I have a custom action preview and a button that can pass data to the method (I have hard coded it for now, as unsure how to get the data from outside the form).
This is what I have so far:
action_item :preview, only: :new, name: 'preview_button' do
link_to 'Preview', preview_my_admin_panel_posts_path(post: { title: 'Test Title', comments: 'test comments', category_id: '1' })
end
# Add controller action
collection_action :preview, method: :get do
#post = Post.new(params[:post_params])
end
My view gets rendered but nothing gets outputted, how do I then show the data?
Is this the correct approach?
Update
Can now show the hardcoded data with
collection_action :preview, method: :get do
#post = Post.new(permitted_params[:post])
end
The difference being in the active admin docs
The permit_params call creates a method called permitted_params. You should use this method when overriding create or update actions:
Now I just need to grab the form data outside the form and pass it through my link_to and then be able to keep the form populated with the same data if I go back to the form.
Ideally I would like to have the preview button within the f.actions as then I would have access to the #post object.

form_tag calling an instance of a class on the controller

I am trying to use a form_tag on a Rails application that, when submitted, call an instance of a class on the controller.
View
form_tag '/order', method: :get
select_tag 'type', options_for_select(#options_for_type, #type)
text_field_tag 'numbers', nil, placeholder: "100"
submit_tag "Get Info"
controller
def order
#order = Order.new(params_here).fetch_info
#type = params[:type] || "type1"
#options_for_type = [["type1", "type1"], ["type2", "type2"]]
...
end
This class sends some API requests (JSON).
Now problem is, I am getting an error from the API that some parameters are missing, which is expected since I haven't submitted the form with all the params, but it seems the class is called automatically when the page is loaded. I would have expected the class to be called only when the submit button is pressed?
Can you make the form_tag sort of a block? Like:
form_tag '/order', method: :get do
# All form elements
end
Maybe displaying your route here can also help.

Rendering ajax result in different view Rails 3

Given that I have the routes.
root :to => 'pessoas#index'
post '/pessoas' => 'pessoas#create'
And my view app/view/pessoas/index.haml has the following view code. (Submit here will post to pessoas#create)
= form_tag "/pessoas", :method => :post, :remote => true, :id => "participe" do
...
= button_tag "Sign up", :class => "envia"
And my create.js.erb view is.
$('#user-feedback').html('<%= #message %>');
And the controller code simply saves the model.
def create
#pessoa = Pessoa.new(params[:cliente])
if #pessoa.save
#message = "Success"
else
#message = "Failure"
end
respond_to do |format|
format.js
end
end
Then I go to the root path /
And I fill the form and click on submit.
Then the element p#user-feedback haven't been changed. Why?
I notice in app's log: that the request was done right and the jquery also but it render at /pessoas response but not at current view (index).
What can I do? Or should I just forget about this and make this with $.ajax way?
I want a way to provide feedback to the users.

Problem rendering an Ajax partial with link_to specifying a controller action

I have a div (id="content") on a "proposition" show page into which I want to render different partials depending on which tab at the top of the div is selected.
I'm trying to render a partial (substantive_content) with AJAX using the following link_to:
<%= link_to "Connected", :url => {:controller => "propositions",
:action => "substantive_content"}, :remote => true %>
I've got two problems:
On clicking the link, a GET request
is sent to
PropositionsController#show, and
not (I presume) to the "substantive
content" action. This just refreshes the page and doesn't do anything remotely. The correct partial isn't displayed.
I'm not sure how then to render a
partial into the "content" div.
At the moment I have this in the PropositionsController:
def substantive_content
respond_to do |format|
format.js{ render :update do |rightbar|
rightbar.replace_html 'content', 'Hello World!'
end}
end
end
... but am not sure where to go from here. Am I even going about this in the right way? I'm very new to rails, javascript, etc.
Any help with either of these problems would be fantastic. Thanks!
In your routes.rb file you should have an entry for the substantive_content action. It might look something like this:
match "substantive_content" => "propositions#substantive_content", :as => substantive_content
Then in your view the link should look something like this:
<%= link_to "Connected", substantive_content_path, :remote => true %>
I would render a js.erb file when the js request comes to the substantive_content action. The action would look soemthing like this:
def substantive_content
respond_to do |format|
format.js
end
end
Then create a substantive_content.js.erb file in your views/propositions directory. In that file you can use jquery to update the content div:
$('#content').html("<%= render :partial => 'your_partial_name_here' %>");
Without knowing more about the actual application you're working on, its hard to say if this is the best way to accomplish what you want. I generally try to stay as "RESTful" as possible with my apps so I'm reluctant to create actions aren't part of the normal index, new, create, show, etc. set. That's not to say that it isn't neccesary in this case, but it's something to think about.

Form behavior posting nested resource

I am using Ruby on Rails 3 and I would like to change its conventional behavior on posting a form in order to post from a signup action to the create action instead that from the new action. That is, I would like to use the signup action instead of the (conventional) new action in my User controller and trigger the create action to save my model data that contains nested resources.
In my /config/routes.rb file I have:
resources :users do
collection do
get 'signup'
end
resource :profile
end
In my /app/controllers/users_controller.rb I have
class UsersController < ApplicationController
def signup
#signup_user = User.new(params[:user])
#signup_user.build_profile # NOTE: Nested resource
...
end
def create
...
#signup_user.save
respond_to do |format|
format.html { render :action => :signup } # signup.html.erb
end
end
end
In my /app/views/users/signup.html.erb file I have
<%= form_for #signup_user do |f| %>
...
<% end %>
My problem is that if I submit the above form, I will be redirected to the index action of the user controller and not to the create action I expect. It seams that the form posts only to the index action.
How can I solve the problem?
I tryed to use the following
<%= form_for( :user, #signup_user, :url => { :controller => "users", :action => "create" }, :html => { :method => :post } do |f| %>
but I have still the problem: I am redirected to the index action.
SOLUTION
The problem did seam to be in the routers.rb. The correct code was
resources :users do
collection do
get 'signup'
post 'create'
end
resource :profile
end

Resources