rails create pages by hash with nested resources - javascript

image
as image, i'd like to make page with nested resources.
my plan is
register user
create tour(request input informations by params)
if #tour.tour_days(params) value is over 1
create day pages as much as the number of user put into tour_day params
so i want to create page like(if 1user create first tour and put 3 into tour_day)
localhost:3000/tours/1/days/1
localhost:3000/tours/1/days/2
localhost:3000/tours/1/days/3
(if 1user create second tour and put 2 into tour_day)
localhost:3000/tours/2/days/1
localhost:3000/tours/2/days/2
i'd like to create these pages by automatically when user put number into tour_day and click submit button from tour_view
i've done create nested resources and throw data but i can't make clearly.. ;(
the reason i nest resources and create additional controller(day_controller) is afterwords i'd like to add recommendation function(recommendation for whom couldn't make schedule)...
actually i'd like to make page like airbnb(creating room process)
(Am i doing right process??)
tour_controller
class ToursController < ApplicationController
before_action :set_tour, only:[:show, :edit, :update]
before_action :authenticate_user!, except:[:show]
def index
#tours = current_user.tours
end
def show
end
def new
#tour = current_user.tours.build
end
def create
#tour = current_user.tours.build(tour_params)
i = 0
if #tour.save
redirect_to new_tour_day_path(#tour.id, #day), notice: "Saved Your Tour Courses"
else
render :new
end
end
def edit
end
def update
if #tour.update(tour_params)
redirect_to edit_tour_day_path(#tour.id, #day), notice: "Updated Your Tour Courses"
else
render :edit
end
end
private
def set_tour
#tour = Tour.find(params[:id])
end
def tour_params
params.require(:tour).permit(:tour_title, :tour_theme, :tour_summary, :tour_language, :tour_day, :tour_member, :tour_car, :tour_camera, :price)
end
end
day_controller
class DaysController < ApplicationController
before_action :set_tour
before_action :set_day, only: [:show, :edit, :update]
before_action :authenticate_user!, except:[:show]
def index
end
def show
end
def new
#tour = current_user.tours.find(params[:tour_id])
#day = #tour.days.build
end
def create
#tour = current_user.tours.find(params[:tour_id])
#day = #tour.days.build(day_params)
if #day.save
redirect_to edit_tour_day_path(#tour.id, #day), notice: "Saved Your Tour Courses"
else
render :new, notice: "Errors"
end
end
def edit
end
def update
if #day.update(day_params)
redirect_to edit_tour_day_path(#day), notice: "Updated Your Tour Courses"
else
redner :edit
end
end
private
def set_tour
#tour = Tour.find(params[:tour_id])
end
def set_day
#day = Day.find(params[:id])
end
def day_params
params.require(:day).permit(:day_number)
end
end
tour_view(test page)
<div class="container">
<%= form_for #tour, :html => { multipart: true } do |f| %>
<div class="col-md-3 select">
<div class="form-group">
<label>Maximum Tour Day</label>
<%= f.select :tour_day, [["1",1], ["2",2], ["3",3], ["4",4]], prompt: "Select...", class: "form-control" %>
</div>
</div>
<div class="actions">
<%= f.submit "save", class: "btn btn-primary" %>
</div>
<% end %>
</div>
day_view(test page)
<div class="container">
<%= form_for [:tour, #day] do |f| %>
<label>day_number</label>
<%= f.text_field :schedule_, class: "form-control" %>
<div class="actions">
<%= f.submit "Save", class: "btn btn-primary"%>
</div>
<% end %>
</div>
** additionally
Inside of Day view
i'd like to create schedule like
image
when i click an icon, the form appear and make detail tour schedule with ajax.
how can i approach to create this function??
----
this is my first project of web programming, im so confusing
plz... give me some(lots of) help...
----
im using rails 5.0.0.1 and rails 2.3.1

Related

Rails jQuery is rendering text instead of collection

I created a file inside my javascript folder "load-contacts.js" in hopes of trying to load the collections as well as the pagination via jquery.
$(document).on('turbolinks:load', function() {
$('#contacts').html("<%= j render(#contacts) %>");
$('#paginator').html("<%= j paginate #contacts, remote: true %>");
});
When I run this code it actually works and change the part where it intended to change. However, it is only rendering it as text as it instead of the collection and the pagination (kaminari gem).
Here's how it looks like:
Any idea what am i missing here?
UPDATES:
Here's my contacts controller
class ContactsController < ApplicationController
before_action :find_params, only: [:edit, :update, :destroy]
def index
# catch the group id from params in session
session[:selected_group_id] = params[:group_id]
#contacts = Contact.by_group(params[:group_id]).search(params[:term]).order(created_at: :desc).page params[:page]
end
def autocomplete
#contacts = Contact.search(params[:term]).order(created_at: :desc).page(params[:page])
render json: #contacts.map { |contact| { id: contact.id, value: contact.name } }
end
def new
#contact = Contact.new
end
def create
#contact = Contact.new(contact_params)
if #contact.save
flash[:success] = "Contact was successfully created."
redirect_to(previous_query_string)
else
render 'new'
end
end
def edit
end
def update
if #contact.update(contact_params)
flash[:success] = "Contact successfully updated."
redirect_to(previous_query_string)
else
render 'edit'
end
end
def destroy
#contact.destroy
flash[:success] = "Contact successfuly deleted."
redirect_to contacts_path
end
private
def contact_params
params.require(:contact).permit(:name, :email, :phone, :mobile, :company, :address, :city, :state, :country, :zip, :group_id, :avatar)
end
def find_params
#contact = Contact.find(params[:id])
end
def previous_query_string
session[:selected_group_id] ? { group_id: session[:selected_group_id] } : {}
end
end
Here's the part for the kaminari gem pagination:
<div class="card-footer">
<div class="pagination justify-content-center" id="paginator">
<%= paginate #contacts, remote: true %>
</div>
</div>
And here's where I am suppose to be rendering the contacts inside the views/contacts/index.html.erb:
<table class="table" id="contacts">
<%= render partial: "contact", object: #contacts, remote: true %>
</table>
you can't use j render outside .erb. Because j render is method of rails, your js just know it is string
what you need are use script tag inside your html.erb, or use rails ajax. It is working as expected
reference: How to render partial on the same page after clicking on link_to with AJAX
have you tried escape_javascript before calling render.
something like:
<%= escape_javascript render(my_partial) %>

AJAX button processes, doesn't change

I've found a lot of people with the same problem I have, but none of the solutions apply to my situation. I am following Michael Hartl's tutorial here. I have a follow system set up in accordance with chapter 14 (I'm using the latest edition of the book, as I am on Rails 5.1). The follow/unfollow button processes in the database, but I have to manually refresh the page to see the button and follow count change.
I get this in the browser console log:
POST 500 (Internal Server Error)
jquery.self-bd7ddd393353a8d2480a622e80342adf488fb6006d667e8b42e4c0073393abee.js:10255
send # jquery.self-bd7ddd393353a8d2480a622e80342adf488fb6006d667e8b42e4c0073393abee.js:10255
ajax # jquery.self-bd7ddd393353a8d2480a622e80342adf488fb6006d667e8b42e4c0073393abee.js:9739
ajax # jquery_ujs.self-784a997f6726036b1993eb2217c9cb558e1cbb801c6da88105588c56f13b466a.js:94
handleRemote # jquery_ujs.self-784a997f6726036b1993eb2217c9cb558e1cbb801c6da88105588c56f13b466a.js:179
(anonymous) # jquery_ujs.self-784a997f6726036b1993eb2217c9cb558e1cbb801c6da88105588c56f13b466a.js:512
dispatch # jquery.self-bd7ddd393353a8d2480a622e80342adf488fb6006d667e8b42e4c0073393abee.js:5227
elemData.handle # jquery.self-bd7ddd393353a8d2480a622e80342adf488fb6006d667e8b42e4c0073393abee.js:4879
relationships_controller.rb:
class RelationshipsController < ApplicationController
before_action :authenticate_user!
def create
user = User.find(params[:followed_id])
current_user.follow(user)
respond_to do |format|
format.html { redirect_to(:back) }
format.js
end
end
def destroy
user = Relationship.find(params[:id]).followed
current_user.unfollow(user)
respond_to do |format|
format.html { redirect_to(:back) }
format.js
end
end
end
views/users/_follow.html.erb:
<%= form_for(current_user.active_relationships.build, remote: true) do |f| %>
<div><%= hidden_field_tag :followed_id, #user.id %></div>
<%= f.submit "Follow", class: "btn btn-primary" %>
<% end %>
views/users/_unfollow.html.erb
<%= form_for(current_user.active_relationships.find_by(followed_id: #user.id),
html: { method: :delete }, remote: true) do |f| %>
<%= f.submit "Unfollow", class: "btn" %>
<% end %>
views/users/_follow_form.html.erb
<% if current_user != #user %>
<div id="follow_form">
<% if current_user.following?(#user) %>
<%= render 'unfollow' %>
<% else %>
<%= render 'follow' %>
<% end %>
</div>
<% end %>
relationships/create.js.erb
$("#follow_form").html("<%= escape_javascript(render('users/unfollow')) %>");
$("#followers").html('<%= #user.followers.count %>');
relationships/destroy.js.erb
$("#follow_form").html("<%= escape_javascript(render('users/follow')) %>");
$("#followers").html('<%= #user.followers.count %>');
users_controller.rb
class UsersController < ApplicationController
before_action :authenticate_user!, only: [:index, :edit, :update, :destroy,
:following, :followers]
def index
#page_title = "Forge Users"
#users = User.all
end
def show
#user = User.find(params[:id])
#posts = Post.all
end
def following
#title = "Following"
#user = User.find(params[:id])
#users = #user.following
render 'show_follow'
end
def followers
#title = "Followers"
#user = User.find(params[:id])
#users = #user.followers
render 'show_follow'
end
end
What am I doing wrong?
You set local variable user
def create
user = User.find(params[:followed_id])
but reference class instance variable #user in your .js.erb template.
$("#followers").html('<%= #user.followers.count %>');
Instead, set #user = User.find(params[:followed_id]) so it is available to the views. Actually, I think you are trying to set #user = current_user. That might make more sense.
You need to rename your Javascript files to have .erb ending.
relationships/create.js.erb and relationships/destroy.js.erb. It's because you have Ruby in the file so it has to process the Ruby before it gets interpreted as Javascript and sent back.
http://guides.rubyonrails.org/working_with_javascript_in_rails.html#a-simple-example

I can't call my new variable, why not?

I'm new in rails, so I do not master even the subtleties of this langage.
I meet an error which say undefined method 'prix' for Online::ActiveRecord_Associations when I try to call my variable "prix" and "portion" into the post index.
So I can I do that ?
My code :
Onlines controller :
class OnlinesController < ApplicationController
before_action :authenticate_user!
before_action :set_post
before_action :owned_online, only: [:new, :edit, :update]
before_action :set_online
def index
#post = Post.all.order('created_at DESC')
end
def new
#online = current_user.onlines.build
#online.post_id = #post.id
#online.user_id = current_user.id
end
def create
#online = #post.onlines.create(online_params)
#online.user_id = current_user.id
#online.post_id = #post.id
if #online.save(online_params)
#online.update(push: true)
#online.update(pushed_at: Time.zone.now)
flash[:success] = 'Votre post est en ligne !'
redirect_to post_path(#post)
else
render 'new'
end
end
def show
end
def update
if #onlines.update(online_params)
if #online.push == false
if #online.portion <= 1
#online.update(push: false)
flash[:success] = 'Veuillez indiquer le nombre de parts disponibles '
redirect_to root_path
else
#online.update(push: true)
flash[:success] = 'Votre post a bien été pushé !'
redirect_to root_path
end
end
else
#user.errors.full_messages
flash[:error] = #user.errors.full_messages
render :edit
end
end
private
def online_params
params.require(:online).permit(:user_id, :post_id, :prix, :portion, :push, :pushed_at)
end
def owned_online
#post = Post.find(params[:post_id])
unless current_user == #post.user
flash[:alert] = "That post doesn't belong to you!"
redirect_to :back
end
end
def set_post
#post = Post.find_by(params[:post_id])
end
def set_online
#post = Post.find(params[:post_id])
#online = Online.find_by(params[:id])
end
end
Onlines/Index view :
<div class="title text-center">
<h1>Alors ? On mange quoi ?</h1>
</div>
<br>
<%= render 'posts/post' %>
posts /post view :
<div class="row">
<%-#posts.each do |post|%>
<div class="post">
<div class="form-group text-center">
<h3> <%=post.title%></h3>
</div>
<p> Posted by : <%= link_to post.user.pseudo, profile_path(post.user.pseudo) %>, <%= time_ago_in_words(post.created_at) %> ago </p>
**<p><%= post.onlines.prix %></p>
<p><%= #online.portion %></p>**
<div class="image text-center">
<div class="image-border">
<%= link_to (image_tag post.image.url(:medium), class:'img-responsive'), post_path(post)%>
</div>
</div>
</div>
<% end %>
</div>
</div>
</div>
So, if you have any ideas, tell me please.
In your posts/post.html.erb
**<p><%= post.onlines.prix %></p>
You encountered the error undefined method 'prix' for Online::ActiveRecord_Associations because posts.onlines is a collection of one or more Online objects, and NOT a single Online record.
By this, I meant that the following values may differ (as an example):
post.onlines.first.prix
post.onlines.last.prix
If you want to select just the #online record for every specific post in your <%-#posts.each do |post|%>
loop, then you could just do the following
posts/post.html.erb
...
**<p><%= #online.prix %></p>
<p><%= #online.portion %></p>**
...
Or if you want to show all prix values for each Online record in post.onlines, then you could do the following
posts/post.html.erb
...
<% post.onlines.each do |online| %>
**<p><%= online.prix %></p>
<% end %>
<p><%= #online.portion %></p>
...

How to update two _form from one submit?

How can the current_user update his email, if he chooses, without having to have a separate submit button?
current code
<%= form_for(#challenge) do |challenge| %>
<%= challenge.action %>
<%= challenge.send_email %>
<% end %>
<%= form_for(current_user) do |user| %>
<%= user.email %>
<%= user.submit %>
<% end %>
<%= form_for(#challenge) do |challenge| %>
<%= challenge.submit %>
<% end %>
overall idea
<%= form_for(#challenge) do |challenge| %>
<%= challenge.action %>
<%= challenge.send_email %>
<%= form_for(current_user) do |user| %> # Or somehow remove form_for(current_user) altogether while still being able to update current_user.email within the form_for(#challenge)
<%= user.email %>
<% end %>
<%= challenge.submit %>
<% end %>
image of _form
Offering controllers code to see if we can make this work via fields_for
challenges_controller
class ChallengesController < ApplicationController
before_action :set_challenge, only: [:show, :edit, :update, :destroy, :challenging, :mark_accomplished, :mark_completed, :create_freebie, :like]
respond_to :html, :json
def show
#challenge_to_deadline = current_user.challenges.group_by {|i| i.deadline} if current_user
#notable = #challenge
#notes = #notable.notes
#note = Note.new
#commentable = #challenge
#comments = #commentable.comments
#comment = Comment.new
#correct_user = current_user.challenges.find_by(id: params[:id])
end
def new
#challenge = Challenge.new
respond_modal_with #challenge, location: root_path
end
def edit
end
def create
#challenge = Challenge.new(challenge_params)
if params[:step] == '2'
if current_user == nil
# If there is no user, store the lifetime values to the session.
session[:challenge_action] = challenge_params[:action]
session[:challenge_committed] = challenge_params[:committed]
session[:challenge_deadline] = [params["challenge"]["deadline(3i)"], params["challenge"]["deadline(2i)"], params["challenge"]["deadline(1i)"]].join('/')
session[:challenge_date_started] = [params["challenge"]["date_started(3i)"], params["challenge"]["date_started(2i)"], params["challenge"]["date_started(1i)"]].join('/')
session[:challenge_order] = challenge_params[:order]
session[:challenge_days_challenged] = challenge_params[:days_challenged]
session[:challenge_why] = challenge_params[:why]
session[:challenge_conceal] = challenge_params[:conceal]
redirect_to signup_path
else
#challenge = current_user.challenges.build(challenge_params)
if #challenge.conceal == true
#challenge.save
redirect_to root_path
if #challenge.date_started.present?
flash[:info] = 'habit Challenge secretly saved! Click "Strike 1" upon incompleting a day.'
else
flash[:info] = 'goal Challenge secretly saved! Click checkmark upon completing it.'
end
elsif
#challenge.save
track_activity #challenge
redirect_to root_path
if #challenge.date_started.present?
flash[:info] = 'habit Challenge saved! Click "Strike 1" upon incompleting a day.'
else
flash[:info] = 'goal Challenge saved! Click checkmark upon completing it.'
end
else
respond_modal_with #challenge
end
end
end
end
def update
#challenge.update(challenge_params)
flash[:info] = 'Challenge updated'
respond_modal_with #challenge, location: root_path
end
private
def set_challenge
#challenge = Challenge.find(params[:id])
end
def challenge_params
params.require(:challenge).permit(
:action,
:why,
:like,
:deadline,
:accomplished,
:tag_list,
:conceal,
:archive,
:trigger,
:missed_days,
:target,
:reward,
:order,
:date_started,
:date_ended,
:days_challenged,
:completed_at,
:freebie,
:freebie_date,
:send_email => [],
:committed => [])
end
end
users_controller
class UsersController < ApplicationController
before_action :logged_in_user, only: [:index, :edit, :update, :destroy, :following, :followers]
before_action :correct_user, only: [:edit, :update]
def show
#user = User.find(params[:id])
#past_challenges = #user.challenges.publish.order("deadline ASC").select{ |challenge| challenge.deadline < Date.current if challenge.deadline.present? }
#past_challenges_by_years = #past_challenges.group_by { |t| t.deadline.beginning_of_year }
#present_oneshot_challenges = #user.challenges.unaccomplished.publish.order("deadline ASC").select{ |challenge| challenge.deadline == Date.current if challenge.deadline.present? }
#present_habit_challenges = #user.challenges.unaccomplished.publish.order("date_started DESC").select{ |challenge| challenge.date_started <= Date.tomorrow if challenge.date_started.present? }
#future_challenges = #user.challenges.unaccomplished.publish.order("deadline ASC").select{ |challenge| challenge.deadline > Date.current if challenge.deadline.present? }
#future_challenges_by_years = #future_challenges.group_by { |t| t.deadline.beginning_of_year }
#inspirations = #user.inspirations.publish
end
def new
#user = User.new
end
def create
#user = User.new(user_params)
if #user.save
action = session.delete(:challenge_action)
deadline = session.delete(:challenge_deadline)
committed = session.delete(:challenge_committed)
date_started = session.delete(:challenge_date_started)
order = session.delete(:challenge_order)
days_challenged = session.delete(:challenge_days_challenged)
why = session.delete(:challenge_why)
conceal = session.delete(:challenge_conceal)
# Create
if deadline.present?
#user.challenges.create(action: action, deadline: deadline, why: why, conceal: conceal, date_started: date_started, committed: committed, days_challenged: days_challenged)
end
#user.send_welcome_email
log_in #user
redirect_to tutorial_url
flash[:info] = 'Let the games begin! Add another challenge with + Challenge'
else
render 'new'
end
end
def edit
#user = User.find(params[:id])
end
def update
#user = User.find(params[:id])
if #user.update_attributes(user_params)
redirect_to root_url
flash[:success] = "Settings updated"
else
render 'edit'
end
end
private
def user_params
if params[:conceal] = true
params.require(:user).permit(:time_zone, :name, :email, :tag_list, :password, :conceal, inspirations_attributes: [:name, :tag_list, :conceal], activities_attributes: [:conceal, :action, :trackable_id, :trackable_type])
else
params[:user][:conceal] = false
params.require(:user).permit(:time_zone, :name, :image, :tag_list, :email, :password, inspirations_attributes: [:name, :tag_list], activities_attributes: [:action, :trackable_id, :trackable_type])
end
end
# Confirms a logged-in user.
def logged_in_user
unless logged_in?
store_location
flash[:danger] = "Please sign in first"
redirect_to root_url
end
end
# Confirms the correct user.
def correct_user
#user = User.find(params[:id])
redirect_to(root_url) unless current_user?(#user)
end
end
Embedding form within another form is not valid html. There can be multiple forms with in a page, but they cannot be embedded within each other.
If you want to have fields for different models with in the same form, depending upon the association between those models, you could make use of fields_for within the form_for to render the form fields from those models. And then when the form is submitted, again depending upon the associations between the models, you could persist the data.
You could also make use of javascript and submit/update parts of the form. For example: when the contents of the text_field have changed, you could trigger an AJAX request and persist the data.
Refer to FormHelper#fields_for for more info.
Update:
Based on your response in the comments, as you have the following models:
class User < ActiveRecord::Base
has_many :challenges
end
class Challenge < ActiveRecord::Base
belongs_to :user
end
You have two approaches.
Approach 1: Through controller action filters. Intercept the user's email from the params in the controller and update it. Something like:
class ChallengesController < ApplicationController
before_action :update_user_email, if: proc {|c| c.current_user.present? && c.params[:email].present? }
private
def update_user_email
email = params[:email]
current_user.update_attribute(:email, email)
end
end
And update your form code as follows:
<%= form_for(#challenge) do |challenge| %>
# include fields for challenge
<%= text_field_tag :email, current_user.email %>
<%= challenge.submit %>
<% end %>
Approach 2: Through model callbacks. Using a combination of attr_accessor and the callbacks in the model.
class Challenge < ActiveRecord::Base
belongs_to :user
attr_accessor :user_email
before_save :update_user_email, if: :user_email_present?
protected
def user_email
self.user.email if self.user.present?
end
def user_email_present?
self.user_email.present?
end
def update_user_email
# based on your logic in controller, looks like the
# `current_user` and the `user` of the `challenge`
# are one and the same. So you could directly update
# `email` on the `user` object.
self.user.update_attribute(:email, self.user_email)
end
end
And update your form code as follows:
<%= form_for(#challenge) do |challenge| %>
# include fields for challenge
<%= challenge.text_field :user_email, current_user.email %>
<%= challenge.submit %>
<% end %>

RJS: Ajaxified select_tag

Since I didn't get the expected answer on my last question I'll try to simplify and narrow my question:
How can I build a dropdown-menu that uses AJAX (no submit-button) to call the show action of a certain controller?
The following things are given:
Model-Association is Categories HABTM Projects, therefore the dropdown-menu consists of all category names.
The view partial where the dropdown-menu should be implemented. Below the dropdown menu is a list of projects that should change according to the choice made in the dropdown menu:
<!-- placeholder for AJAX dropdown menu -->
<!-- list of projects related to categories chosen by the select tag -->
<ul class="projects">
<% #projects.each do |_project| %>
<li>
<%= link_to(_project.name, _project) %>
</li>
<% end %>
</ul>
The Categories controller with the show-action that should be called:
class CategoriesController < ApplicationController
def show
# params[:id] should be the choice the user made in the dropdown menu
#category = Category.find(params[:id])
#projects = #category.projects.find(:all)
respond_to do |format|
format.html # show.html.erb
format.js # needed for ajax response?
end
end
def index
#projects = Category.find(params[:id]).projects.find(:all)
#category = #project.categories.first
respond_to do |format|
format.html # index.html.erb
end
end
end
The route to call the show-action in the Categories controller:
category GET /categories/:id {:controller=>"categories", :action=>"show"}
How would you implement this? Any help is very apreciated!
How about this:
<% form_for :category, :url => { :action => "show" } do |f| %>
<%= select_tag :id, options_from_collection_for_select(Category.find(:all), :id, :name),
{ :onchange => "this.form.submit();"} %>
<% end %>
That will call a traditional html call, so it will refresh the entire page (and respond to format.html).
Then the controller will find the category by the submitted [:id]
#category = Category.find(params[:id])

Categories

Resources