Partial template submit_tag - ruby-on-rails

I would like to create a login field everywhere on the top of my page, so I've add a :
in application.html.erb :
<%= render :partial => 'sessions/new' %>
in .../views/sessions/_new.html.erb
<%= form_tag do %>
<div>
<label for="name">Email :</label>
<%= text_field_tag :name, params[:name] %>
<label for="password">Mot de passe :</label>
<%= password_field_tag :password, params[:password] %>
</div>
<div>
<%= submit_tag "Connection" %>
</div>
</fieldset>
But it's work only if I am in a sessions controller when I test it in my browser,
I think that :
<%= submit_tag "Connection" %>
refers to his current controller (sessions) that's why it's doesn't work in ads/index for exemple but do its job in sessions/index.
What can I do ?
Do I have to specify the controller in the submit_tag ?
Thanks a lot :)

You need to tell the form tag the url that the form should submit to. Maybe by default it submits to the current action or something? You should never rely on the default whatever it is.
Read the api
http://railsbrain.com/api/rails-2.3.2/doc/index.html?a=M002551&name=form_tag
oh and btw the submit tag is just a button, it doesn't have anything to do with why the form does or doesn't work. There's a lot of confusion among rails novices about forms - a lot of people don't really understand how forms work. Before using any rails helpers at all, i'd strongly recommend making your form in pure html. That way you will understand what is actually going on, and the form helpers will be just that, ie "things that help you to do something more quickly" rather than being these magical things that leave you totally clueless when they don't do what you expect.

You need to specify the controller but on the form_tag not the submit_tag
e.g. <%= form_tag :controller => 'sessions', :action => 'new' %>

Related

Form without action Ruby on Rails

I have been recently working with Ruby on Rails and have run into an issue that I can not quite figure out. I need to create a bunch of form mockups, that do not function. That is they should have the submit button, but it should not do anything upon being clicked. Normally using html I would do something along the lines of
<form action="#">
</form>
Trying to convert this to use Rails form helpers, I have done the following
<%= form_tag "#" do %>
<%= label_tag :username, "Username: " %>
<%= text_field_tag :username %>
<br />
<%= label_tag :password, "Password: " %>
<%= password_field_tag :password %>
<br />
<%= submit_tag "Login" %>
<% end %>
This generates a form that is similar to what I want to achieve, however when clicking the submit button it tries to access /# via post which is not the desired result. Currently the only thing I can think of to achieve this is to set the disabled attribute of the button, but is there a better way?
Unfortunately this can't be achieved with form helpers. Defining a form_for or a form_tag requires an action for the form. You can set
:action => "#"
But this will require including the action in routes -> having a controller with action for it -> rendering some page yet again.
You could manipulate the form after loading with javascript however (sust remember to set :remote to true - ). Or alternatively, if you insist on using the form helpers - replace the submit_tag with a button_tag:
<%= button_tag "Login", :type => 'button'%>
Try
<% form_tag "#", :onSubmit => "return false" do %>
Have you tried with button_tag instead of submit_tag? See here. Just make sure you don't use the default, or you will be right back where you started.

Passing Input Text From text_field_tag as a variable for a URL...?

Okay, so I didn't know really how to word this correctly, but here is essentially what I am trying to do.
I am trying to take the text that a user inputs into my search box and pass it on to the URL.
Here is my view page so far:
<h1>What's the weather like by you?</h1>
<br />
<%= form_tag('http://api.wunderground.com/api/myAPIkey/conditions/q/**USER_TEXT_FROM_TEXT_FIELD_TAG**.json',:method =>
'get') do %>
<p>
<%= text_field_tag 'zipcode', params[:search] %>
<%= submit_tag "Check It Out!", :name => nil %>
</p>
<% end %>
I know this is probably such an easy thing to do, but I can't seem to find any way to correctly do it. Thanks for your help!
It looks like you are trying to redirect form submission to different url based on user input.
My no JavaScript sugestion would be to go through your own controller and redirect_to custom url. Something like this:
change your view to:
<h1>What's the weather like by you?</h1>
<br />
<%= form_tag('/weather') do %>
<p>
<%= text_field_tag 'zipcode' %>
<%= submit_tag "Check It Out!", :name => nil %>
</p>
<% end %>
create weather controller:
rails g controller weather create
add this line to your config/route.rb file:
match 'weather' => 'weather#create', via: :post
and modify you app/controllers/weather_controller.rb to look like this:
class WeatherController < ApplicationController
def create
redirect_to "http://api.wunderground.com/api/myAPIkey/conditions/q/#{params[:zipcode].split.join('+')}.json"
end
end
This isn't a nice solution and it isn't the smartest solution, it simply duplicates your code using rails stack. Your question doesn't give many information about what you would like to to with the date returned by api?? Do you really want to simply redirect to given url and see data as json?
I just try to give you another idea how to tackle this problem, its not a final solution.

re-use views on rails

I can't find a way to use views from other views.
I think I can explain it better with this example: I have two controllers, IndexController and UserController. The user controller has an action called login with its view which renders the login form and validates the user when a post from its form happens.
In IndexController, there is an action called home which renders the home page view. In this view I want to render the loginform, but I want to re-use the user/login view.
In almost all the frameworks I worked with there is a way to call another controller action in order to re-use and isolate the logic.
I find that, in Rails, there is a helper render that I can call in the following way:
render 'user/login'
or
render :template => 'user/login'
In both cases I only get errors like "the route index/login does not exist" or "the controller index has to login action". Besides, I set the login view file name as login or _login. (I read that is used for partial views.)
You can do this by creating a shared folder within your views folder and create the login form as a partial.
So your login partial might be something like this.
/app/views/shared/_loginform.html.erb:
<div id="loginform">
<%= form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>
<%= f.text_field :email %>
<%= f.password_field :password %>
<%= f.submit "Login" %>
<% end %>
<div id="loginlinks">
<%= link_to "Become a member", new_member_registration_path %> <br />
<%= link_to "Forgot Password?", new_member_password_path %>
</div>
</div>
Then in your views just call:
<%= render "shared/loginform" %>
You should take a look at Railscast: "#269 Template Inheritance".
Since Rails 3.1, controller inheritance now applies to views as well. This is preferred over the older "shared folder" strategy.
In short, you put your shared partials in app/views/application then you can override them in your resource specific view directories (e.g app/views/users).

Keep Rails from redirecting a page

I'm having some trouble and I hope someone can help me. I have an erb file with a form in it that has a button. When you click the button it redirects the page elsewhere and shows the erb file that I told it to point to. This is great except I'd really like to stuff the content of that directed-to erb file into a div that is sitting below my form. My view looks something like this.
<div id="formentry">
<%= form_for #time, :url => {:action => 'list'}, :remote => true, :update => 'results' do |f|%>
<%= select :time, :period, TimeSelectModel::TIMEVALUES %>
<%= f.submit "Submit" %>
<% end %>
</div>
<div id="results"></div>
From what I've read online this seems like the approach you're supposed to take to do this in Rails3 but I'm not finding that it's working. (see: the page is completely redirecting) What am I doing wrong? Thanks.
My impression from your post is that you want to submit a form and show the results without leaving the page. What you are looking to do requires use of javascript/ajax.
Checkout railscast 205 for an example of how to do this.

Easy way to turn a Rails form into an AJAX form?

I have the following form in my Rails application:
<% form_tag password_resets_path, :id => 'recoverPasswordForm' do %>
<label for="passwordRecoveryEmailAddress">Email Address:</label>
<%= text_field_tag "passwordRecoveryEmailAddress" %>
<%= submit_tag 'Recover' %>
<br />
<div id="forgotPasswordLoginLinkContainer">
<a id="forgotPasswordLoginLink" href="/login">Login Instead</a>
</div>
<% end %>
When this form is submitted, the page must reload. I would like to easily turn this form into an AJAX form, such that the form submits via AJAX, and a page reload does not happen.
I could do this easily using jQuery, hooking into the .submit() function. But, I am curious: does Rails provide some easy way to turn any given form into an AJAX form? Or, what's the simplest (yet elegant) way possible? Maybe something like
<% form_tag password_resets_path, :id => 'recoverPasswordForm', :ajax => true do %>
I'm using Rails 2.
Yes, and you were close with your guess. Rails 3 allows you to do form_tag ..., :remote => true to let the form use AJAX if Javascript is available.
See http://railsapi.com/doc/rails-v3.0.0/classes/ActionView/Helpers/FormTagHelper.html#M002483

Resources