Rails No association found for name with React Nested form
after setup a simple association mas_one through with rails, i've tried to nest the form with react, but in the logs rails shows this message:
ArgumentError (No association found for name
location'. Has it been
' app/models/post.rb:3:in `'
defined yet?):
app/models/post.rb:17:in
the model post has one location through locatable
class Post < ApplicationRecord
belongs_to :user, touch: true
accepts_nested_attributes_for :location
has_one :locatable
has_one :location, through: :locatable
end
and in the post controller
def post_params
params.require(:post).permit(:title, :body, :user_id,
location_attributes: [:address])
end
what i've tried to is instead of accepts_nested_attributes_for :location use :locatable
and on post_params use locatable_attributes instead of location
on post.jsx
i use
buildFormData() {
let formData = new FormData();
formData.append("post[title]", this.state.post.title);
formData.append("post[body]", this.state.post.body);
formData.append("post[location_attributes][address]", this.state.address);
}
on render
render() {
return (
<div className="form-group">
<label>Address</label>
<input
type="text"
name="location"
onChange={e => this.handleAddressChange(e)}
/>
{this.renderAddressInlineError}
</div>)
}
and finally the function
handleAddressChange(e) {
let { address } = this.state;
address = e.target.value;
this.setState({ address: address });
}
So, i've tried to change location to locatable and the same error is spitted by the log.
So the question is: is that a react.js issue or the rails model issue?
ruby-on-rails ruby reactjs
add a comment |
after setup a simple association mas_one through with rails, i've tried to nest the form with react, but in the logs rails shows this message:
ArgumentError (No association found for name
location'. Has it been
' app/models/post.rb:3:in `'
defined yet?):
app/models/post.rb:17:in
the model post has one location through locatable
class Post < ApplicationRecord
belongs_to :user, touch: true
accepts_nested_attributes_for :location
has_one :locatable
has_one :location, through: :locatable
end
and in the post controller
def post_params
params.require(:post).permit(:title, :body, :user_id,
location_attributes: [:address])
end
what i've tried to is instead of accepts_nested_attributes_for :location use :locatable
and on post_params use locatable_attributes instead of location
on post.jsx
i use
buildFormData() {
let formData = new FormData();
formData.append("post[title]", this.state.post.title);
formData.append("post[body]", this.state.post.body);
formData.append("post[location_attributes][address]", this.state.address);
}
on render
render() {
return (
<div className="form-group">
<label>Address</label>
<input
type="text"
name="location"
onChange={e => this.handleAddressChange(e)}
/>
{this.renderAddressInlineError}
</div>)
}
and finally the function
handleAddressChange(e) {
let { address } = this.state;
address = e.target.value;
this.setState({ address: address });
}
So, i've tried to change location to locatable and the same error is spitted by the log.
So the question is: is that a react.js issue or the rails model issue?
ruby-on-rails ruby reactjs
add a comment |
after setup a simple association mas_one through with rails, i've tried to nest the form with react, but in the logs rails shows this message:
ArgumentError (No association found for name
location'. Has it been
' app/models/post.rb:3:in `'
defined yet?):
app/models/post.rb:17:in
the model post has one location through locatable
class Post < ApplicationRecord
belongs_to :user, touch: true
accepts_nested_attributes_for :location
has_one :locatable
has_one :location, through: :locatable
end
and in the post controller
def post_params
params.require(:post).permit(:title, :body, :user_id,
location_attributes: [:address])
end
what i've tried to is instead of accepts_nested_attributes_for :location use :locatable
and on post_params use locatable_attributes instead of location
on post.jsx
i use
buildFormData() {
let formData = new FormData();
formData.append("post[title]", this.state.post.title);
formData.append("post[body]", this.state.post.body);
formData.append("post[location_attributes][address]", this.state.address);
}
on render
render() {
return (
<div className="form-group">
<label>Address</label>
<input
type="text"
name="location"
onChange={e => this.handleAddressChange(e)}
/>
{this.renderAddressInlineError}
</div>)
}
and finally the function
handleAddressChange(e) {
let { address } = this.state;
address = e.target.value;
this.setState({ address: address });
}
So, i've tried to change location to locatable and the same error is spitted by the log.
So the question is: is that a react.js issue or the rails model issue?
ruby-on-rails ruby reactjs
after setup a simple association mas_one through with rails, i've tried to nest the form with react, but in the logs rails shows this message:
ArgumentError (No association found for name
location'. Has it been
' app/models/post.rb:3:in `'
defined yet?):
app/models/post.rb:17:in
the model post has one location through locatable
class Post < ApplicationRecord
belongs_to :user, touch: true
accepts_nested_attributes_for :location
has_one :locatable
has_one :location, through: :locatable
end
and in the post controller
def post_params
params.require(:post).permit(:title, :body, :user_id,
location_attributes: [:address])
end
what i've tried to is instead of accepts_nested_attributes_for :location use :locatable
and on post_params use locatable_attributes instead of location
on post.jsx
i use
buildFormData() {
let formData = new FormData();
formData.append("post[title]", this.state.post.title);
formData.append("post[body]", this.state.post.body);
formData.append("post[location_attributes][address]", this.state.address);
}
on render
render() {
return (
<div className="form-group">
<label>Address</label>
<input
type="text"
name="location"
onChange={e => this.handleAddressChange(e)}
/>
{this.renderAddressInlineError}
</div>)
}
and finally the function
handleAddressChange(e) {
let { address } = this.state;
address = e.target.value;
this.setState({ address: address });
}
So, i've tried to change location to locatable and the same error is spitted by the log.
So the question is: is that a react.js issue or the rails model issue?
ruby-on-rails ruby reactjs
ruby-on-rails ruby reactjs
asked Dec 29 '18 at 18:46
user9157191
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
For someone that has passing through this issue
just use: accepts_nested_attributes_for
below **has_one/has_many**
has_one/has_many through
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53972396%2frails-no-association-found-for-name-with-react-nested-form%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
For someone that has passing through this issue
just use: accepts_nested_attributes_for
below **has_one/has_many**
has_one/has_many through
add a comment |
For someone that has passing through this issue
just use: accepts_nested_attributes_for
below **has_one/has_many**
has_one/has_many through
add a comment |
For someone that has passing through this issue
just use: accepts_nested_attributes_for
below **has_one/has_many**
has_one/has_many through
For someone that has passing through this issue
just use: accepts_nested_attributes_for
below **has_one/has_many**
has_one/has_many through
answered Dec 29 '18 at 19:24
user9157191
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53972396%2frails-no-association-found-for-name-with-react-nested-form%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown