in js.rjs file i am shwoing prompt with textbox like
page << "var name = prompt('Enter name:'); "
i want to add the value name to the table names
is it possible to do from rjs
if so please guide me how to do it..
You shouldn't do this back and forwarding with rjs. You're returning javascript to be executed by the client, and you can't get the value back directly unless you embed another AJAX call into the javascript your return.
A better way to do this would be to use a single AJAX call. Use a remote_form_for with a text_field for the user to enter their name into, then POST it to your controller action and store it in the database in the normal fashion.
Something like:
# In your view
<% remote_form_for :user, :url => { :controller => :users, :action => :create } do |f| %>
<%= f.text_field :name %>
<%= f.submit %>
<% end %>
# In your controller
def create
#user = User.new(params[:user])
#user.save
render :update do |page|
# Return whatever javascript you want here
page << "alert('Created user')"
end
end
Related
I would like to have a drop down menu with a list of all the user names in the db. From there, I would like the user to choose his/her name and be able to click login and be taken to their respective page. At this point, a password is not needed. Currently, I have the following:
controller:
def login
#user = User.new
#users = User.all
# #user = User.find_by_id(:id)
# redirect_to user_path(#user)
end
view:
<%= form_for #user, url: '/login', html: {method: 'get'} do |f| %>
<%= f.label "Name" %>
<br/>
<%= select_tag :user, options_for_select(#users) do |users| %>
<%= link_to users.name, users %>
<% end %>
<br/>
<br/>
<%= f.submit 'Login' %>
<% end %>
I cannot seem to link the user to their path and also, i want to show the users name in the drop down menu. Currently, it shows a hexidecimal pointer.
Thank you in advance.
You shouldn't be making a new User object here: you just want to load one out of the database. What you want to do in the controller is just to set current_user to be one of the existing users, right?
Also you've got the form submitting back to the action which loads the form in, which seems weird. I would make it submit to a new action, like "set_current_user" which is a POST action.
in your login template:
<%= form_tag '/set_current_user' do %>
<%= f.label "Name" %>
<br/>
<%= select_tag "user_id", options_for_select(#users.collect{|user| [user.name, user.id] } %>
<br/>
<br/>
<%= submit_tag 'Login' %>
<% end %>
in the controller (you'll need to amend routes.rb to make the '/set_current_user' go to this action) you then need to set something which will keep the user logged in. The traditional way to do this is via session[:user_id], and to have a method current_user which uses this.
def set_current_user
session[:user_id] = params[:user_id]
redirect_to "/" and return
end
Your initial approach is reminiscent of how this sort of thing is normally handled, wherein you do have a form_for, but it's for a UserSession object rather than a User object.
I have three field
a) email
b) phone number
c) browse files (Here one link `add more file`)
When user click on add more file link then one more browse button appear and so on
I want to save all files when user click on submit button
How Can I do that with paperclip in rails 4
Model
User.rb
has_many invoices
accepts_nested_attributes_for :invoices, :allow_destroy => true
Invoice.rb
belongs_to :user
I have use field_for for invoice in view
controller
def new
#user = User.new
#user.invoices.build
end
def create
User.create(user_params)
end
It looks like your current setup will work well for one file, however if you want to add multiple files, you'll need to build the objects again & load a new fields_for to support that. To do this, I would recommend using Ajax:
I don't know if this will work, but this is how I would tackle the problem:
Put your fields_for into a partial
On your form, include "+" button with :remote => :true
Controller will handle AJAX request & build a new object
On successful AJAX request, load a partial with new fields_for
See if the submit includes all the files
Create fields_for Partial
Your form will likely look like this:
<%= f.fields_for :invoices do |field| %>
<%= field.file_field :image %>
<% end %>
You need to turn this into a partial:
<%= f.fields_for :invoices do |field| %>
<%= render :partial => 'images_fields', locals: {f: field} %>
<% end %>
Your partial could then look like:
<%= f.file_field :image %>
Include "+" Button On Your Form
On your form, you can just create a link like this:
<%= link_to "+", your_controller_action, :remote => :true %>
Controller Will Handle Ajax Request
#app/controllers/your_controller.rb
def new
#user = User.new
#user.invoices.build
respond_to do |format|
format.js { #user.invoices.build }
format.html
end
end
This should render a new object, which you can then append to the form with the returned JS:
#app/views/controller/new.js.erb
$("form").append("<%=j render :partial => 'images_fields', locals: {f: #user} %>")
See If It Submits All The Files
My code probably won't work as it needs debugging. However, this is the process I would use. If you try it, we can debug in the comments
Let me know how it goes!!
I am working on a simple youtube list app where the user can add videos to the list. I am validating for the presence of the video_id field.
class Track < ActiveRecord::Base
attr_accessible :title, :thumbnail_url, :video_id
validates :video_id, :presence => true
end
i have the following create function defined in my controller:
def create
#fetches the video info, stores it in #trackinfo
if is_url(params[:track][:query])
#trackinfo = getTrackInfo(params[:track][:query])
else
#trackinfo = youTubeQuery(params[:track][:query])
end
#use #trackinfo to create track object
#track = Track.new(#trackinfo)
#tracks = Track.all
#video_ids = Track.pluck(:video_id)
if #track.save
else
render :action=>"index"
end
end
in my index.erb.html i have the following block:
<%= render partial: "error_message" %>
the corresponding _error_message.erb.html just contains the error messages from the validation:
<% if #track.errors.any? %>
<% #track.errors.full_messages.each do |msg| %>
<%= msg %><br>
<% end %>
<% end %>
the problem is when the validation fails, i am not able to see the error message from the rendered view. I logged the messages right before it entered the render index, and was able to see the messages:
from the controller:
if #track.save
else
puts "#{#track.errors.full_messages}" #i am able to see this
render :action=>"index"
end
i dont know what happens during the render causing the error messages not to be displayed, on the server logs it does say that the _error_messages.erb.html has been rendered, but i just dont see anything happen to the page. I feel like i have missed something really obvious. anyone knows what i should do?
Thanks
i think i resolved this issue, but im not sure if my fix is proper. I forgot to mention that on the main index.erb.html i have a search bar and a submit buttom embedded in an ajax form that calls the create function inside the controller
<%= form_tag(#track, :remote=>true, :method => :post, :class => 'new_track') do %>
<%= text_field_tag "track_query", nil, :placeholder => 'enter a query or a link', :id => "search_bar", :name => "track[query]", :size => 30, :required => true %>
<%= submit_tag "add" , :name => 'commit'%>
</p>
<% end %>
i also now have the error div in the same page (I deleted the render partial and just stuck an empty div to be populated in the index.erb.html):
<div id = "error_message">
</div>
in the file create.js.erb, I added the following lines:
<% elsif #track.errors.any? %>
if($.trim($("#error_message").html())==''){
<% #track.errors.full_messages.each do |msg| %>
$('#error_message').hide().append('<%= msg %>');
<% end %>
$('#error_message').fadeIn();
}
$('#search_bar').val('');
it seems that when i remove the render :action=>"index" completely from my create function in the controller, the error messages were able to be displayed on the index page. I was hoping to not put so much processing on the client javascript side and more on the controller side. Is there another way to do this? Still wondering why the render did not render that partial html. is it because the form is ajax and wont render the whole page? i apologize if i dont exactly know what im talking about :)
I have the following form and controller.
<%= form_tag do %>
<div>
<%= label_tag :Old %>
<%= password_field_tag :old_password %>
</div>
<div>
<%= label_tag :New %>
<%= password_field_tag :new_password %>
</div>
<div>
<%= label_tag :Confirm %>
<%= password_field_tag :confirm_password %>
</div>
<div>
<%= submit_tag "Update" %>
</div>
<% end %>
and the controller:
def change
#user = current_user
#op = params[:old_password]
#np = params[:new_password]
#cp = params[:confirm_password]
if #np == #cp
#user.update_with_password(:password => #np, :current_password=>#op)
if #user.save
flash[:notice] = "Password Successfully Changed"
redirect_to home_path
end
else
#user.errors.add("Incorrect confirmation")
end
end
This is all tied to 'password/change' in the config/routes.rb
The problem is that when I go to /password/change I immediately an redirected to home and receive the "password successfully changed" flash notice. What I take from it is that it is not requiring me to click the submit button in order to pass the parameters. How do I make it so that it waits for the submission of the form before continuing through the controller?
The problem is that you need 2 separate methods. One to show the view and one to handle the form post.
Right now the "change" method in your password_controller is handling the form post so you need a method like "index" to show the form
def index
#move your form to /views/password/index.html.erb or specifically render your 'change' view
end
Then in your markup add the action to your form
<%= form_tag('/password/change') do %>
…
Then in your controller you can specify POST only for the change method
class PasswordController < ApplicationController
verify :method => :post, :only => :change
…
UPDATE
Instead of using the verify method (removed in rails 3) you should either set up your route to be restful (Rails Routing from the Outside In) or you can create a specific route for changing the password like so:
match 'password/change' => 'password#change', :via => :post
The best practice will be to separate these two things into different controller methods. One should be used to simply display the view, while the other should be used to handle the POST request. However, if you're dead set on doing it this way, I believe a solution like this will work:
def change
#user = current_user
#op = params[:old_password]
#np = params[:new_password]
#cp = params[:confirm_password]
if #np && #np == #cp # This checks to see if params[:new_password] is nil
#user.update_with_password(:password => #np, :current_password=>#op)
I'd strongly suggest you separate it out though.
When you render password/change from the browser, params is nil, so #np == #cp always evaluates to TRUE, executes the update, saves, and does the redirect — in fact, if you check I would bet the user's password is being set to nil.
You need to make sure the params are not empty before attempting the update.
Does that help point you in the right direction?
side note: from a code readability standpoint I might recommend just using the variables inline, instead of the instance vars, but that's just my opinion :)
I have an erb file named index that has a form in it. As a simple example:
<% form_for #foo, :url => {:action => 'bar'} do |f| %>
<%= f.submit "BAR!" %>
<%end%>
When I click the BAR! button it preforms the actions I expect and it forwards me onto the bar.erb file, displaying the expected output. What I would like to be able to do, however, is to take the generated html from this page and stuff it into the innerHTML of a div on the index page. I assume there is a way but I must ask, is there a way to achieve this? Are there any examples available that would be helpful? Thanks!
You should be able to pass the id of the div to update like so:
<% remote_form_for #foo, :url => {:action => 'bar'}, :update => 'id-of-div-to-update' do |f| %>
<%= f.submit "BAR!" %>
<%end%>
In the controller:
def bar
# your code here
respond_to do |format|
format.html { redirect_to(xxx) }
format.js
end
end
Rails will look for a template named bar.js and will render it and return it's content to the browser without a redirect.