Re: [Rails] local variables in _form.html.erb undefined

2018-02-08 Thread Walter Lee Davis
Show the code where you are trying to render this form. A partial that renders 
implicitly (like in a scaffolded CRUD form):

#/app/views/posts/new.html.erb
<%= render 'form' %>

...will get a local variable named `post` in the render context. 

What does your controller look like, and what does the enclosing template look 
like that is rendering this partial?

Walter

> On Feb 8, 2018, at 6:37 PM, fugee ohu  wrote:
> 
> _active_record_post_comments.html.erb
> 
> 
>   <%= render post.comments %>
> 
> <% if (user_signed_in? && current_user != User.find(post.blogger_id)) %>
> 
>   <%= render partial: "blogit/comments/form", locals: { post: 
> post, comment: comment } %>
>   
> <% end %>
> 
> _form.html.erb
> 
> <%= form_for [post, comment], remote: true,
>   html: {
> class: "new_blog_comment", id: "new_blog_comment" } do |f| -%>
>  
> error:
> ActionView::Template::Error (undefined local variable or method `post' for 
> #<#:0x007f607eeeb278>
> Did you mean?  @post):
> 1: 
> 2: <%= form_for [post, comment], remote: true,
> 3:   html: {
> 4: class: "new_blog_comment", id: "new_blog_comment" } do |f| -%>
> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to rubyonrails-talk+unsubscr...@googlegroups.com.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/rubyonrails-talk/4d482d1f-748e-465a-a541-ca9a069ed6a1%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/36D5090D-460C-40E7-91BD-9E140241B88B%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.


[Rails] local variables in _form.html.erb undefined

2018-02-08 Thread fugee ohu
_active_record_post_comments.html.erb


  <%= render post.comments %>

<% if (user_signed_in? && current_user != User.find(post.blogger_id)) %>

<%= render partial: "blogit/comments/form", locals: { post: post, comment: 
comment } %>

<% end %>

_form.html.erb

<%= form_for [post, comment], remote: true,
  html: {
class: "new_blog_comment", id: "new_blog_comment" } do |f| -%>
 
error:
ActionView::Template::Error (undefined local variable or method `post' for 
#<#:0x007f607eeeb278>
Did you mean?  @post):
1: 
2: <%= form_for [post, comment], remote: true,
3:   html: {
4: class: "new_blog_comment", id: "new_blog_comment" } do |f| -%>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/4d482d1f-748e-465a-a541-ca9a069ed6a1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[Rails] combining bootstrap classes with search_form_for

2018-02-08 Thread Joe Guerra
I've setup ransack to do some full text search...

but the search text box isn't formated with bootstrap styling.  I've pasted 
my code below.  The top has the ransack search , below the bootstrap 
styling (need to combine them).  

Any hints?



 

<%= search_form_for @search do |f| %>



  <%#= f.label :title_or_template_cont %>
  

   
  <%= f.text_field :location_or_desc_cont , placeholder: "Search" %>
  
  
<%= f.submit "Search" %>
 

  
  
  <% end %>





  
  Search



Thanks!

Joe

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/e5c0dc37-f9ec-4c74-995c-48e9e825d9e4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Rails] Problem With Blacklight Catalog Routes

2018-02-08 Thread Dane Terrell
I've been trying to create it, and nothing seems to work. So my question 
is, how do you create it. I've never worked with Ruby On Rails

On Thursday, February 8, 2018 at 1:12:23 PM UTC-8, Hassan Schroeder wrote:
>
> On Thu, Feb 8, 2018 at 12:48 PM, Dane Terrell  > wrote: 
> > When I input localhost:3000 here is what I get 
> > 
> > NameError in CatalogController#index 
> > 
> > undefined local variable or method `search_state' for 
> > # 
>
> OK, so that's a totally different thing. Your request is being sent to 
> the right controller, but "search_state" isn't set, so... you'll have to 
> figure out where that's intended to come from. 
>
> >   find_or_initialize_search_session_from_params search_state.to_h 
>
> The "_from_params" is probably a clue there... 
>
> > When I enter localhost:3000/catalog here what I still get 
>
> > No route matches [GET] "/catalog" 
>
> Of course -- why are you even trying that when `rake routes` has 
> shown the route doesn't exist? If you want it to exist, you have to 
> create it, but it doesn't seem to be a part of this search thing. 
>
> -- 
> Hassan Schroeder  hassan.s...@gmail.com 
>  
> twitter: @hassan 
> Consulting Availability : Silicon Valley or remote 
>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/8f2b979c-5a08-4df1-9a92-821984dc7736%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Rails] Problem With Blacklight Catalog Routes

2018-02-08 Thread Hassan Schroeder
On Thu, Feb 8, 2018 at 12:48 PM, Dane Terrell  wrote:
> When I input localhost:3000 here is what I get
>
> NameError in CatalogController#index
>
> undefined local variable or method `search_state' for
> #

OK, so that's a totally different thing. Your request is being sent to
the right controller, but "search_state" isn't set, so... you'll have to
figure out where that's intended to come from.

>   find_or_initialize_search_session_from_params search_state.to_h

The "_from_params" is probably a clue there...

> When I enter localhost:3000/catalog here what I still get

> No route matches [GET] "/catalog"

Of course -- why are you even trying that when `rake routes` has
shown the route doesn't exist? If you want it to exist, you have to
create it, but it doesn't seem to be a part of this search thing.

-- 
Hassan Schroeder  hassan.schroe...@gmail.com
twitter: @hassan
Consulting Availability : Silicon Valley or remote

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/CACmC4yAz%3DxDnKQT54wF1_kDBZy6G8ayOECykvxwiVY1Ha7kBZg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Rails] Problem With Blacklight Catalog Routes

2018-02-08 Thread Dane Terrell
When I input localhost:3000 here is what I get

NameError in CatalogController#index
undefined local variable or method `search_state' for 
# Did you mean? search_results
Extracted source (around line *#51*):

49
50
51
52
53
54
  

  end
elsif start_new_search_session?
  find_or_initialize_search_session_from_params search_state.to_h
elsif search_session['id']
  begin
searches_from_history.find(search_session['id'])

Rails.root: /home/*/railsfiles/search_app
Application Trace  | Framework Trace 
 | Full Trace 

blacklight (6.14.1) app/controllers/concerns/blacklight/search_context.rb:51:in 
`find_search_session' 
blacklight (6.14.1) app/controllers/concerns/blacklight/search_context.rb:30:in 
`current_search_session' 
blacklight (6.14.1) app/controllers/concerns/blacklight/search_context.rb:35:in 
`set_current_search_session' 
activesupport (5.2.0.rc1) lib/active_support/callbacks.rb:426:in `block in 
make_lambda' 
activesupport (5.2.0.rc1) lib/active_support/callbacks.rb:179:in `block (2 
levels) in halting_and_conditional' 
actionpack (5.2.0.rc1) lib/abstract_controller/callbacks.rb:34:in `block (2 
levels) in ' 
activesupport (5.2.0.rc1) lib/active_support/callbacks.rb:180:in `block in 
halting_and_conditional' 
activesupport (5.2.0.rc1) lib/active_support/callbacks.rb:513:in `block in 
invoke_before' 
activesupport (5.2.0.rc1) lib/active_support/callbacks.rb:513:in `each' 

activesupport (5.2.0.rc1) lib/active_support/callbacks.rb:513:in 
`invoke_before' 
activesupport (5.2.0.rc1) lib/active_support/callbacks.rb:131:in 
`run_callbacks' 
actionpack (5.2.0.rc1) lib/abstract_controller/callbacks.rb:41:in 
`process_action' 
actionpack (5.2.0.rc1) lib/action_controller/metal/rescue.rb:22:in 
`process_action' 
actionpack (5.2.0.rc1) lib/action_controller/metal/instrumentation.rb:34:in 
`block in process_action' 
activesupport (5.2.0.rc1) lib/active_support/notifications.rb:168:in `block in 
instrument' 
activesupport (5.2.0.rc1) 
lib/active_support/notifications/instrumenter.rb:23:in `instrument' 

activesupport (5.2.0.rc1) lib/active_support/notifications.rb:168:in 
`instrument' 
actionpack (5.2.0.rc1) lib/action_controller/metal/instrumentation.rb:32:in 
`process_action' 
actionpack (5.2.0.rc1) lib/action_controller/metal/params_wrapper.rb:256:in 
`process_action' 
activerecord (5.2.0.rc1) lib/active_record/railties/controller_runtime.rb:24:in 
`process_action' 
actionpack (5.2.0.rc1) lib/abstract_controller/base.rb:134:in `process' 

actionview (5.2.0.rc1) lib/action_view/rendering.rb:32:in `process' 

actionpack (5.2.0.rc1) lib/action_controller/metal.rb:191:in `dispatch' 

actionpack (5.2.0.rc1) lib/action_controller/metal.rb:252:in `dispatch' 

actionpack (5.2.0.rc1) lib/action_dispatch/routing/route_set.rb:52:in 
`dispatch' 
actionpack (5.2.0.rc1) lib/action_dispatch/routing/route_set.rb:34:in `serve' 

actionpack (5.2.0.rc1) lib/action_dispatch/journey/router.rb:52:in `block in 
serve' 
actionpack (5.2.0.rc1) lib/action_dispatch/journey/router.rb:35:in `each' 

actionpack (5.2.0.rc1) lib/action_dispatch/journey/router.rb:35:in `serve' 

actionpack (5.2.0.rc1) lib/action_dispatch/routing/route_set.rb:840:in `call' 

warden (1.2.7) lib/warden/manager.rb:36:in `block in call' 

warden (1.2.7) lib/warden/manager.rb:35:in `catch' 
warden (1.2.7) lib/warden/manager.rb:35:in `call' 
rack (2.0.4) lib/rack/tempfile_reaper.rb:15:in `call' 
rack (2.0.4) lib/rack/etag.rb:25:in `call' 
rack (2.0.4) lib/rack/conditional_get.rb:25:in `call' 
rack (2.0.4) lib/rack/head.rb:12:in `call' 
actionpack (5.2.0.rc1) 
lib/action_dispatch/http/content_security_policy.rb:18:in `call' 

rack (2.0.4) lib/rack/session/abstract/id.rb:232:in `context' 

rack (2.0.4) lib/rack/session/abstract/id.rb:226:in `call' 

actionpack (5.2.0.rc1) lib/action_dispatch/middleware/cookies.rb:663:in `call' 

activerecord 

Re: [Rails] Problem With Blacklight Catalog Routes

2018-02-08 Thread Hassan Schroeder
On Thu, Feb 8, 2018 at 12:02 PM, Dane Terrell 
answered 1 of 4 questions with:

> Here is the output

So it appears that the error message

No route matches [GET] "/catalog"

is correct    Did you enter that in the address bar or did going to
literally "http:/localhost:3000/" generate that error?

-- 
Hassan Schroeder  hassan.schroe...@gmail.com
twitter: @hassan
Consulting Availability : Silicon Valley or remote

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/CACmC4yArV_SBET3yjgf6oaTXssJUmNchHvztHE3F5achX0HqmQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Rails] Problem With Blacklight Catalog Routes

2018-02-08 Thread Dane Terrell
Here is the output

rake routes
   Prefix Verb URI Pattern  
 Controller#Action
   blacklight  /
 Blacklight::Engine
 root GET  /
 catalog#index
 new_user_session GET  /users/sign_in(.:format)
  devise/sessions#new
 user_session POST /users/sign_in(.:format)
  devise/sessions#create
 destroy_user_session GET  /users/sign_out(.:format)
 devise/sessions#destroy
new_user_password GET  /users/password/new(.:format)
 devise/passwords#new
   edit_user_password GET  /users/password/edit(.:format)  
  devise/passwords#edit
user_password PATCH/users/password(.:format)
 devise/passwords#update
  PUT  /users/password(.:format)
 devise/passwords#update
  POST /users/password(.:format)
 devise/passwords#create
 cancel_user_registration GET  /users/cancel(.:format)  
 devise/registrations#cancel
new_user_registration GET  /users/sign_up(.:format)
  devise/registrations#new
   edit_user_registration GET  /users/edit(.:format)
 devise/registrations#edit
user_registration PATCH/users(.:format)
  devise/registrations#update
  PUT  /users(.:format)
  devise/registrations#update
  DELETE   /users(.:format)
  devise/registrations#destroy
  POST /users(.:format)
  devise/registrations#create
  email_solr_document GET|POST /catalog/:id/email(.:format)
  catalog#email
sms_solr_document GET|POST /catalog/:id/sms(.:format)  
  catalog#sms
   citation_solr_document GET  /catalog/:id/citation(.:format)  
 catalog#citation
 email_solr_documents GET|POST /catalog/email(.:format)
  catalog#email
   sms_solr_documents GET|POST /catalog/sms(.:format)  
  catalog#sms
  citation_solr_documents GET  /catalog/citation(.:format)  
 catalog#citation
solr_document GET  /catalog/:id(.:format)  
  catalog#show
 catalog_static_pages GET  /static_pages/catalog(.:format)  
 static_pages#catalog
 new_static_pages GET  /static_pages/new(.:format)  
 static_pages#new
edit_static_pages GET  /static_pages/edit(.:format)
  static_pages#edit
 static_pages GET  /static_pages(.:format)  
 static_pages#show
  PATCH/static_pages(.:format)  
 static_pages#update
  PUT  /static_pages(.:format)  
 static_pages#update
  DELETE   /static_pages(.:format)  
 static_pages#destroy
  POST /static_pages(.:format)  
 static_pages#create
   email_bookmark GET|POST /bookmarks/:id/email(.:format)  
  bookmarks#email
 sms_bookmark GET|POST /bookmarks/:id/sms(.:format)
  bookmarks#sms
citation_bookmark GET  /bookmarks/:id/citation(.:format)
 

Re: [Rails] Problem With Blacklight Catalog Routes

2018-02-08 Thread Hassan Schroeder
On Thu, Feb 8, 2018 at 9:53 AM, Dane Terrell  wrote:
> Hi, I'm starting my first ruby on rails app featuring blacklight with solr.
> I can install and loginto the solr blacklight server on port 8983, but I
> can't seem to access localhost:3000. It appears to be a routing issue.

Never heard of Blacklight but the first obvious questions are

versions of Ruby | Rails | Blacklight ? output of `rake routes` ?

-- 
Hassan Schroeder  hassan.schroe...@gmail.com
twitter: @hassan
Consulting Availability : Silicon Valley or remote

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/CACmC4yBxfsrk_G0h%3Dkye%2BXRnwXgKemZOzaEbSw1Zb1C4wqdL9w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


[Rails] Problem With Blacklight Catalog Routes

2018-02-08 Thread Dane Terrell
Hi, I'm starting my first ruby on rails app featuring blacklight with solr. 
I can install and loginto the solr blacklight server on port 8983, but I 
can't seem to access localhost:3000. It appears to be a routing issue. When 
I try to access localhost:3000 here is what I get..

Routing Error
No route matches [GET] "/catalog"
Rails.root: /home/*/railsfiles/search_app
I've tried adding routes to no avail. Here is the contents of my routes.rb 
file as it came installed by default with your project setup plus my 
addition of the get: catalog route.
Can you help?

Rails.application.routes.draw do

  mount Blacklight::Engine => '/'
  root to: "catalog#index"
  devise_for :users
  concern :exportable, Blacklight::Routes::Exportable.new

  resources :solr_documents, only: [:show], path: '/catalog', controller: 
'cata$
concerns :exportable
  end

  resource :static_pages do
  collection do
get :catalog
  end
end

  resources :bookmarks do
concerns :exportable

collection do
  delete 'clear'
end
  end
  # For details on the DSL available within this file, see 
http://guides.rubyon$
end


 
Dane 

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/337d4666-d7f3-4772-8b0d-9f6a4a015c37%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Rails] has_many_through new association for exisitng records

2018-02-08 Thread fugee ohu


On Thursday, February 8, 2018 at 12:23:43 PM UTC-5, Walter Lee Davis wrote:
>
> That validation was probably on the person, not the picture, unless you 
> added validates_associated to the Person class. 
>
> Look at this: 
> https://github.com/walterdavis/fugee/blob/master/app/controllers/people_controller.rb#L74
>  
>
> and this: 
>
>
> https://github.com/walterdavis/fugee/blob/master/app/views/people/_form.html.erb#L25
>  
>
> The rest is scaffolded, there's nothing mysterious here. 
>
> Clone this to your machine, run it in rails server. 
>
> Go to localhost:3000/pictures and add some pictures (just file names). 
>
> Go to localhost:3000/people, and add some people. 
>
> See how you can choose pictures for each person? See how the association 
> is saved and updated? Watch in the console as the record is saved or 
> updated from the web. 
>
> Walter 
>
> > On Feb 8, 2018, at 11:36 AM, fugee ohu  
> wrote: 
> > 
> > I created an update action in the persons controller Valitadation was 
> failing with :name can't be blank so I assumed @person.save was trying to 
> create a new picture That's why I moved the action from the pictures 
> controller to the persons controller and changed the action to 
> @person.update instead of @person.save Did you already understand that? 
>
>
I'll do that today thanks In the meanwhile, the validtion's in picture.rb

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/0e19c5db-fbb2-4c31-ba79-ca3ec73a3778%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Rails] has_many_through new association for exisitng records

2018-02-08 Thread Walter Lee Davis
That validation was probably on the person, not the picture, unless you added 
validates_associated to the Person class.

Look at this: 
https://github.com/walterdavis/fugee/blob/master/app/controllers/people_controller.rb#L74

and this:

https://github.com/walterdavis/fugee/blob/master/app/views/people/_form.html.erb#L25

The rest is scaffolded, there's nothing mysterious here.

Clone this to your machine, run it in rails server.

Go to localhost:3000/pictures and add some pictures (just file names).

Go to localhost:3000/people, and add some people. 

See how you can choose pictures for each person? See how the association is 
saved and updated? Watch in the console as the record is saved or updated from 
the web.

Walter

> On Feb 8, 2018, at 11:36 AM, fugee ohu  wrote:
> 
> I created an update action in the persons controller Valitadation was failing 
> with :name can't be blank so I assumed @person.save was trying to create a 
> new picture That's why I moved the action from the pictures controller to the 
> persons controller and changed the action to @person.update instead of 
> @person.save Did you already understand that?

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/7373F00F-930B-4491-B1B2-1E48DCC574E4%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Rails] has_many_through new association for exisitng records

2018-02-08 Thread fugee ohu


On Thursday, February 8, 2018 at 11:36:47 AM UTC-5, fugee ohu wrote:
>
>
>
> On Thursday, February 8, 2018 at 11:10:29 AM UTC-5, Walter Lee Davis wrote:
>>
>>
>> > On Feb 8, 2018, at 8:28 AM, fugee ohu  wrote: 
>> > 
>> > I was having trouble hiding and showing elements on the form so I 
>> created a separate action in the pictures controller to handle the 
>> submission of the form that selects pictures to add to the person from 
>> existing pictures in the database It attempts to create a new picture I 
>> guess because validation fails :name cannot be blank 
>> > 
>> > def add_from_pictures_create 
>> >if (params[:picture][:person_id]) 
>> >@person=Person.find(params[:picture][:person_id]) 
>> >@pictures = Picture.find(params[:person][:picture_ids]) 
>>
>> # these are now new instances -- with different object IDs -- than the 
>> ones you want to compare them with 
>>
>> >@person.pictures << @pictures 
>>
>> # assigning these in this way means that you are adding an array to an 
>> array. Trouble is, you are adding duplicate instances in the second array 
>> to an array which may already contain the ones you want to add. 
>>
>> >respond_to do |format| 
>> >  if @person.save 
>> > format.html {  redirect_to 
>> pictures_path(person_id: params[:picture][:person_id]), notice: 'Person 
>> picture updated.' } 
>> > format.json { render :show, status: 
>> :created, location: @picture } 
>>
>> I would avoid this entire approach. Think about this in the abstract: you 
>> are editing and updating the @person, manipulating the picture_ids 
>> attribute on that instance. Your form is built on the person. This form 
>> should be submitted to the PersonController#update method. If you have (as 
>> I have said many times in this thread) whitelisted the picture_ids 
>> attribute, then simply calling @person.save will persist that attribute -- 
>> it is already going to be in the person_params strong parameters hash. 
>>
>> Here's a clue: when you find yourself writing something like this in a 
>> controller: 
>>
>> > @person=Person.find(params[:picture][:person_id]) 
>>
>> ...just walk away and have a think about your life. 
>>
>> Walter 
>>
>>
> I created an update action in the persons controller Valitadation was 
> failing with :name can't be blank so I assumed @person.save was trying to 
> create a new picture That's why I moved the action from the pictures 
> controller to the persons controller and changed the action to 
> @person.update instead of @person.save Did you already understand that?
>

You wanted me to use patch or post to achieve @person.pictures << @pictures 
and then I would wanna use if @person.save or if @person.update Please 
clarify that Thanks in advance  I took out the stuff left over from when 
this routine was in the pictures controller and now rely on the callback 
set_person 

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/40052a2b-87bd-40be-ac63-510da6b1c6d4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Rails] has_many_through new association for exisitng records

2018-02-08 Thread fugee ohu


On Thursday, February 8, 2018 at 11:10:29 AM UTC-5, Walter Lee Davis wrote:
>
>
> > On Feb 8, 2018, at 8:28 AM, fugee ohu  
> wrote: 
> > 
> > I was having trouble hiding and showing elements on the form so I 
> created a separate action in the pictures controller to handle the 
> submission of the form that selects pictures to add to the person from 
> existing pictures in the database It attempts to create a new picture I 
> guess because validation fails :name cannot be blank 
> > 
> > def add_from_pictures_create 
> >if (params[:picture][:person_id]) 
> >@person=Person.find(params[:picture][:person_id]) 
> >@pictures = Picture.find(params[:person][:picture_ids]) 
>
> # these are now new instances -- with different object IDs -- than the 
> ones you want to compare them with 
>
> >@person.pictures << @pictures 
>
> # assigning these in this way means that you are adding an array to an 
> array. Trouble is, you are adding duplicate instances in the second array 
> to an array which may already contain the ones you want to add. 
>
> >respond_to do |format| 
> >  if @person.save 
> > format.html {  redirect_to 
> pictures_path(person_id: params[:picture][:person_id]), notice: 'Person 
> picture updated.' } 
> > format.json { render :show, status: 
> :created, location: @picture } 
>
> I would avoid this entire approach. Think about this in the abstract: you 
> are editing and updating the @person, manipulating the picture_ids 
> attribute on that instance. Your form is built on the person. This form 
> should be submitted to the PersonController#update method. If you have (as 
> I have said many times in this thread) whitelisted the picture_ids 
> attribute, then simply calling @person.save will persist that attribute -- 
> it is already going to be in the person_params strong parameters hash. 
>
> Here's a clue: when you find yourself writing something like this in a 
> controller: 
>
> > @person=Person.find(params[:picture][:person_id]) 
>
> ...just walk away and have a think about your life. 
>
> Walter 
>
>
I created an update action in the persons controller Valitadation was 
failing with :name can't be blank so I assumed @person.save was trying to 
create a new picture That's why I moved the action from the pictures 
controller to the persons controller and changed the action to 
@person.update instead of @person.save Did you already understand that?

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/287ca87d-266b-44b4-8ae8-847b6b3feb30%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Rails] has_many_through new association for exisitng records

2018-02-08 Thread Walter Lee Davis

> On Feb 8, 2018, at 8:28 AM, fugee ohu  wrote:
> 
> I was having trouble hiding and showing elements on the form so I created a 
> separate action in the pictures controller to handle the submission of the 
> form that selects pictures to add to the person from existing pictures in the 
> database It attempts to create a new picture I guess because validation fails 
> :name cannot be blank
> 
> def add_from_pictures_create
>if (params[:picture][:person_id])
>@person=Person.find(params[:picture][:person_id])
>@pictures = Picture.find(params[:person][:picture_ids])

# these are now new instances -- with different object IDs -- than the ones you 
want to compare them with

>@person.pictures << @pictures

# assigning these in this way means that you are adding an array to an array. 
Trouble is, you are adding duplicate instances in the second array to an array 
which may already contain the ones you want to add.

>respond_to do |format|
>  if @person.save
> format.html {  redirect_to 
> pictures_path(person_id: params[:picture][:person_id]), notice: 'Person 
> picture updated.' }
> format.json { render :show, status: :created, 
> location: @picture } 

I would avoid this entire approach. Think about this in the abstract: you are 
editing and updating the @person, manipulating the picture_ids attribute on 
that instance. Your form is built on the person. This form should be submitted 
to the PersonController#update method. If you have (as I have said many times 
in this thread) whitelisted the picture_ids attribute, then simply calling 
@person.save will persist that attribute -- it is already going to be in the 
person_params strong parameters hash.

Here's a clue: when you find yourself writing something like this in a 
controller:

> @person=Person.find(params[:picture][:person_id])

...just walk away and have a think about your life.

Walter

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/6319F830-3E44-455C-AD7E-F1924DFB36EE%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Rails] has_many_through new association for exisitng records

2018-02-08 Thread fugee ohu


On Sunday, February 4, 2018 at 6:25:46 PM UTC-5, Walter Lee Davis wrote:
>
> Even more interesting (to me, anyway) is what happens if you create (but 
> don't save) any of these objects: 
>
> 2.4.2 :006 > p = Person.new name: 'Walter' 
>  => # 
> 2.4.2 :007 > c = Picture.new file: 'some file' 
>  => # nil> 
> 2.4.2 :008 > p.pictures << c 
>  => # file: "some file", created_at: nil, updated_at: nil>]> 
> 2.4.2 :009 > p.save 
>(1.9ms)  begin transaction 
>   SQL (23.1ms)  INSERT INTO "people" ("name", "created_at", "updated_at") 
> VALUES (?, ?, ?)  [["name", "Walter"], ["created_at", "2018-02-04 
> 23:23:15.342294"], ["updated_at", "2018-02-04 23:23:15.342294"]] 
>   SQL (0.3ms)  INSERT INTO "pictures" ("file", "created_at", "updated_at") 
> VALUES (?, ?, ?)  [["file", "some file"], ["created_at", "2018-02-04 
> 23:23:15.374862"], ["updated_at", "2018-02-04 23:23:15.374862"]] 
>   SQL (0.4ms)  INSERT INTO "person_pictures" ("person_id", "picture_id", 
> "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["person_id", 2], 
> ["picture_id", 2], ["created_at", "2018-02-04 23:23:15.376805"], 
> ["updated_at", "2018-02-04 23:23:15.376805"]] 
>(1.3ms)  commit transaction 
>  => true 
> 2.4.2 :010 > 
>
> When you save one of them, all three are saved in order to preserve the 
> entire set of relationships. 
>
> Walter 
>
> > On Feb 4, 2018, at 4:54 PM, Walter Lee Davis  > wrote: 
> > 
> > I can't duplicate this finding here. Here's the console log: 
> > 
> > 2.4.2 :001 > p = Person.new name: 'Walter' 
> > => # 
> > 2.4.2 :002 > p.save 
> >   (0.2ms)  begin transaction 
> >  SQL (2.8ms)  INSERT INTO "people" ("name", "created_at", "updated_at") 
> VALUES (?, ?, ?)  [["name", "Walter"], ["created_at", "2018-02-04 
> 21:47:31.000973"], ["updated_at", "2018-02-04 21:47:31.000973"]] 
> >   (1.3ms)  commit transaction 
> > => true 
> > 2.4.2 :003 > c = Picture.new file: 'some file' 
> > => # nil> 
> > 2.4.2 :004 > c.save 
> >   (0.2ms)  begin transaction 
> >  SQL (2.2ms)  INSERT INTO "pictures" ("file", "created_at", 
> "updated_at") VALUES (?, ?, ?)  [["file", "some file"], ["created_at", 
> "2018-02-04 21:47:54.717609"], ["updated_at", "2018-02-04 
> 21:47:54.717609"]] 
> >   (1.4ms)  commit transaction 
> > => true 
> > 2.4.2 :005 > p.pictures << c 
> >   (0.1ms)  begin transaction 
> >  SQL (0.6ms)  INSERT INTO "person_pictures" ("person_id", "picture_id", 
> "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["person_id", 1], 
> ["picture_id", 1], ["created_at", "2018-02-04 21:47:58.847298"], 
> ["updated_at", "2018-02-04 21:47:58.847298"]] 
> >   (1.1ms)  commit transaction 
> >  Picture Load (0.4ms)  SELECT  "pictures".* FROM "pictures" INNER JOIN 
> "person_pictures" ON "pictures"."id" = "person_pictures"."picture_id" WHERE 
> "person_pictures"."person_id" = ? LIMIT ?  [["person_id", 1], ["LIMIT", 
> 11]] 
> > => # "some file", created_at: "2018-02-04 21:47:54", updated_at: "2018-02-04 
> 21:47:54">]> 
> > 2.4.2 :006 > 
> > 
> > Here's the models: 
> > 
> > class Picture < ApplicationRecord 
> >  has_many :person_pictures 
> >  has_many :people, through: :person_pictures 
> > end 
> > 
> > class Person < ApplicationRecord 
> >  has_many :person_pictures 
> >  has_many :pictures, through: :person_pictures 
> > end 
> > 
> > class PersonPicture < ApplicationRecord 
> >  belongs_to :person 
> >  belongs_to :picture 
> > end 
> > 
> > Whatever is happening on your app is not clear, but you can see that 
> after you save the person and the picture, when you add that saved picture 
> to the saved person's 'pictures' collection, the only record that gets 
> created is a person_picture. Now if either the person or the picture was in 
> the "new" state, that is to say, not saved yet, then I could imagine that 
> it would be saved by ActiveRecord first in order to allow the 
> person_picture record to be saved. Both IDs have to be known before the 
> join object can be saved. 
> > 
> > Walter 
> > 
> >> On Feb 4, 2018, at 2:30 PM, fugee ohu  
> wrote: 
> >> 
> >> In a has_many_through association where both records exist how do I 
> create a new association 
> >> In this case Person has_many_pictures through person_picture and I'm 
> trying to add an existing picture to an existing person like this: 
> >> @person.pictures << @picture 
> >> But this creates a new picture instead of adding the association 
> >> 
> >> 
> >> -- 
> >> You received this message because you are subscribed to the Google 
> Groups "Ruby on Rails: Talk" group. 
> >> To unsubscribe from this group and stop receiving emails from it, send 
> an email to rubyonrails-ta...@googlegroups.com . 
> >> To post to this group, send email to rubyonra...@googlegroups.com 
> . 
> >> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/rubyonrails-talk/741e9a16-483f-4fc6-a2e7-77706b9b250c%40googlegroups.com.
>  
>
> >> For more options, visit https://groups.google.com/d/optout. 
> > 
> > 

Re: [Rails] has_many_through new association for exisitng records

2018-02-08 Thread fugee ohu


On Sunday, February 4, 2018 at 6:25:46 PM UTC-5, Walter Lee Davis wrote:
>
> Even more interesting (to me, anyway) is what happens if you create (but 
> don't save) any of these objects: 
>
> 2.4.2 :006 > p = Person.new name: 'Walter' 
>  => # 
> 2.4.2 :007 > c = Picture.new file: 'some file' 
>  => # nil> 
> 2.4.2 :008 > p.pictures << c 
>  => # file: "some file", created_at: nil, updated_at: nil>]> 
> 2.4.2 :009 > p.save 
>(1.9ms)  begin transaction 
>   SQL (23.1ms)  INSERT INTO "people" ("name", "created_at", "updated_at") 
> VALUES (?, ?, ?)  [["name", "Walter"], ["created_at", "2018-02-04 
> 23:23:15.342294"], ["updated_at", "2018-02-04 23:23:15.342294"]] 
>   SQL (0.3ms)  INSERT INTO "pictures" ("file", "created_at", "updated_at") 
> VALUES (?, ?, ?)  [["file", "some file"], ["created_at", "2018-02-04 
> 23:23:15.374862"], ["updated_at", "2018-02-04 23:23:15.374862"]] 
>   SQL (0.4ms)  INSERT INTO "person_pictures" ("person_id", "picture_id", 
> "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["person_id", 2], 
> ["picture_id", 2], ["created_at", "2018-02-04 23:23:15.376805"], 
> ["updated_at", "2018-02-04 23:23:15.376805"]] 
>(1.3ms)  commit transaction 
>  => true 
> 2.4.2 :010 > 
>
> When you save one of them, all three are saved in order to preserve the 
> entire set of relationships. 
>
> Walter 
>
> > On Feb 4, 2018, at 4:54 PM, Walter Lee Davis  > wrote: 
> > 
> > I can't duplicate this finding here. Here's the console log: 
> > 
> > 2.4.2 :001 > p = Person.new name: 'Walter' 
> > => # 
> > 2.4.2 :002 > p.save 
> >   (0.2ms)  begin transaction 
> >  SQL (2.8ms)  INSERT INTO "people" ("name", "created_at", "updated_at") 
> VALUES (?, ?, ?)  [["name", "Walter"], ["created_at", "2018-02-04 
> 21:47:31.000973"], ["updated_at", "2018-02-04 21:47:31.000973"]] 
> >   (1.3ms)  commit transaction 
> > => true 
> > 2.4.2 :003 > c = Picture.new file: 'some file' 
> > => # nil> 
> > 2.4.2 :004 > c.save 
> >   (0.2ms)  begin transaction 
> >  SQL (2.2ms)  INSERT INTO "pictures" ("file", "created_at", 
> "updated_at") VALUES (?, ?, ?)  [["file", "some file"], ["created_at", 
> "2018-02-04 21:47:54.717609"], ["updated_at", "2018-02-04 
> 21:47:54.717609"]] 
> >   (1.4ms)  commit transaction 
> > => true 
> > 2.4.2 :005 > p.pictures << c 
> >   (0.1ms)  begin transaction 
> >  SQL (0.6ms)  INSERT INTO "person_pictures" ("person_id", "picture_id", 
> "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["person_id", 1], 
> ["picture_id", 1], ["created_at", "2018-02-04 21:47:58.847298"], 
> ["updated_at", "2018-02-04 21:47:58.847298"]] 
> >   (1.1ms)  commit transaction 
> >  Picture Load (0.4ms)  SELECT  "pictures".* FROM "pictures" INNER JOIN 
> "person_pictures" ON "pictures"."id" = "person_pictures"."picture_id" WHERE 
> "person_pictures"."person_id" = ? LIMIT ?  [["person_id", 1], ["LIMIT", 
> 11]] 
> > => # "some file", created_at: "2018-02-04 21:47:54", updated_at: "2018-02-04 
> 21:47:54">]> 
> > 2.4.2 :006 > 
> > 
> > Here's the models: 
> > 
> > class Picture < ApplicationRecord 
> >  has_many :person_pictures 
> >  has_many :people, through: :person_pictures 
> > end 
> > 
> > class Person < ApplicationRecord 
> >  has_many :person_pictures 
> >  has_many :pictures, through: :person_pictures 
> > end 
> > 
> > class PersonPicture < ApplicationRecord 
> >  belongs_to :person 
> >  belongs_to :picture 
> > end 
> > 
> > Whatever is happening on your app is not clear, but you can see that 
> after you save the person and the picture, when you add that saved picture 
> to the saved person's 'pictures' collection, the only record that gets 
> created is a person_picture. Now if either the person or the picture was in 
> the "new" state, that is to say, not saved yet, then I could imagine that 
> it would be saved by ActiveRecord first in order to allow the 
> person_picture record to be saved. Both IDs have to be known before the 
> join object can be saved. 
> > 
> > Walter 
> > 
> >> On Feb 4, 2018, at 2:30 PM, fugee ohu  
> wrote: 
> >> 
> >> In a has_many_through association where both records exist how do I 
> create a new association 
> >> In this case Person has_many_pictures through person_picture and I'm 
> trying to add an existing picture to an existing person like this: 
> >> @person.pictures << @picture 
> >> But this creates a new picture instead of adding the association 
> >> 
> >> 
> >> -- 
> >> You received this message because you are subscribed to the Google 
> Groups "Ruby on Rails: Talk" group. 
> >> To unsubscribe from this group and stop receiving emails from it, send 
> an email to rubyonrails-ta...@googlegroups.com . 
> >> To post to this group, send email to rubyonra...@googlegroups.com 
> . 
> >> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/rubyonrails-talk/741e9a16-483f-4fc6-a2e7-77706b9b250c%40googlegroups.com.
>  
>
> >> For more options, visit https://groups.google.com/d/optout. 
> > 
> >